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 None; // We handle only variables and functions below.
39   auto T = VD->getType();
40   if (auto FuncT = T->getAs<FunctionType>()) {
41     // Functions are a special case. They are completed as 'foo()' and we want
42     // to match their return type rather than the function type itself.
43     // FIXME(ibiryukov): in some cases, we might want to avoid completing `()`
44     // after the function name, e.g. `std::cout << std::endl`.
45     return FuncT->getReturnType();
46   }
47   return T;
48 }
49 } // namespace
50 
51 llvm::Optional<OpaqueType> OpaqueType::encode(ASTContext &Ctx, QualType T) {
52   if (T.isNull())
53     return None;
54   const Type *C = toEquivClass(Ctx, T);
55   if (!C)
56     return None;
57   llvm::SmallString<128> Encoded;
58   if (index::generateUSRForType(QualType(C, 0), Ctx, Encoded))
59     return None;
60   return OpaqueType(Encoded.str());
61 }
62 
63 OpaqueType::OpaqueType(std::string Data) : Data(std::move(Data)) {}
64 
65 llvm::Optional<OpaqueType> OpaqueType::fromType(ASTContext &Ctx,
66                                                 QualType Type) {
67   return encode(Ctx, Type);
68 }
69 
70 llvm::Optional<OpaqueType>
71 OpaqueType::fromCompletionResult(ASTContext &Ctx,
72                                  const CodeCompletionResult &R) {
73   auto T = typeOfCompletion(R);
74   if (!T)
75     return None;
76   return encode(Ctx, *T);
77 }
78 
79 } // namespace clangd
80 } // namespace clang
81