clang-tools 8.0.0

ExpectedTypes.h
Go to the documentation of this file.
1 //===--- ExpectedTypes.h - Simplified C++ types -----------------*- C++-*--===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 // A simplified model of C++ types that can be used to check whether they are
10 // convertible between each other for the purposes of code completion ranking
11 // without looking at the ASTs. Note that we don't aim to fully mimic the C++
12 // conversion rules, merely try to have a model that gives useful improvements
13 // to the code completion ranking.
14 //
15 // We define an encoding of AST types as opaque strings, which can be stored in
16 // the index. Similar types (such as `int` and `long`) are folded together,
17 // forming equivalence classes with the same encoding.
18 //===----------------------------------------------------------------------===//
19 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_EXPECTED_TYPES_H
20 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_EXPECTED_TYPES_H
21
22 #include "clang/AST/Type.h"
23 #include "llvm/ADT/StringRef.h"
24
25 namespace clang {
26 class CodeCompletionResult;
27
28 namespace clangd {
29 /// A representation of a type that can be computed based on clang AST and
30 /// compared for equality. The encoding is stable between different ASTs, this
31 /// allows the representation to be stored in the index and compared with types
32 /// coming from a different AST later.
33 /// OpaqueType is a strongly-typedefed std::string, you can get the underlying
34 /// string with raw().
35 class OpaqueType {
36 public:
37 /// Create a type from a code completion result.
38 static llvm::Optional<OpaqueType>
39 fromCompletionResult(ASTContext &Ctx, const CodeCompletionResult &R);
40 /// Construct an instance from a clang::QualType. This is usually a
41 /// PreferredType from a clang's completion context.
42 static llvm::Optional<OpaqueType> fromType(ASTContext &Ctx, QualType Type);
43
44 /// Get the raw byte representation of the type. You can only rely on the
45 /// types being equal iff their raw representation is the same. The particular
46 /// details of the used encoding might change over time and one should not
47 /// rely on it.
48 llvm::StringRef raw() const { return Data; }
49
50 friend bool operator==(const OpaqueType &L, const OpaqueType &R) {
51 return L.Data == R.Data;
52 }
53 friend bool operator!=(const OpaqueType &L, const OpaqueType &R) {
54 return !(L == R);
55 }
56
57 private:
58 static llvm::Optional<OpaqueType> encode(ASTContext &Ctx, QualType Type);
59 explicit OpaqueType(std::string Data);
60
61 std::string Data;
62 };
63 } // namespace clangd
64 } // namespace clang
65 #endif
clang::clangd::OpaqueType
A representation of a type that can be computed based on clang AST and compared for equality...
Definition: ExpectedTypes.h:35
clang::clangd::OpaqueType::fromType
static llvm::Optional< OpaqueType > fromType(ASTContext &Ctx, QualType Type)
Construct an instance from a clang::QualType.
Definition: ExpectedTypes.cpp:67
clang::clangd::OpaqueType::operator==
friend bool operator==(const OpaqueType &L, const OpaqueType &R)
Definition: ExpectedTypes.h:50
clang::clangd::OpaqueType::raw
llvm::StringRef raw() const
Get the raw byte representation of the type.
Definition: ExpectedTypes.h:48
Ctx
Context Ctx
Definition: TUScheduler.cpp:224
clang::clangd::OpaqueType::operator!=
friend bool operator!=(const OpaqueType &L, const OpaqueType &R)
Definition: ExpectedTypes.h:53
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ChangeNamespace.cpp:18
clang::clangd::OpaqueType::fromCompletionResult
static llvm::Optional< OpaqueType > fromCompletionResult(ASTContext &Ctx, const CodeCompletionResult &R)
Create a type from a code completion result.
Definition: ExpectedTypes.cpp:73