xref: /freebsd/contrib/llvm-project/clang/lib/ASTMatchers/Dynamic/Registry.cpp (revision a7dea1671b87c07d2d266f836bfa8b58efc7c134)
1 //===- Registry.cpp - Matcher registry ------------------------------------===//
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 /// \file
10 /// Registry map populated at static initialization time.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/ASTMatchers/Dynamic/Registry.h"
15 #include "Marshallers.h"
16 #include "clang/AST/ASTTypeTraits.h"
17 #include "clang/ASTMatchers/ASTMatchers.h"
18 #include "clang/ASTMatchers/Dynamic/Diagnostics.h"
19 #include "clang/ASTMatchers/Dynamic/VariantValue.h"
20 #include "llvm/ADT/Optional.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ADT/StringMap.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/Support/ManagedStatic.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include <cassert>
27 #include <iterator>
28 #include <memory>
29 #include <set>
30 #include <string>
31 #include <utility>
32 #include <vector>
33 
34 using namespace clang::ast_type_traits;
35 
36 namespace clang {
37 namespace ast_matchers {
38 namespace dynamic {
39 
40 namespace {
41 
42 using internal::MatcherDescriptor;
43 
44 using ConstructorMap = llvm::StringMap<std::unique_ptr<const MatcherDescriptor>>;
45 
46 class RegistryMaps {
47 public:
48   RegistryMaps();
49   ~RegistryMaps();
50 
51   const ConstructorMap &constructors() const { return Constructors; }
52 
53 private:
54   void registerMatcher(StringRef MatcherName,
55                        std::unique_ptr<MatcherDescriptor> Callback);
56 
57   ConstructorMap Constructors;
58 };
59 
60 } // namespace
61 
62 void RegistryMaps::registerMatcher(
63     StringRef MatcherName, std::unique_ptr<MatcherDescriptor> Callback) {
64   assert(Constructors.find(MatcherName) == Constructors.end());
65   Constructors[MatcherName] = std::move(Callback);
66 }
67 
68 #define REGISTER_MATCHER(name)                                                 \
69   registerMatcher(#name, internal::makeMatcherAutoMarshall(                    \
70                              ::clang::ast_matchers::name, #name));
71 
72 #define REGISTER_MATCHER_OVERLOAD(name)                                        \
73   registerMatcher(#name,                                                       \
74       std::make_unique<internal::OverloadedMatcherDescriptor>(name##Callbacks))
75 
76 #define SPECIFIC_MATCHER_OVERLOAD(name, Id)                                    \
77   static_cast<::clang::ast_matchers::name##_Type##Id>(                         \
78       ::clang::ast_matchers::name)
79 
80 #define MATCHER_OVERLOAD_ENTRY(name, Id)                                       \
81         internal::makeMatcherAutoMarshall(SPECIFIC_MATCHER_OVERLOAD(name, Id), \
82                                           #name)
83 
84 #define REGISTER_OVERLOADED_2(name)                                            \
85   do {                                                                         \
86     std::unique_ptr<MatcherDescriptor> name##Callbacks[] = {                   \
87         MATCHER_OVERLOAD_ENTRY(name, 0),                                       \
88         MATCHER_OVERLOAD_ENTRY(name, 1)};                                      \
89     REGISTER_MATCHER_OVERLOAD(name);                                           \
90   } while (false)
91 
92 /// Generate a registry map with all the known matchers.
93 /// Please keep sorted alphabetically!
94 RegistryMaps::RegistryMaps() {
95   // TODO: Here is the list of the missing matchers, grouped by reason.
96   //
97   // Need Variant/Parser fixes:
98   // ofKind
99   //
100   // Polymorphic + argument overload:
101   // findAll
102   //
103   // Other:
104   // equalsNode
105 
106   REGISTER_OVERLOADED_2(callee);
107   REGISTER_OVERLOADED_2(hasPrefix);
108   REGISTER_OVERLOADED_2(hasType);
109   REGISTER_OVERLOADED_2(ignoringParens);
110   REGISTER_OVERLOADED_2(isDerivedFrom);
111   REGISTER_OVERLOADED_2(isDirectlyDerivedFrom);
112   REGISTER_OVERLOADED_2(isSameOrDerivedFrom);
113   REGISTER_OVERLOADED_2(loc);
114   REGISTER_OVERLOADED_2(pointsTo);
115   REGISTER_OVERLOADED_2(references);
116   REGISTER_OVERLOADED_2(thisPointerType);
117 
118   std::unique_ptr<MatcherDescriptor> equalsCallbacks[] = {
119       MATCHER_OVERLOAD_ENTRY(equals, 0),
120       MATCHER_OVERLOAD_ENTRY(equals, 1),
121       MATCHER_OVERLOAD_ENTRY(equals, 2),
122   };
123   REGISTER_MATCHER_OVERLOAD(equals);
124 
125   REGISTER_MATCHER(accessSpecDecl);
126   REGISTER_MATCHER(addrLabelExpr);
127   REGISTER_MATCHER(alignOfExpr);
128   REGISTER_MATCHER(allOf);
129   REGISTER_MATCHER(anyOf);
130   REGISTER_MATCHER(anything);
131   REGISTER_MATCHER(argumentCountIs);
132   REGISTER_MATCHER(arraySubscriptExpr);
133   REGISTER_MATCHER(arrayType);
134   REGISTER_MATCHER(asString);
135   REGISTER_MATCHER(asmStmt);
136   REGISTER_MATCHER(atomicExpr);
137   REGISTER_MATCHER(atomicType);
138   REGISTER_MATCHER(autoType);
139   REGISTER_MATCHER(autoreleasePoolStmt)
140   REGISTER_MATCHER(binaryConditionalOperator);
141   REGISTER_MATCHER(binaryOperator);
142   REGISTER_MATCHER(blockDecl);
143   REGISTER_MATCHER(blockExpr);
144   REGISTER_MATCHER(blockPointerType);
145   REGISTER_MATCHER(booleanType);
146   REGISTER_MATCHER(breakStmt);
147   REGISTER_MATCHER(builtinType);
148   REGISTER_MATCHER(cStyleCastExpr);
149   REGISTER_MATCHER(callExpr);
150   REGISTER_MATCHER(caseStmt);
151   REGISTER_MATCHER(castExpr);
152   REGISTER_MATCHER(characterLiteral);
153   REGISTER_MATCHER(chooseExpr);
154   REGISTER_MATCHER(classTemplateDecl);
155   REGISTER_MATCHER(classTemplateSpecializationDecl);
156   REGISTER_MATCHER(complexType);
157   REGISTER_MATCHER(compoundLiteralExpr);
158   REGISTER_MATCHER(compoundStmt);
159   REGISTER_MATCHER(conditionalOperator);
160   REGISTER_MATCHER(constantArrayType);
161   REGISTER_MATCHER(constantExpr);
162   REGISTER_MATCHER(containsDeclaration);
163   REGISTER_MATCHER(continueStmt);
164   REGISTER_MATCHER(cudaKernelCallExpr);
165   REGISTER_MATCHER(cxxBindTemporaryExpr);
166   REGISTER_MATCHER(cxxBoolLiteral);
167   REGISTER_MATCHER(cxxCatchStmt);
168   REGISTER_MATCHER(cxxConstCastExpr);
169   REGISTER_MATCHER(cxxConstructExpr);
170   REGISTER_MATCHER(cxxConstructorDecl);
171   REGISTER_MATCHER(cxxConversionDecl);
172   REGISTER_MATCHER(cxxCtorInitializer);
173   REGISTER_MATCHER(cxxDeductionGuideDecl);
174   REGISTER_MATCHER(cxxDefaultArgExpr);
175   REGISTER_MATCHER(cxxDeleteExpr);
176   REGISTER_MATCHER(cxxDependentScopeMemberExpr);
177   REGISTER_MATCHER(cxxDestructorDecl);
178   REGISTER_MATCHER(cxxDynamicCastExpr);
179   REGISTER_MATCHER(cxxForRangeStmt);
180   REGISTER_MATCHER(cxxFunctionalCastExpr);
181   REGISTER_MATCHER(cxxMemberCallExpr);
182   REGISTER_MATCHER(cxxMethodDecl);
183   REGISTER_MATCHER(cxxNewExpr);
184   REGISTER_MATCHER(cxxNullPtrLiteralExpr);
185   REGISTER_MATCHER(cxxOperatorCallExpr);
186   REGISTER_MATCHER(cxxRecordDecl);
187   REGISTER_MATCHER(cxxReinterpretCastExpr);
188   REGISTER_MATCHER(cxxStaticCastExpr);
189   REGISTER_MATCHER(cxxStdInitializerListExpr);
190   REGISTER_MATCHER(cxxTemporaryObjectExpr);
191   REGISTER_MATCHER(cxxThisExpr);
192   REGISTER_MATCHER(cxxThrowExpr);
193   REGISTER_MATCHER(cxxTryStmt);
194   REGISTER_MATCHER(cxxUnresolvedConstructExpr);
195   REGISTER_MATCHER(decayedType);
196   REGISTER_MATCHER(decl);
197   REGISTER_MATCHER(declCountIs);
198   REGISTER_MATCHER(declRefExpr);
199   REGISTER_MATCHER(declStmt);
200   REGISTER_MATCHER(declaratorDecl);
201   REGISTER_MATCHER(decltypeType);
202   REGISTER_MATCHER(defaultStmt);
203   REGISTER_MATCHER(dependentSizedArrayType);
204   REGISTER_MATCHER(designatedInitExpr);
205   REGISTER_MATCHER(designatorCountIs);
206   REGISTER_MATCHER(doStmt);
207   REGISTER_MATCHER(eachOf);
208   REGISTER_MATCHER(elaboratedType);
209   REGISTER_MATCHER(enumConstantDecl);
210   REGISTER_MATCHER(enumDecl);
211   REGISTER_MATCHER(enumType);
212   REGISTER_MATCHER(equalsBoundNode);
213   REGISTER_MATCHER(equalsIntegralValue);
214   REGISTER_MATCHER(explicitCastExpr);
215   REGISTER_MATCHER(expr);
216   REGISTER_MATCHER(exprWithCleanups);
217   REGISTER_MATCHER(fieldDecl);
218   REGISTER_MATCHER(floatLiteral);
219   REGISTER_MATCHER(forEach);
220   REGISTER_MATCHER(forEachArgumentWithParam);
221   REGISTER_MATCHER(forEachConstructorInitializer);
222   REGISTER_MATCHER(forEachDescendant);
223   REGISTER_MATCHER(forEachOverridden);
224   REGISTER_MATCHER(forEachSwitchCase);
225   REGISTER_MATCHER(forField);
226   REGISTER_MATCHER(forFunction);
227   REGISTER_MATCHER(forStmt);
228   REGISTER_MATCHER(friendDecl);
229   REGISTER_MATCHER(functionDecl);
230   REGISTER_MATCHER(functionProtoType);
231   REGISTER_MATCHER(functionTemplateDecl);
232   REGISTER_MATCHER(functionType);
233   REGISTER_MATCHER(gnuNullExpr);
234   REGISTER_MATCHER(gotoStmt);
235   REGISTER_MATCHER(has);
236   REGISTER_MATCHER(hasAncestor);
237   REGISTER_MATCHER(hasAnyArgument);
238   REGISTER_MATCHER(hasAnyClause);
239   REGISTER_MATCHER(hasAnyConstructorInitializer);
240   REGISTER_MATCHER(hasAnyDeclaration);
241   REGISTER_MATCHER(hasAnyName);
242   REGISTER_MATCHER(hasAnyParameter);
243   REGISTER_MATCHER(hasAnySelector);
244   REGISTER_MATCHER(hasAnySubstatement);
245   REGISTER_MATCHER(hasAnyTemplateArgument);
246   REGISTER_MATCHER(hasAnyUsingShadowDecl);
247   REGISTER_MATCHER(hasArgument);
248   REGISTER_MATCHER(hasArgumentOfType);
249   REGISTER_MATCHER(hasArraySize);
250   REGISTER_MATCHER(hasAttr);
251   REGISTER_MATCHER(hasAutomaticStorageDuration);
252   REGISTER_MATCHER(hasBase);
253   REGISTER_MATCHER(hasBitWidth);
254   REGISTER_MATCHER(hasBody);
255   REGISTER_MATCHER(hasCanonicalType);
256   REGISTER_MATCHER(hasCaseConstant);
257   REGISTER_MATCHER(hasCastKind);
258   REGISTER_MATCHER(hasCondition);
259   REGISTER_MATCHER(hasConditionVariableStatement);
260   REGISTER_MATCHER(hasDecayedType);
261   REGISTER_MATCHER(hasDeclContext);
262   REGISTER_MATCHER(hasDeclaration);
263   REGISTER_MATCHER(hasDeducedType);
264   REGISTER_MATCHER(hasDefaultArgument);
265   REGISTER_MATCHER(hasDefinition);
266   REGISTER_MATCHER(hasDescendant);
267   REGISTER_MATCHER(hasDestinationType);
268   REGISTER_MATCHER(hasDynamicExceptionSpec);
269   REGISTER_MATCHER(hasEitherOperand);
270   REGISTER_MATCHER(hasElementType);
271   REGISTER_MATCHER(hasElse);
272   REGISTER_MATCHER(hasExplicitSpecifier);
273   REGISTER_MATCHER(hasExternalFormalLinkage);
274   REGISTER_MATCHER(hasFalseExpression);
275   REGISTER_MATCHER(hasGlobalStorage);
276   REGISTER_MATCHER(hasImplicitDestinationType);
277   REGISTER_MATCHER(hasInClassInitializer);
278   REGISTER_MATCHER(hasIncrement);
279   REGISTER_MATCHER(hasIndex);
280   REGISTER_MATCHER(hasInit);
281   REGISTER_MATCHER(hasInitializer);
282   REGISTER_MATCHER(hasKeywordSelector);
283   REGISTER_MATCHER(hasLHS);
284   REGISTER_MATCHER(hasLocalQualifiers);
285   REGISTER_MATCHER(hasLocalStorage);
286   REGISTER_MATCHER(hasLoopInit);
287   REGISTER_MATCHER(hasLoopVariable);
288   REGISTER_MATCHER(hasMethod);
289   REGISTER_MATCHER(hasName);
290   REGISTER_MATCHER(hasNullSelector);
291   REGISTER_MATCHER(hasObjectExpression);
292   REGISTER_MATCHER(hasOperatorName);
293   REGISTER_MATCHER(hasOverloadedOperatorName);
294   REGISTER_MATCHER(hasParameter);
295   REGISTER_MATCHER(hasParent);
296   REGISTER_MATCHER(hasQualifier);
297   REGISTER_MATCHER(hasRHS);
298   REGISTER_MATCHER(hasRangeInit);
299   REGISTER_MATCHER(hasReceiver);
300   REGISTER_MATCHER(hasReceiverType);
301   REGISTER_MATCHER(hasReplacementType);
302   REGISTER_MATCHER(hasReturnValue);
303   REGISTER_MATCHER(hasSelector);
304   REGISTER_MATCHER(hasSingleDecl);
305   REGISTER_MATCHER(hasSize);
306   REGISTER_MATCHER(hasSizeExpr);
307   REGISTER_MATCHER(hasSourceExpression);
308   REGISTER_MATCHER(hasSpecializedTemplate);
309   REGISTER_MATCHER(hasStaticStorageDuration);
310   REGISTER_MATCHER(hasStructuredBlock);
311   REGISTER_MATCHER(hasSyntacticForm);
312   REGISTER_MATCHER(hasTargetDecl);
313   REGISTER_MATCHER(hasTemplateArgument);
314   REGISTER_MATCHER(hasThen);
315   REGISTER_MATCHER(hasThreadStorageDuration);
316   REGISTER_MATCHER(hasTrailingReturn);
317   REGISTER_MATCHER(hasTrueExpression);
318   REGISTER_MATCHER(hasTypeLoc);
319   REGISTER_MATCHER(hasUnaryOperand);
320   REGISTER_MATCHER(hasUnarySelector);
321   REGISTER_MATCHER(hasUnderlyingDecl);
322   REGISTER_MATCHER(hasUnderlyingType);
323   REGISTER_MATCHER(hasUnqualifiedDesugaredType);
324   REGISTER_MATCHER(hasValueType);
325   REGISTER_MATCHER(ifStmt);
326   REGISTER_MATCHER(ignoringElidableConstructorCall);
327   REGISTER_MATCHER(ignoringImpCasts);
328   REGISTER_MATCHER(ignoringImplicit);
329   REGISTER_MATCHER(ignoringParenCasts);
330   REGISTER_MATCHER(ignoringParenImpCasts);
331   REGISTER_MATCHER(imaginaryLiteral);
332   REGISTER_MATCHER(implicitCastExpr);
333   REGISTER_MATCHER(implicitValueInitExpr);
334   REGISTER_MATCHER(incompleteArrayType);
335   REGISTER_MATCHER(indirectFieldDecl);
336   REGISTER_MATCHER(initListExpr);
337   REGISTER_MATCHER(injectedClassNameType);
338   REGISTER_MATCHER(innerType);
339   REGISTER_MATCHER(integerLiteral);
340   REGISTER_MATCHER(isAllowedToContainClauseKind);
341   REGISTER_MATCHER(isAnonymous);
342   REGISTER_MATCHER(isAnyCharacter);
343   REGISTER_MATCHER(isAnyPointer);
344   REGISTER_MATCHER(isArray);
345   REGISTER_MATCHER(isArrow);
346   REGISTER_MATCHER(isAssignmentOperator);
347   REGISTER_MATCHER(isBaseInitializer);
348   REGISTER_MATCHER(isBitField);
349   REGISTER_MATCHER(isCatchAll);
350   REGISTER_MATCHER(isClass);
351   REGISTER_MATCHER(isClassMessage);
352   REGISTER_MATCHER(isClassMethod);
353   REGISTER_MATCHER(isConst);
354   REGISTER_MATCHER(isConstQualified);
355   REGISTER_MATCHER(isConstexpr);
356   REGISTER_MATCHER(isCopyAssignmentOperator);
357   REGISTER_MATCHER(isCopyConstructor);
358   REGISTER_MATCHER(isDefaultConstructor);
359   REGISTER_MATCHER(isDefaulted);
360   REGISTER_MATCHER(isDefinition);
361   REGISTER_MATCHER(isDelegatingConstructor);
362   REGISTER_MATCHER(isDeleted);
363   REGISTER_MATCHER(isExceptionVariable);
364   REGISTER_MATCHER(isExpansionInFileMatching);
365   REGISTER_MATCHER(isExpansionInMainFile);
366   REGISTER_MATCHER(isExpansionInSystemHeader);
367   REGISTER_MATCHER(isExplicit);
368   REGISTER_MATCHER(isExplicitTemplateSpecialization);
369   REGISTER_MATCHER(isExpr);
370   REGISTER_MATCHER(isExternC);
371   REGISTER_MATCHER(isFinal);
372   REGISTER_MATCHER(isImplicit);
373   REGISTER_MATCHER(isInStdNamespace);
374   REGISTER_MATCHER(isInTemplateInstantiation);
375   REGISTER_MATCHER(isInline);
376   REGISTER_MATCHER(isInstanceMessage);
377   REGISTER_MATCHER(isInstanceMethod);
378   REGISTER_MATCHER(isInstantiated);
379   REGISTER_MATCHER(isInstantiationDependent);
380   REGISTER_MATCHER(isInteger);
381   REGISTER_MATCHER(isIntegral);
382   REGISTER_MATCHER(isLambda);
383   REGISTER_MATCHER(isListInitialization);
384   REGISTER_MATCHER(isMain);
385   REGISTER_MATCHER(isMemberInitializer);
386   REGISTER_MATCHER(isMoveAssignmentOperator);
387   REGISTER_MATCHER(isMoveConstructor);
388   REGISTER_MATCHER(isNoReturn);
389   REGISTER_MATCHER(isNoThrow);
390   REGISTER_MATCHER(isNoneKind);
391   REGISTER_MATCHER(isOMPStructuredBlock);
392   REGISTER_MATCHER(isOverride);
393   REGISTER_MATCHER(isPrivate);
394   REGISTER_MATCHER(isProtected);
395   REGISTER_MATCHER(isPublic);
396   REGISTER_MATCHER(isPure);
397   REGISTER_MATCHER(isScoped);
398   REGISTER_MATCHER(isSharedKind);
399   REGISTER_MATCHER(isSignedInteger);
400   REGISTER_MATCHER(isStandaloneDirective);
401   REGISTER_MATCHER(isStaticLocal);
402   REGISTER_MATCHER(isStaticStorageClass);
403   REGISTER_MATCHER(isStruct);
404   REGISTER_MATCHER(isTemplateInstantiation);
405   REGISTER_MATCHER(isTypeDependent);
406   REGISTER_MATCHER(isUnion);
407   REGISTER_MATCHER(isUnsignedInteger);
408   REGISTER_MATCHER(isUserProvided);
409   REGISTER_MATCHER(isValueDependent);
410   REGISTER_MATCHER(isVariadic);
411   REGISTER_MATCHER(isVirtual);
412   REGISTER_MATCHER(isVirtualAsWritten);
413   REGISTER_MATCHER(isVolatileQualified);
414   REGISTER_MATCHER(isWritten);
415   REGISTER_MATCHER(lValueReferenceType);
416   REGISTER_MATCHER(labelDecl);
417   REGISTER_MATCHER(labelStmt);
418   REGISTER_MATCHER(lambdaExpr);
419   REGISTER_MATCHER(linkageSpecDecl);
420   REGISTER_MATCHER(matchesName);
421   REGISTER_MATCHER(matchesSelector);
422   REGISTER_MATCHER(materializeTemporaryExpr);
423   REGISTER_MATCHER(member);
424   REGISTER_MATCHER(memberExpr);
425   REGISTER_MATCHER(memberPointerType);
426   REGISTER_MATCHER(namedDecl);
427   REGISTER_MATCHER(namesType);
428   REGISTER_MATCHER(namespaceAliasDecl);
429   REGISTER_MATCHER(namespaceDecl);
430   REGISTER_MATCHER(nestedNameSpecifier);
431   REGISTER_MATCHER(nestedNameSpecifierLoc);
432   REGISTER_MATCHER(nonTypeTemplateParmDecl);
433   REGISTER_MATCHER(nullPointerConstant);
434   REGISTER_MATCHER(nullStmt);
435   REGISTER_MATCHER(numSelectorArgs);
436   REGISTER_MATCHER(objcCatchStmt);
437   REGISTER_MATCHER(objcCategoryDecl);
438   REGISTER_MATCHER(objcCategoryImplDecl);
439   REGISTER_MATCHER(objcFinallyStmt);
440   REGISTER_MATCHER(objcImplementationDecl);
441   REGISTER_MATCHER(objcInterfaceDecl);
442   REGISTER_MATCHER(objcIvarDecl);
443   REGISTER_MATCHER(objcIvarRefExpr);
444   REGISTER_MATCHER(objcMessageExpr);
445   REGISTER_MATCHER(objcMethodDecl);
446   REGISTER_MATCHER(objcObjectPointerType);
447   REGISTER_MATCHER(objcPropertyDecl);
448   REGISTER_MATCHER(objcProtocolDecl);
449   REGISTER_MATCHER(objcThrowStmt);
450   REGISTER_MATCHER(objcTryStmt);
451   REGISTER_MATCHER(ofClass);
452   REGISTER_MATCHER(ompDefaultClause);
453   REGISTER_MATCHER(ompExecutableDirective);
454   REGISTER_MATCHER(on);
455   REGISTER_MATCHER(onImplicitObjectArgument);
456   REGISTER_MATCHER(opaqueValueExpr);
457   REGISTER_MATCHER(parameterCountIs);
458   REGISTER_MATCHER(parenExpr);
459   REGISTER_MATCHER(parenListExpr);
460   REGISTER_MATCHER(parenType);
461   REGISTER_MATCHER(parmVarDecl);
462   REGISTER_MATCHER(pointee);
463   REGISTER_MATCHER(pointerType);
464   REGISTER_MATCHER(predefinedExpr);
465   REGISTER_MATCHER(qualType);
466   REGISTER_MATCHER(rValueReferenceType);
467   REGISTER_MATCHER(realFloatingPointType);
468   REGISTER_MATCHER(recordDecl);
469   REGISTER_MATCHER(recordType);
470   REGISTER_MATCHER(referenceType);
471   REGISTER_MATCHER(refersToDeclaration);
472   REGISTER_MATCHER(refersToIntegralType);
473   REGISTER_MATCHER(refersToTemplate);
474   REGISTER_MATCHER(refersToType);
475   REGISTER_MATCHER(requiresZeroInitialization);
476   REGISTER_MATCHER(returnStmt);
477   REGISTER_MATCHER(returns);
478   REGISTER_MATCHER(sizeOfExpr);
479   REGISTER_MATCHER(specifiesNamespace);
480   REGISTER_MATCHER(specifiesType);
481   REGISTER_MATCHER(specifiesTypeLoc);
482   REGISTER_MATCHER(statementCountIs);
483   REGISTER_MATCHER(staticAssertDecl);
484   REGISTER_MATCHER(stmt);
485   REGISTER_MATCHER(stmtExpr);
486   REGISTER_MATCHER(stringLiteral);
487   REGISTER_MATCHER(substNonTypeTemplateParmExpr);
488   REGISTER_MATCHER(substTemplateTypeParmType);
489   REGISTER_MATCHER(switchCase);
490   REGISTER_MATCHER(switchStmt);
491   REGISTER_MATCHER(tagType);
492   REGISTER_MATCHER(templateArgument);
493   REGISTER_MATCHER(templateArgumentCountIs);
494   REGISTER_MATCHER(templateName);
495   REGISTER_MATCHER(templateSpecializationType);
496   REGISTER_MATCHER(templateTypeParmDecl);
497   REGISTER_MATCHER(templateTypeParmType);
498   REGISTER_MATCHER(throughUsingDecl);
499   REGISTER_MATCHER(to);
500   REGISTER_MATCHER(translationUnitDecl);
501   REGISTER_MATCHER(type);
502   REGISTER_MATCHER(typeAliasDecl);
503   REGISTER_MATCHER(typeAliasTemplateDecl);
504   REGISTER_MATCHER(typeLoc);
505   REGISTER_MATCHER(typedefDecl);
506   REGISTER_MATCHER(typedefNameDecl);
507   REGISTER_MATCHER(typedefType);
508   REGISTER_MATCHER(unaryExprOrTypeTraitExpr);
509   REGISTER_MATCHER(unaryOperator);
510   REGISTER_MATCHER(unaryTransformType);
511   REGISTER_MATCHER(unless);
512   REGISTER_MATCHER(unresolvedLookupExpr);
513   REGISTER_MATCHER(unresolvedMemberExpr);
514   REGISTER_MATCHER(unresolvedUsingTypenameDecl);
515   REGISTER_MATCHER(unresolvedUsingValueDecl);
516   REGISTER_MATCHER(userDefinedLiteral);
517   REGISTER_MATCHER(usesADL);
518   REGISTER_MATCHER(usingDecl);
519   REGISTER_MATCHER(usingDirectiveDecl);
520   REGISTER_MATCHER(valueDecl);
521   REGISTER_MATCHER(varDecl);
522   REGISTER_MATCHER(variableArrayType);
523   REGISTER_MATCHER(voidType);
524   REGISTER_MATCHER(whileStmt);
525   REGISTER_MATCHER(withInitializer);
526 }
527 
528 RegistryMaps::~RegistryMaps() = default;
529 
530 static llvm::ManagedStatic<RegistryMaps> RegistryData;
531 
532 // static
533 llvm::Optional<MatcherCtor> Registry::lookupMatcherCtor(StringRef MatcherName) {
534   auto it = RegistryData->constructors().find(MatcherName);
535   return it == RegistryData->constructors().end()
536              ? llvm::Optional<MatcherCtor>()
537              : it->second.get();
538 }
539 
540 static llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
541                                      const std::set<ASTNodeKind> &KS) {
542   unsigned Count = 0;
543   for (std::set<ASTNodeKind>::const_iterator I = KS.begin(), E = KS.end();
544        I != E; ++I) {
545     if (I != KS.begin())
546       OS << "|";
547     if (Count++ == 3) {
548       OS << "...";
549       break;
550     }
551     OS << *I;
552   }
553   return OS;
554 }
555 
556 std::vector<ArgKind> Registry::getAcceptedCompletionTypes(
557     ArrayRef<std::pair<MatcherCtor, unsigned>> Context) {
558   ASTNodeKind InitialTypes[] = {
559       ASTNodeKind::getFromNodeKind<Decl>(),
560       ASTNodeKind::getFromNodeKind<QualType>(),
561       ASTNodeKind::getFromNodeKind<Type>(),
562       ASTNodeKind::getFromNodeKind<Stmt>(),
563       ASTNodeKind::getFromNodeKind<NestedNameSpecifier>(),
564       ASTNodeKind::getFromNodeKind<NestedNameSpecifierLoc>(),
565       ASTNodeKind::getFromNodeKind<TypeLoc>()};
566 
567   // Starting with the above seed of acceptable top-level matcher types, compute
568   // the acceptable type set for the argument indicated by each context element.
569   std::set<ArgKind> TypeSet(std::begin(InitialTypes), std::end(InitialTypes));
570   for (const auto &CtxEntry : Context) {
571     MatcherCtor Ctor = CtxEntry.first;
572     unsigned ArgNumber = CtxEntry.second;
573     std::vector<ArgKind> NextTypeSet;
574     for (const ArgKind &Kind : TypeSet) {
575       if (Kind.getArgKind() == Kind.AK_Matcher &&
576           Ctor->isConvertibleTo(Kind.getMatcherKind()) &&
577           (Ctor->isVariadic() || ArgNumber < Ctor->getNumArgs()))
578         Ctor->getArgKinds(Kind.getMatcherKind(), ArgNumber, NextTypeSet);
579     }
580     TypeSet.clear();
581     TypeSet.insert(NextTypeSet.begin(), NextTypeSet.end());
582   }
583   return std::vector<ArgKind>(TypeSet.begin(), TypeSet.end());
584 }
585 
586 std::vector<MatcherCompletion>
587 Registry::getMatcherCompletions(ArrayRef<ArgKind> AcceptedTypes) {
588   std::vector<MatcherCompletion> Completions;
589 
590   // Search the registry for acceptable matchers.
591   for (const auto &M : RegistryData->constructors()) {
592     const MatcherDescriptor& Matcher = *M.getValue();
593     StringRef Name = M.getKey();
594 
595     std::set<ASTNodeKind> RetKinds;
596     unsigned NumArgs = Matcher.isVariadic() ? 1 : Matcher.getNumArgs();
597     bool IsPolymorphic = Matcher.isPolymorphic();
598     std::vector<std::vector<ArgKind>> ArgsKinds(NumArgs);
599     unsigned MaxSpecificity = 0;
600     for (const ArgKind& Kind : AcceptedTypes) {
601       if (Kind.getArgKind() != Kind.AK_Matcher)
602         continue;
603       unsigned Specificity;
604       ASTNodeKind LeastDerivedKind;
605       if (Matcher.isConvertibleTo(Kind.getMatcherKind(), &Specificity,
606                                   &LeastDerivedKind)) {
607         if (MaxSpecificity < Specificity)
608           MaxSpecificity = Specificity;
609         RetKinds.insert(LeastDerivedKind);
610         for (unsigned Arg = 0; Arg != NumArgs; ++Arg)
611           Matcher.getArgKinds(Kind.getMatcherKind(), Arg, ArgsKinds[Arg]);
612         if (IsPolymorphic)
613           break;
614       }
615     }
616 
617     if (!RetKinds.empty() && MaxSpecificity > 0) {
618       std::string Decl;
619       llvm::raw_string_ostream OS(Decl);
620 
621       if (IsPolymorphic) {
622         OS << "Matcher<T> " << Name << "(Matcher<T>";
623       } else {
624         OS << "Matcher<" << RetKinds << "> " << Name << "(";
625         for (const std::vector<ArgKind> &Arg : ArgsKinds) {
626           if (&Arg != &ArgsKinds[0])
627             OS << ", ";
628 
629           bool FirstArgKind = true;
630           std::set<ASTNodeKind> MatcherKinds;
631           // Two steps. First all non-matchers, then matchers only.
632           for (const ArgKind &AK : Arg) {
633             if (AK.getArgKind() == ArgKind::AK_Matcher) {
634               MatcherKinds.insert(AK.getMatcherKind());
635             } else {
636               if (!FirstArgKind) OS << "|";
637               FirstArgKind = false;
638               OS << AK.asString();
639             }
640           }
641           if (!MatcherKinds.empty()) {
642             if (!FirstArgKind) OS << "|";
643             OS << "Matcher<" << MatcherKinds << ">";
644           }
645         }
646       }
647       if (Matcher.isVariadic())
648         OS << "...";
649       OS << ")";
650 
651       std::string TypedText = Name;
652       TypedText += "(";
653       if (ArgsKinds.empty())
654         TypedText += ")";
655       else if (ArgsKinds[0][0].getArgKind() == ArgKind::AK_String)
656         TypedText += "\"";
657 
658       Completions.emplace_back(TypedText, OS.str(), MaxSpecificity);
659     }
660   }
661 
662   return Completions;
663 }
664 
665 VariantMatcher Registry::constructMatcher(MatcherCtor Ctor,
666                                           SourceRange NameRange,
667                                           ArrayRef<ParserValue> Args,
668                                           Diagnostics *Error) {
669   return Ctor->create(NameRange, Args, Error);
670 }
671 
672 VariantMatcher Registry::constructBoundMatcher(MatcherCtor Ctor,
673                                                SourceRange NameRange,
674                                                StringRef BindID,
675                                                ArrayRef<ParserValue> Args,
676                                                Diagnostics *Error) {
677   VariantMatcher Out = constructMatcher(Ctor, NameRange, Args, Error);
678   if (Out.isNull()) return Out;
679 
680   llvm::Optional<DynTypedMatcher> Result = Out.getSingleMatcher();
681   if (Result.hasValue()) {
682     llvm::Optional<DynTypedMatcher> Bound = Result->tryBind(BindID);
683     if (Bound.hasValue()) {
684       return VariantMatcher::SingleMatcher(*Bound);
685     }
686   }
687   Error->addError(NameRange, Error->ET_RegistryNotBindable);
688   return VariantMatcher();
689 }
690 
691 } // namespace dynamic
692 } // namespace ast_matchers
693 } // namespace clang
694