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 17 namespace clang { 18 namespace clangd { 19 namespace { 20 21 static const Type *toEquivClass(ASTContext &Ctx, QualType T) { 22 if (T.isNull() || T->isDependentType()) 23 return nullptr; 24 // Drop references, we do not handle reference inits properly anyway. 25 T = T.getCanonicalType().getNonReferenceType(); 26 // Numeric types are the simplest case. 27 if (T->isBooleanType()) 28 return Ctx.BoolTy.getTypePtr(); 29 if (T->isIntegerType() && !T->isEnumeralType()) 30 return Ctx.IntTy.getTypePtr(); // All integers are equivalent. 31 if (T->isFloatingType() && !T->isComplexType()) 32 return Ctx.FloatTy.getTypePtr(); // All floats are equivalent. 33 34 // Do some simple transformations. 35 if (T->isArrayType()) // Decay arrays to pointers. 36 return Ctx.getPointerType(QualType(T->getArrayElementTypeNoTypeQual(), 0)) 37 .getTypePtr(); 38 // Drop the qualifiers and return the resulting type. 39 // FIXME: also drop qualifiers from pointer types, e.g. 'const T* => T*' 40 return T.getTypePtr(); 41 } 42 43 static llvm::Optional<QualType> 44 typeOfCompletion(const CodeCompletionResult &R) { 45 const NamedDecl *D = R.Declaration; 46 // Templates do not have a type on their own, look at the templated decl. 47 if (auto *Template = dyn_cast_or_null<TemplateDecl>(D)) 48 D = Template->getTemplatedDecl(); 49 auto *VD = dyn_cast_or_null<ValueDecl>(D); 50 if (!VD) 51 return llvm::None; // We handle only variables and functions below. 52 auto T = VD->getType(); 53 if (T.isNull()) 54 return llvm::None; 55 if (auto *FuncT = T->getAs<FunctionType>()) { 56 // Functions are a special case. They are completed as 'foo()' and we want 57 // to match their return type rather than the function type itself. 58 // FIXME(ibiryukov): in some cases, we might want to avoid completing `()` 59 // after the function name, e.g. `std::cout << std::endl`. 60 return FuncT->getReturnType(); 61 } 62 return T; 63 } 64 } // namespace 65 66 llvm::Optional<OpaqueType> OpaqueType::encode(ASTContext &Ctx, QualType T) { 67 if (T.isNull()) 68 return None; 69 const Type *C = toEquivClass(Ctx, T); 70 if (!C) 71 return None; 72 llvm::SmallString<128> Encoded; 73 if (index::generateUSRForType(QualType(C, 0), Ctx, Encoded)) 74 return None; 75 return OpaqueType(std::string(Encoded.str())); 76 } 77 78 OpaqueType::OpaqueType(std::string Data) : Data(std::move(Data)) {} 79 80 llvm::Optional<OpaqueType> OpaqueType::fromType(ASTContext &Ctx, 81 QualType Type) { 82 return encode(Ctx, Type); 83 } 84 85 llvm::Optional<OpaqueType> 86 OpaqueType::fromCompletionResult(ASTContext &Ctx, 87 const CodeCompletionResult &R) { 88 auto T = typeOfCompletion(R); 89 if (!T) 90 return None; 91 return encode(Ctx, *T); 92 } 93 94 } // namespace clangd 95 } // namespace clang 96