@@ -566,6 +566,51 @@ static void enhanceFromIndex(HoverInfo &Hover, const Decl *D,
566
566
Req, [&](const Symbol &S) { Hover.Documentation = S.Documentation ; });
567
567
}
568
568
569
+ // Populates Type, ReturnType, and Parameters for function-like decls.
570
+ static void fillFunctionTypeAndParams (HoverInfo &HI, const Decl *D,
571
+ const FunctionDecl *FD,
572
+ const PrintingPolicy &Policy) {
573
+ HI.Parameters .emplace ();
574
+ for (const ParmVarDecl *PVD : FD->parameters ()) {
575
+ HI.Parameters ->emplace_back ();
576
+ auto &P = HI.Parameters ->back ();
577
+ if (!PVD->getType ().isNull ()) {
578
+ P.Type .emplace ();
579
+ llvm::raw_string_ostream OS (*P.Type );
580
+ PVD->getType ().print (OS, Policy);
581
+ } else {
582
+ std::string Param;
583
+ llvm::raw_string_ostream OS (Param);
584
+ PVD->dump (OS);
585
+ OS.flush ();
586
+ elog (" Got param with null type: {0}" , Param);
587
+ }
588
+ if (!PVD->getName ().empty ())
589
+ P.Name = PVD->getNameAsString ();
590
+ if (PVD->hasDefaultArg ()) {
591
+ P.Default .emplace ();
592
+ llvm::raw_string_ostream Out (*P.Default );
593
+ PVD->getDefaultArg ()->printPretty (Out, nullptr , Policy);
594
+ }
595
+ }
596
+
597
+ if (const auto * CCD = llvm::dyn_cast<CXXConstructorDecl>(FD)) {
598
+ // Constructor's "return type" is the class type.
599
+ HI.ReturnType = declaredType (CCD->getParent ()).getAsString (Policy);
600
+ // Don't provide any type for the constructor itself.
601
+ } else if (const auto * CDD = llvm::dyn_cast<CXXDestructorDecl>(FD)){
602
+ HI.ReturnType = " void" ;
603
+ } else {
604
+ HI.ReturnType = FD->getReturnType ().getAsString (Policy);
605
+
606
+ QualType FunctionType = FD->getType ();
607
+ if (const VarDecl *VD = llvm::dyn_cast<VarDecl>(D)) // Lambdas
608
+ FunctionType = VD->getType ().getDesugaredType (D->getASTContext ());
609
+ HI.Type = FunctionType.getAsString (Policy);
610
+ }
611
+ // FIXME: handle variadics.
612
+ }
613
+
569
614
// / Generate a \p Hover object given the declaration \p D.
570
615
static HoverInfo getHoverContents (const Decl *D, const SymbolIndex *Index) {
571
616
HoverInfo HI;
@@ -601,45 +646,7 @@ static HoverInfo getHoverContents(const Decl *D, const SymbolIndex *Index) {
601
646
602
647
// Fill in types and params.
603
648
if (const FunctionDecl *FD = getUnderlyingFunction (D)) {
604
- HI.ReturnType .emplace ();
605
- {
606
- llvm::raw_string_ostream OS (*HI.ReturnType );
607
- FD->getReturnType ().print (OS, Policy);
608
- }
609
-
610
- HI.Parameters .emplace ();
611
- for (const ParmVarDecl *PVD : FD->parameters ()) {
612
- HI.Parameters ->emplace_back ();
613
- auto &P = HI.Parameters ->back ();
614
- if (!PVD->getType ().isNull ()) {
615
- P.Type .emplace ();
616
- llvm::raw_string_ostream OS (*P.Type );
617
- PVD->getType ().print (OS, Policy);
618
- } else {
619
- std::string Param;
620
- llvm::raw_string_ostream OS (Param);
621
- PVD->dump (OS);
622
- OS.flush ();
623
- elog (" Got param with null type: {0}" , Param);
624
- }
625
- if (!PVD->getName ().empty ())
626
- P.Name = PVD->getNameAsString ();
627
- if (PVD->hasDefaultArg ()) {
628
- P.Default .emplace ();
629
- llvm::raw_string_ostream Out (*P.Default );
630
- PVD->getDefaultArg ()->printPretty (Out, nullptr , Policy);
631
- }
632
- }
633
-
634
- HI.Type .emplace ();
635
- llvm::raw_string_ostream TypeOS (*HI.Type );
636
- // Lambdas
637
- if (const VarDecl *VD = llvm::dyn_cast<VarDecl>(D))
638
- VD->getType ().getDesugaredType (D->getASTContext ()).print (TypeOS, Policy);
639
- // Functions
640
- else
641
- FD->getType ().print (TypeOS, Policy);
642
- // FIXME: handle variadics.
649
+ fillFunctionTypeAndParams (HI, D, FD, Policy);
643
650
} else if (const auto *VD = dyn_cast<ValueDecl>(D)) {
644
651
HI.Type .emplace ();
645
652
llvm::raw_string_ostream OS (*HI.Type );
0 commit comments