clang-tools 8.0.0

ExpectedTypes.cpp
Go to the documentation of this file.
1 #include "ExpectedTypes.h"
2 #include "clang/AST/ASTContext.h"
3 #include "clang/AST/Type.h"
4 #include "clang/Index/USRGeneration.h"
5 #include "clang/Sema/CodeCompleteConsumer.h"
6 #include "llvm/ADT/STLExtras.h"
7
8 namespace clang {
9 namespace clangd {
10 namespace {
11
12 static const Type *toEquivClass(ASTContext &Ctx, QualType T) {
13 if (T.isNull() || T->isDependentType())
14 return nullptr;
15 // Drop references, we do not handle reference inits properly anyway.
16 T = T.getCanonicalType().getNonReferenceType();
17 // Numeric types are the simplest case.
18 if (T->isBooleanType())
19 return Ctx.BoolTy.getTypePtr();
20 if (T->isIntegerType() && !T->isEnumeralType())
21 return Ctx.IntTy.getTypePtr(); // All integers are equivalent.
22 if (T->isFloatingType() && !T->isComplexType())
23 return Ctx.FloatTy.getTypePtr(); // All floats are equivalent.
24
25 // Do some simple transformations.
26 if (T->isArrayType()) // Decay arrays to pointers.
27 return Ctx.getPointerType(QualType(T->getArrayElementTypeNoTypeQual(), 0))
28 .getTypePtr();
29 // Drop the qualifiers and return the resulting type.
30 // FIXME: also drop qualifiers from pointer types, e.g. 'const T* => T*'
31 return T.getTypePtr();
32 }
33
34 static llvm::Optional<QualType>
35 typeOfCompletion(const CodeCompletionResult &R) {
36 auto *VD = dyn_cast_or_null<ValueDecl>(R.Declaration);
37 if (!VD)
38 return llvm::None; // We handle only variables and functions below.
39 auto T = VD->getType();
40 if (T.isNull())
41 return llvm::None;
42 if (auto FuncT = T->getAs<FunctionType>()) {
43 // Functions are a special case. They are completed as 'foo()' and we want
44 // to match their return type rather than the function type itself.
45 // FIXME(ibiryukov): in some cases, we might want to avoid completing `()`
46 // after the function name, e.g. `std::cout << std::endl`.
47 return FuncT->getReturnType();
48 }
49 return T;
50 }
51 } // namespace
52
53 llvm::Optional<OpaqueType> OpaqueType::encode(ASTContext &Ctx, QualType T) {
54 if (T.isNull())
55 return None;
56 const Type *C = toEquivClass(Ctx, T);
57 if (!C)
58 return None;
59 llvm::SmallString<128> Encoded;
60 if (index::generateUSRForType(QualType(C, 0), Ctx, Encoded))
61 return None;
62 return OpaqueType(Encoded.str());
63 }
64
65 OpaqueType::OpaqueType(std::string Data) : Data(std::move(Data)) {}
66
67 llvm::Optional<OpaqueType> OpaqueType::fromType(ASTContext &Ctx,
68 QualType Type) {
69 return encode(Ctx, Type);
70 }
71
72 llvm::Optional<OpaqueType>
73 OpaqueType::fromCompletionResult(ASTContext &Ctx,
74 const CodeCompletionResult &R) {
75 auto T = typeOfCompletion(R);
76 if (!T)
77 return None;
78 return encode(Ctx, *T);
79 }
80
81 } // namespace clangd
82 } // namespace clang
clang::clangd::TextDocumentSyncKind::None
Documents should not be synced at all.
Ctx
Context Ctx
Definition: TUScheduler.cpp:224
ExpectedTypes.h
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ChangeNamespace.cpp:18