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"
21 static const Type *toEquivClass(ASTContext &
Ctx, QualType T) {
22 if (T.isNull() || T->isDependentType())
25 T = T.getCanonicalType().getNonReferenceType();
27 if (T->isBooleanType())
28 return Ctx.BoolTy.getTypePtr();
29 if (T->isIntegerType() && !T->isEnumeralType())
30 return Ctx.IntTy.getTypePtr();
31 if (T->isFloatingType() && !T->isComplexType())
32 return Ctx.FloatTy.getTypePtr();
36 return Ctx.getPointerType(QualType(T->getArrayElementTypeNoTypeQual(), 0))
40 return T.getTypePtr();
43 static llvm::Optional<QualType>
44 typeOfCompletion(
const CodeCompletionResult &R) {
45 const NamedDecl *
D = R.Declaration;
47 if (
auto *Template = dyn_cast_or_null<TemplateDecl>(
D))
48 D = Template->getTemplatedDecl();
49 auto *VD = dyn_cast_or_null<ValueDecl>(
D);
52 auto T = VD->getType();
55 if (
auto *FuncT = T->getAs<FunctionType>()) {
60 return FuncT->getReturnType();
66 llvm::Optional<OpaqueType> OpaqueType::encode(ASTContext &
Ctx, QualType T) {
69 const Type *
C = toEquivClass(
Ctx, T);
72 llvm::SmallString<128> Encoded;
73 if (index::generateUSRForType(QualType(
C, 0),
Ctx, Encoded))
75 return OpaqueType(std::string(Encoded.str()));
78 OpaqueType::OpaqueType(std::string Data) : Data(std::move(Data)) {}
80 llvm::Optional<OpaqueType> OpaqueType::fromType(ASTContext &
Ctx,
85 llvm::Optional<OpaqueType>
86 OpaqueType::fromCompletionResult(ASTContext &
Ctx,
87 const CodeCompletionResult &R) {
88 auto T = typeOfCompletion(R);
91 return encode(
Ctx, *T);