workflows: Fix typo from 2d3d0f50ceb938c155a7283e684f28190d24d6ba
[llvm-project.git] / clang-tools-extra / clangd / ExpectedTypes.h
blob873b106b91d176800f95e576958b66e8a76f7c65
1 //===--- ExpectedTypes.h - Simplified C++ types -----------------*- C++-*--===//
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 // A simplified model of C++ types that can be used to check whether they are
9 // convertible between each other for the purposes of code completion ranking
10 // without looking at the ASTs. Note that we don't aim to fully mimic the C++
11 // conversion rules, merely try to have a model that gives useful improvements
12 // to the code completion ranking.
14 // We define an encoding of AST types as opaque strings, which can be stored in
15 // the index. Similar types (such as `int` and `long`) are folded together,
16 // forming equivalence classes with the same encoding.
17 //===----------------------------------------------------------------------===//
18 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_EXPECTEDTYPES_H
19 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_EXPECTEDTYPES_H
21 #include "clang/AST/Type.h"
22 #include "llvm/ADT/StringRef.h"
24 namespace clang {
25 class CodeCompletionResult;
27 namespace clangd {
28 /// A representation of a type that can be computed based on clang AST and
29 /// compared for equality. The encoding is stable between different ASTs, this
30 /// allows the representation to be stored in the index and compared with types
31 /// coming from a different AST later.
32 /// OpaqueType is a strongly-typedefed std::string, you can get the underlying
33 /// string with raw().
34 class OpaqueType {
35 public:
36 /// Create a type from a code completion result.
37 static llvm::Optional<OpaqueType>
38 fromCompletionResult(ASTContext &Ctx, const CodeCompletionResult &R);
39 /// Construct an instance from a clang::QualType. This is usually a
40 /// PreferredType from a clang's completion context.
41 static llvm::Optional<OpaqueType> fromType(ASTContext &Ctx, QualType Type);
43 /// Get the raw byte representation of the type. You can only rely on the
44 /// types being equal iff their raw representation is the same. The particular
45 /// details of the used encoding might change over time and one should not
46 /// rely on it.
47 llvm::StringRef raw() const { return Data; }
49 friend bool operator==(const OpaqueType &L, const OpaqueType &R) {
50 return L.Data == R.Data;
52 friend bool operator!=(const OpaqueType &L, const OpaqueType &R) {
53 return !(L == R);
56 private:
57 static llvm::Optional<OpaqueType> encode(ASTContext &Ctx, QualType Type);
58 explicit OpaqueType(std::string Data);
60 std::string Data;
62 } // namespace clangd
63 } // namespace clang
64 #endif