clang-tools  9.0.0
ExpectedTypes.cpp
Go to the documentation of this file.
1 //===--- ExpectedTypes.cpp ---------------------------------------*- C++-*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "ExpectedTypes.h"
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"
17 
18 namespace clang {
19 namespace clangd {
20 namespace {
21 
22 static const Type *toEquivClass(ASTContext &Ctx, QualType T) {
23  if (T.isNull() || T->isDependentType())
24  return nullptr;
25  // Drop references, we do not handle reference inits properly anyway.
26  T = T.getCanonicalType().getNonReferenceType();
27  // Numeric types are the simplest case.
28  if (T->isBooleanType())
29  return Ctx.BoolTy.getTypePtr();
30  if (T->isIntegerType() && !T->isEnumeralType())
31  return Ctx.IntTy.getTypePtr(); // All integers are equivalent.
32  if (T->isFloatingType() && !T->isComplexType())
33  return Ctx.FloatTy.getTypePtr(); // All floats are equivalent.
34 
35  // Do some simple transformations.
36  if (T->isArrayType()) // Decay arrays to pointers.
37  return Ctx.getPointerType(QualType(T->getArrayElementTypeNoTypeQual(), 0))
38  .getTypePtr();
39  // Drop the qualifiers and return the resulting type.
40  // FIXME: also drop qualifiers from pointer types, e.g. 'const T* => T*'
41  return T.getTypePtr();
42 }
43 
44 static llvm::Optional<QualType>
45 typeOfCompletion(const CodeCompletionResult &R) {
46  const NamedDecl *D = R.Declaration;
47  if (!D)
48  return llvm::None;
49  // Templates do not have a type on their own, look at the templated decl.
50  if (auto *Template = dyn_cast<TemplateDecl>(D))
51  D = Template->getTemplatedDecl();
52  auto *VD = dyn_cast<ValueDecl>(D);
53  if (!VD)
54  return llvm::None; // We handle only variables and functions below.
55  auto T = VD->getType();
56  if (T.isNull())
57  return llvm::None;
58  if (auto FuncT = T->getAs<FunctionType>()) {
59  // Functions are a special case. They are completed as 'foo()' and we want
60  // to match their return type rather than the function type itself.
61  // FIXME(ibiryukov): in some cases, we might want to avoid completing `()`
62  // after the function name, e.g. `std::cout << std::endl`.
63  return FuncT->getReturnType();
64  }
65  return T;
66 }
67 } // namespace
68 
69 llvm::Optional<OpaqueType> OpaqueType::encode(ASTContext &Ctx, QualType T) {
70  if (T.isNull())
71  return None;
72  const Type *C = toEquivClass(Ctx, T);
73  if (!C)
74  return None;
75  llvm::SmallString<128> Encoded;
76  if (index::generateUSRForType(QualType(C, 0), Ctx, Encoded))
77  return None;
78  return OpaqueType(Encoded.str());
79 }
80 
81 OpaqueType::OpaqueType(std::string Data) : Data(std::move(Data)) {}
82 
83 llvm::Optional<OpaqueType> OpaqueType::fromType(ASTContext &Ctx,
84  QualType Type) {
85  return encode(Ctx, Type);
86 }
87 
88 llvm::Optional<OpaqueType>
89 OpaqueType::fromCompletionResult(ASTContext &Ctx,
90  const CodeCompletionResult &R) {
91  auto T = typeOfCompletion(R);
92  if (!T)
93  return None;
94  return encode(Ctx, *T);
95 }
96 
97 } // namespace clangd
98 } // namespace clang
Documents should not be synced at all.
Context Ctx
const Decl * D
Definition: XRefs.cpp:868
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
NodeType Type