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