1d360b298SIlya Biryukov //===--- ExpectedTypes.h - Simplified C++ types -----------------*- C++-*--===// 2d360b298SIlya Biryukov // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6d360b298SIlya Biryukov // 7d360b298SIlya Biryukov //===----------------------------------------------------------------------===// 8d360b298SIlya Biryukov // A simplified model of C++ types that can be used to check whether they are 9d360b298SIlya Biryukov // convertible between each other for the purposes of code completion ranking 10d360b298SIlya Biryukov // without looking at the ASTs. Note that we don't aim to fully mimic the C++ 11d360b298SIlya Biryukov // conversion rules, merely try to have a model that gives useful improvements 12d360b298SIlya Biryukov // to the code completion ranking. 13d360b298SIlya Biryukov // 14d360b298SIlya Biryukov // We define an encoding of AST types as opaque strings, which can be stored in 15d360b298SIlya Biryukov // the index. Similar types (such as `int` and `long`) are folded together, 16d360b298SIlya Biryukov // forming equivalence classes with the same encoding. 17d360b298SIlya Biryukov //===----------------------------------------------------------------------===// 185bd643d3SChristian Kühnel #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_EXPECTEDTYPES_H 195bd643d3SChristian Kühnel #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_EXPECTEDTYPES_H 20d360b298SIlya Biryukov 21d360b298SIlya Biryukov #include "clang/AST/Type.h" 22d360b298SIlya Biryukov #include "llvm/ADT/StringRef.h" 2371f55735SKazu Hirata #include <optional> 24d360b298SIlya Biryukov 25d360b298SIlya Biryukov namespace clang { 26d360b298SIlya Biryukov class CodeCompletionResult; 27d360b298SIlya Biryukov 28d360b298SIlya Biryukov namespace clangd { 29d360b298SIlya Biryukov /// A representation of a type that can be computed based on clang AST and 30d360b298SIlya Biryukov /// compared for equality. The encoding is stable between different ASTs, this 31d360b298SIlya Biryukov /// allows the representation to be stored in the index and compared with types 32d360b298SIlya Biryukov /// coming from a different AST later. 33d360b298SIlya Biryukov /// OpaqueType is a strongly-typedefed std::string, you can get the underlying 34d360b298SIlya Biryukov /// string with raw(). 35d360b298SIlya Biryukov class OpaqueType { 36d360b298SIlya Biryukov public: 37d360b298SIlya Biryukov /// Create a type from a code completion result. 38*f71ffd3bSKazu Hirata static std::optional<OpaqueType> 39d360b298SIlya Biryukov fromCompletionResult(ASTContext &Ctx, const CodeCompletionResult &R); 40d360b298SIlya Biryukov /// Construct an instance from a clang::QualType. This is usually a 41d360b298SIlya Biryukov /// PreferredType from a clang's completion context. 42*f71ffd3bSKazu Hirata static std::optional<OpaqueType> fromType(ASTContext &Ctx, QualType Type); 43d360b298SIlya Biryukov 44d360b298SIlya Biryukov /// Get the raw byte representation of the type. You can only rely on the 45d360b298SIlya Biryukov /// types being equal iff their raw representation is the same. The particular 46d360b298SIlya Biryukov /// details of the used encoding might change over time and one should not 47d360b298SIlya Biryukov /// rely on it. raw()48d360b298SIlya Biryukov llvm::StringRef raw() const { return Data; } 49d360b298SIlya Biryukov 50d360b298SIlya Biryukov friend bool operator==(const OpaqueType &L, const OpaqueType &R) { 51d360b298SIlya Biryukov return L.Data == R.Data; 52d360b298SIlya Biryukov } 53d360b298SIlya Biryukov friend bool operator!=(const OpaqueType &L, const OpaqueType &R) { 54d360b298SIlya Biryukov return !(L == R); 55d360b298SIlya Biryukov } 56d360b298SIlya Biryukov 57d360b298SIlya Biryukov private: 58*f71ffd3bSKazu Hirata static std::optional<OpaqueType> encode(ASTContext &Ctx, QualType Type); 59d360b298SIlya Biryukov explicit OpaqueType(std::string Data); 60d360b298SIlya Biryukov 61d360b298SIlya Biryukov std::string Data; 62d360b298SIlya Biryukov }; 63d360b298SIlya Biryukov } // namespace clangd 64d360b298SIlya Biryukov } // namespace clang 65d360b298SIlya Biryukov #endif 66