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"
22 static const Type *toEquivClass(ASTContext &
Ctx, QualType T) {
23 if (T.isNull() || T->isDependentType())
26 T = T.getCanonicalType().getNonReferenceType();
28 if (T->isBooleanType())
29 return Ctx.BoolTy.getTypePtr();
30 if (T->isIntegerType() && !T->isEnumeralType())
31 return Ctx.IntTy.getTypePtr();
32 if (T->isFloatingType() && !T->isComplexType())
33 return Ctx.FloatTy.getTypePtr();
37 return Ctx.getPointerType(QualType(T->getArrayElementTypeNoTypeQual(), 0))
41 return T.getTypePtr();
44 static llvm::Optional<QualType>
45 typeOfCompletion(
const CodeCompletionResult &R) {
46 const NamedDecl *D = R.Declaration;
48 if (
auto *Template = dyn_cast_or_null<TemplateDecl>(D))
49 D = Template->getTemplatedDecl();
50 auto *VD = dyn_cast_or_null<ValueDecl>(D);
53 auto T = VD->getType();
56 if (
auto FuncT = T->getAs<FunctionType>()) {
61 return FuncT->getReturnType();
67 llvm::Optional<OpaqueType> OpaqueType::encode(ASTContext &
Ctx, QualType T) {
70 const Type *C = toEquivClass(
Ctx, T);
73 llvm::SmallString<128> Encoded;
74 if (index::generateUSRForType(QualType(C, 0),
Ctx, Encoded))
76 return OpaqueType(std::string(Encoded.str()));
79 OpaqueType::OpaqueType(std::string Data) : Data(std::move(Data)) {}
81 llvm::Optional<OpaqueType> OpaqueType::fromType(ASTContext &
Ctx,
86 llvm::Optional<OpaqueType>
87 OpaqueType::fromCompletionResult(ASTContext &
Ctx,
88 const CodeCompletionResult &R) {
89 auto T = typeOfCompletion(R);
92 return encode(
Ctx, *T);