2019-02-28 22:01:11 +08:00
|
|
|
//===--- ExpectedTypes.cpp ---------------------------------------*- C++-*-===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-11-26 23:25:20 +08:00
|
|
|
#include "ExpectedTypes.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
2019-05-28 21:56:21 +08:00
|
|
|
#include "clang/AST/DeclTemplate.h"
|
2018-11-26 23:25:20 +08:00
|
|
|
#include "clang/AST/Type.h"
|
|
|
|
#include "clang/Index/USRGeneration.h"
|
|
|
|
#include "clang/Sema/CodeCompleteConsumer.h"
|
2019-05-28 21:56:21 +08:00
|
|
|
#include "llvm/ADT/None.h"
|
2018-11-26 23:25:20 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
static const Type *toEquivClass(ASTContext &Ctx, QualType T) {
|
|
|
|
if (T.isNull() || T->isDependentType())
|
|
|
|
return nullptr;
|
|
|
|
// Drop references, we do not handle reference inits properly anyway.
|
|
|
|
T = T.getCanonicalType().getNonReferenceType();
|
|
|
|
// Numeric types are the simplest case.
|
|
|
|
if (T->isBooleanType())
|
|
|
|
return Ctx.BoolTy.getTypePtr();
|
|
|
|
if (T->isIntegerType() && !T->isEnumeralType())
|
|
|
|
return Ctx.IntTy.getTypePtr(); // All integers are equivalent.
|
|
|
|
if (T->isFloatingType() && !T->isComplexType())
|
|
|
|
return Ctx.FloatTy.getTypePtr(); // All floats are equivalent.
|
|
|
|
|
|
|
|
// Do some simple transformations.
|
|
|
|
if (T->isArrayType()) // Decay arrays to pointers.
|
|
|
|
return Ctx.getPointerType(QualType(T->getArrayElementTypeNoTypeQual(), 0))
|
|
|
|
.getTypePtr();
|
|
|
|
// Drop the qualifiers and return the resulting type.
|
|
|
|
// FIXME: also drop qualifiers from pointer types, e.g. 'const T* => T*'
|
|
|
|
return T.getTypePtr();
|
|
|
|
}
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
static llvm::Optional<QualType>
|
|
|
|
typeOfCompletion(const CodeCompletionResult &R) {
|
2019-05-28 21:56:21 +08:00
|
|
|
const NamedDecl *D = R.Declaration;
|
|
|
|
// Templates do not have a type on their own, look at the templated decl.
|
2019-11-20 22:51:18 +08:00
|
|
|
if (auto *Template = dyn_cast_or_null<TemplateDecl>(D))
|
2019-05-28 21:56:21 +08:00
|
|
|
D = Template->getTemplatedDecl();
|
2019-11-20 22:51:18 +08:00
|
|
|
auto *VD = dyn_cast_or_null<ValueDecl>(D);
|
2018-11-26 23:25:20 +08:00
|
|
|
if (!VD)
|
2019-01-24 18:41:43 +08:00
|
|
|
return llvm::None; // We handle only variables and functions below.
|
2018-11-26 23:25:20 +08:00
|
|
|
auto T = VD->getType();
|
2019-01-24 18:41:43 +08:00
|
|
|
if (T.isNull())
|
|
|
|
return llvm::None;
|
2018-11-26 23:25:20 +08:00
|
|
|
if (auto FuncT = T->getAs<FunctionType>()) {
|
|
|
|
// Functions are a special case. They are completed as 'foo()' and we want
|
|
|
|
// to match their return type rather than the function type itself.
|
|
|
|
// FIXME(ibiryukov): in some cases, we might want to avoid completing `()`
|
|
|
|
// after the function name, e.g. `std::cout << std::endl`.
|
|
|
|
return FuncT->getReturnType();
|
|
|
|
}
|
|
|
|
return T;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::Optional<OpaqueType> OpaqueType::encode(ASTContext &Ctx, QualType T) {
|
2018-11-26 23:25:20 +08:00
|
|
|
if (T.isNull())
|
|
|
|
return None;
|
|
|
|
const Type *C = toEquivClass(Ctx, T);
|
|
|
|
if (!C)
|
|
|
|
return None;
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::SmallString<128> Encoded;
|
2018-11-26 23:25:20 +08:00
|
|
|
if (index::generateUSRForType(QualType(C, 0), Ctx, Encoded))
|
|
|
|
return None;
|
2020-01-29 03:23:46 +08:00
|
|
|
return OpaqueType(std::string(Encoded.str()));
|
2018-11-26 23:25:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
OpaqueType::OpaqueType(std::string Data) : Data(std::move(Data)) {}
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::Optional<OpaqueType> OpaqueType::fromType(ASTContext &Ctx,
|
|
|
|
QualType Type) {
|
2018-11-26 23:25:20 +08:00
|
|
|
return encode(Ctx, Type);
|
|
|
|
}
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::Optional<OpaqueType>
|
2018-11-26 23:25:20 +08:00
|
|
|
OpaqueType::fromCompletionResult(ASTContext &Ctx,
|
|
|
|
const CodeCompletionResult &R) {
|
|
|
|
auto T = typeOfCompletion(R);
|
|
|
|
if (!T)
|
|
|
|
return None;
|
|
|
|
return encode(Ctx, *T);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|