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"
21static 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();
43static std::optional<QualType> typeOfCompletion(
const CodeCompletionResult &R) {
44 const NamedDecl *D = R.Declaration;
46 if (
auto *Template = dyn_cast_or_null<TemplateDecl>(D))
47 D = Template->getTemplatedDecl();
48 auto *VD = dyn_cast_or_null<ValueDecl>(D);
51 auto T = VD->getType();
54 if (
auto *FuncT =
T->getAs<FunctionType>()) {
59 return FuncT->getReturnType();
65std::optional<OpaqueType> OpaqueType::encode(ASTContext &Ctx, QualType T) {
68 const Type *
C = toEquivClass(Ctx, T);
71 llvm::SmallString<128> Encoded;
72 if (index::generateUSRForType(QualType(
C, 0), Ctx, Encoded))
74 return OpaqueType(std::string(Encoded));
77OpaqueType::OpaqueType(std::string Data) : Data(std::move(Data)) {}
79std::optional<OpaqueType> OpaqueType::fromType(ASTContext &Ctx, QualType
Type) {
80 return encode(Ctx,
Type);
83std::optional<OpaqueType>
84OpaqueType::fromCompletionResult(ASTContext &Ctx,
85 const CodeCompletionResult &R) {
86 auto T = typeOfCompletion(R);
89 return encode(Ctx, *T);
@ Type
An inlay hint that for a type annotation.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//