Support named values in the autocomplete feature.

Summary:
This includes:
 - Passing a Sema to completeExpression to allow for named values in the
   expression.
 - Passing a map of names to values to the parser.
 - Update the Sema interface to include completion for matchers.
 - Change the parser to use the Sema for completion, instead of going
   directly to Registry.

Reviewers: pcc

Subscribers: klimek, cfe-commits

Differential Revision: http://reviews.llvm.org/D3509

llvm-svn: 215472
diff --git a/clang/lib/ASTMatchers/Dynamic/VariantValue.cpp b/clang/lib/ASTMatchers/Dynamic/VariantValue.cpp
index 18c98943..57c7b80 100644
--- a/clang/lib/ASTMatchers/Dynamic/VariantValue.cpp
+++ b/clang/lib/ASTMatchers/Dynamic/VariantValue.cpp
@@ -20,6 +20,35 @@
 namespace ast_matchers {
 namespace dynamic {
 
+std::string ArgKind::asString() const {
+  switch (getArgKind()) {
+  case AK_Matcher:
+    return (Twine("Matcher<") + MatcherKind.asStringRef() + ">").str();
+  case AK_Unsigned:
+    return "unsigned";
+  case AK_String:
+    return "string";
+  }
+  llvm_unreachable("unhandled ArgKind");
+}
+
+bool ArgKind::isConvertibleTo(ArgKind To, unsigned *Specificity) const {
+  if (K != To.K)
+    return false;
+  if (K != AK_Matcher) {
+    if (Specificity)
+      *Specificity = 1;
+    return true;
+  }
+  unsigned Distance;
+  if (!MatcherKind.isBaseOf(To.MatcherKind, &Distance))
+    return false;
+
+  if (Specificity)
+    *Specificity = 100 - Distance;
+  return true;
+}
+
 VariantMatcher::MatcherOps::~MatcherOps() {}
 VariantMatcher::Payload::~Payload() {}
 
@@ -27,21 +56,27 @@
 public:
   SinglePayload(const DynTypedMatcher &Matcher) : Matcher(Matcher) {}
 
-  virtual llvm::Optional<DynTypedMatcher> getSingleMatcher() const {
+  llvm::Optional<DynTypedMatcher> getSingleMatcher() const override {
     return Matcher;
   }
 
-  virtual std::string getTypeAsString() const {
+  std::string getTypeAsString() const override {
     return (Twine("Matcher<") + Matcher.getSupportedKind().asStringRef() + ">")
         .str();
   }
 
-  virtual void makeTypedMatcher(MatcherOps &Ops) const {
+  void makeTypedMatcher(MatcherOps &Ops) const override {
     bool Ignore;
     if (Ops.canConstructFrom(Matcher, Ignore))
       Ops.constructFrom(Matcher);
   }
 
+  bool isConvertibleTo(ast_type_traits::ASTNodeKind Kind,
+                       unsigned *Specificity) const override {
+    return ArgKind(Matcher.getSupportedKind())
+        .isConvertibleTo(Kind, Specificity);
+  }
+
 private:
   const DynTypedMatcher Matcher;
 };
@@ -51,15 +86,15 @@
   PolymorphicPayload(std::vector<DynTypedMatcher> MatchersIn)
       : Matchers(std::move(MatchersIn)) {}
 
-  virtual ~PolymorphicPayload() {}
+  ~PolymorphicPayload() override {}
 
-  virtual llvm::Optional<DynTypedMatcher> getSingleMatcher() const {
+  llvm::Optional<DynTypedMatcher> getSingleMatcher() const override {
     if (Matchers.size() != 1)
       return llvm::Optional<DynTypedMatcher>();
     return Matchers[0];
   }
 
-  virtual std::string getTypeAsString() const {
+  std::string getTypeAsString() const override {
     std::string Inner;
     for (size_t i = 0, e = Matchers.size(); i != e; ++i) {
       if (i != 0)
@@ -69,7 +104,7 @@
     return (Twine("Matcher<") + Inner + ">").str();
   }
 
-  virtual void makeTypedMatcher(MatcherOps &Ops) const {
+  void makeTypedMatcher(MatcherOps &Ops) const override {
     bool FoundIsExact = false;
     const DynTypedMatcher *Found = nullptr;
     int NumFound = 0;
@@ -92,6 +127,21 @@
       Ops.constructFrom(*Found);
   }
 
+  bool isConvertibleTo(ast_type_traits::ASTNodeKind Kind,
+                       unsigned *Specificity) const override {
+    unsigned MaxSpecificity = 0;
+    for (const DynTypedMatcher &Matcher : Matchers) {
+      unsigned ThisSpecificity;
+      if (ArgKind(Matcher.getSupportedKind())
+              .isConvertibleTo(Kind, &ThisSpecificity)) {
+        MaxSpecificity = std::max(MaxSpecificity, ThisSpecificity);
+      }
+    }
+    if (Specificity)
+      *Specificity = MaxSpecificity;
+    return MaxSpecificity > 0;
+  }
+
   const std::vector<DynTypedMatcher> Matchers;
 };
 
@@ -101,11 +151,11 @@
                     std::vector<VariantMatcher> Args)
       : Func(Func), Args(std::move(Args)) {}
 
-  virtual llvm::Optional<DynTypedMatcher> getSingleMatcher() const {
+  llvm::Optional<DynTypedMatcher> getSingleMatcher() const override {
     return llvm::Optional<DynTypedMatcher>();
   }
 
-  virtual std::string getTypeAsString() const {
+  std::string getTypeAsString() const override {
     std::string Inner;
     for (size_t i = 0, e = Args.size(); i != e; ++i) {
       if (i != 0)
@@ -115,10 +165,19 @@
     return Inner;
   }
 
-  virtual void makeTypedMatcher(MatcherOps &Ops) const {
+  void makeTypedMatcher(MatcherOps &Ops) const override {
     Ops.constructVariadicOperator(Func, Args);
   }
 
+  bool isConvertibleTo(ast_type_traits::ASTNodeKind Kind,
+                       unsigned *Specificity) const override {
+    for (const VariantMatcher &Matcher : Args) {
+      if (!Matcher.isConvertibleTo(Kind, Specificity))
+        return false;
+    }
+    return true;
+  }
+
 private:
   const ast_matchers::internal::VariadicOperatorFunction Func;
   const std::vector<VariantMatcher> Args;
@@ -251,6 +310,43 @@
   Value.Matcher = new VariantMatcher(NewValue);
 }
 
+bool VariantValue::isConvertibleTo(ArgKind Kind, unsigned *Specificity) const {
+  switch (Kind.getArgKind()) {
+  case ArgKind::AK_Unsigned:
+    if (!isUnsigned())
+      return false;
+    *Specificity = 1;
+    return true;
+
+  case ArgKind::AK_String:
+    if (!isString())
+      return false;
+    *Specificity = 1;
+    return true;
+
+  case ArgKind::AK_Matcher:
+    if (!isMatcher())
+      return false;
+    return getMatcher().isConvertibleTo(Kind.getMatcherKind(), Specificity);
+  }
+  llvm_unreachable("Invalid Type");
+}
+
+bool VariantValue::isConvertibleTo(ArrayRef<ArgKind> Kinds,
+                                   unsigned *Specificity) const {
+  unsigned MaxSpecificity = 0;
+  for (const ArgKind& Kind : Kinds) {
+    unsigned ThisSpecificity;
+    if (!isConvertibleTo(Kind, &ThisSpecificity))
+      continue;
+    MaxSpecificity = std::max(MaxSpecificity, ThisSpecificity);
+  }
+  if (Specificity && MaxSpecificity > 0) {
+    *Specificity = MaxSpecificity;
+  }
+  return MaxSpecificity > 0;
+}
+
 std::string VariantValue::getTypeAsString() const {
   switch (Type) {
   case VT_String: return "String";