summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clangd/ExpectedTypes.h
diff options
context:
space:
mode:
authorIlya Biryukov <ibiryukov@google.com>2018-11-26 15:25:20 +0000
committerIlya Biryukov <ibiryukov@google.com>2018-11-26 15:25:20 +0000
commitd360b2984e854bf7414239f66e81427cf0e25f1e (patch)
tree70ce372a5cd1208b83ccaf74a2ddac197628a758 /clang-tools-extra/clangd/ExpectedTypes.h
parenta622484fa64e03a45832a95cda1ba61d512244a0 (diff)
downloadbcm5719-llvm-d360b2984e854bf7414239f66e81427cf0e25f1e.tar.gz
bcm5719-llvm-d360b2984e854bf7414239f66e81427cf0e25f1e.zip
[clangd] Initial implementation of expected types
Summary: Provides facilities to model the C++ conversion rules without the AST. The introduced representation can be stored in the index and used to implement type-based ranking improvements for index-based completions. Reviewers: sammccall, ioeric Reviewed By: sammccall Subscribers: malaperle, mgorny, MaskRay, jkorous, arphaman, kadircet, cfe-commits Differential Revision: https://reviews.llvm.org/D52273 llvm-svn: 347559
Diffstat (limited to 'clang-tools-extra/clangd/ExpectedTypes.h')
-rw-r--r--clang-tools-extra/clangd/ExpectedTypes.h65
1 files changed, 65 insertions, 0 deletions
diff --git a/clang-tools-extra/clangd/ExpectedTypes.h b/clang-tools-extra/clangd/ExpectedTypes.h
new file mode 100644
index 00000000000..2f231283d7d
--- /dev/null
+++ b/clang-tools-extra/clangd/ExpectedTypes.h
@@ -0,0 +1,65 @@
+//===--- ExpectedTypes.h - Simplified C++ types -----------------*- C++-*--===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+// A simplified model of C++ types that can be used to check whether they are
+// convertible between each other for the purposes of code completion ranking
+// without looking at the ASTs. Note that we don't aim to fully mimic the C++
+// conversion rules, merely try to have a model that gives useful improvements
+// to the code completion ranking.
+//
+// We define an encoding of AST types as opaque strings, which can be stored in
+// the index. Similar types (such as `int` and `long`) are folded together,
+// forming equivalence classes with the same encoding.
+//===----------------------------------------------------------------------===//
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_EXPECTED_TYPES_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_EXPECTED_TYPES_H
+
+#include "clang/AST/Type.h"
+#include "llvm/ADT/StringRef.h"
+
+namespace clang {
+class CodeCompletionResult;
+
+namespace clangd {
+/// A representation of a type that can be computed based on clang AST and
+/// compared for equality. The encoding is stable between different ASTs, this
+/// allows the representation to be stored in the index and compared with types
+/// coming from a different AST later.
+/// OpaqueType is a strongly-typedefed std::string, you can get the underlying
+/// string with raw().
+class OpaqueType {
+public:
+ /// Create a type from a code completion result.
+ static llvm::Optional<OpaqueType>
+ fromCompletionResult(ASTContext &Ctx, const CodeCompletionResult &R);
+ /// Construct an instance from a clang::QualType. This is usually a
+ /// PreferredType from a clang's completion context.
+ static llvm::Optional<OpaqueType> fromType(ASTContext &Ctx, QualType Type);
+
+ /// Get the raw byte representation of the type. You can only rely on the
+ /// types being equal iff their raw representation is the same. The particular
+ /// details of the used encoding might change over time and one should not
+ /// rely on it.
+ llvm::StringRef raw() const { return Data; }
+
+ friend bool operator==(const OpaqueType &L, const OpaqueType &R) {
+ return L.Data == R.Data;
+ }
+ friend bool operator!=(const OpaqueType &L, const OpaqueType &R) {
+ return !(L == R);
+ }
+
+private:
+ static llvm::Optional<OpaqueType> encode(ASTContext &Ctx, QualType Type);
+ explicit OpaqueType(std::string Data);
+
+ std::string Data;
+};
+} // namespace clangd
+} // namespace clang
+#endif
OpenPOWER on IntegriCloud