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