blob: c78ab5b3e959e86d050a7feb01e947d753cfb506 [file] [log] [blame]
Douglas Gregor86d9a522009-09-21 16:56:56 +00001//===--- CodeCompleteConsumer.cpp - Code Completion Interface ---*- C++ -*-===//
Douglas Gregor81b747b2009-09-17 21:32:03 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the CodeCompleteConsumer class.
11//
12//===----------------------------------------------------------------------===//
13#include "clang/Sema/CodeCompleteConsumer.h"
Douglas Gregor75b71282009-09-18 17:54:00 +000014#include "clang/AST/DeclCXX.h"
Douglas Gregor374929f2009-09-18 15:37:17 +000015#include "clang/Parse/Scope.h"
Douglas Gregor81b747b2009-09-17 21:32:03 +000016#include "clang/Lex/Preprocessor.h"
17#include "Sema.h"
18#include "llvm/ADT/STLExtras.h"
19#include "llvm/Support/Compiler.h"
20#include "llvm/Support/raw_ostream.h"
21#include <algorithm>
Douglas Gregore6e03612009-09-18 22:15:54 +000022#include <cstring>
23#include <functional>
Douglas Gregor81b747b2009-09-17 21:32:03 +000024using namespace clang;
25
Douglas Gregore6e03612009-09-18 22:15:54 +000026//===----------------------------------------------------------------------===//
27// Code completion string implementation
28//===----------------------------------------------------------------------===//
Douglas Gregor0563c262009-09-22 23:15:58 +000029CodeCompletionString::Chunk::Chunk(ChunkKind Kind, const char *Text)
30 : Kind(Kind), Text(0)
31{
32 assert((Kind == CK_Text || Kind == CK_Placeholder || Kind == CK_Informative)
33 && "Invalid text chunk kind");
Douglas Gregore6e03612009-09-18 22:15:54 +000034 char *New = new char [std::strlen(Text) + 1];
35 std::strcpy(New, Text);
Douglas Gregor0563c262009-09-22 23:15:58 +000036 this->Text = New;
37}
38
39CodeCompletionString::Chunk
40CodeCompletionString::Chunk::CreateText(const char *Text) {
41 return Chunk(CK_Text, Text);
Douglas Gregore6e03612009-09-18 22:15:54 +000042}
43
44CodeCompletionString::Chunk
45CodeCompletionString::Chunk::CreateOptional(
46 std::auto_ptr<CodeCompletionString> Optional) {
47 Chunk Result;
48 Result.Kind = CK_Optional;
49 Result.Optional = Optional.release();
50 return Result;
51}
52
53CodeCompletionString::Chunk
54CodeCompletionString::Chunk::CreatePlaceholder(const char *Placeholder) {
Douglas Gregor0563c262009-09-22 23:15:58 +000055 return Chunk(CK_Placeholder, Placeholder);
56}
57
58CodeCompletionString::Chunk
59CodeCompletionString::Chunk::CreateInformative(const char *Informative) {
60 return Chunk(CK_Informative, Informative);
Douglas Gregore6e03612009-09-18 22:15:54 +000061}
62
63void
64CodeCompletionString::Chunk::Destroy() {
65 switch (Kind) {
Douglas Gregor0563c262009-09-22 23:15:58 +000066 case CK_Optional:
67 delete Optional;
68 break;
69
70 case CK_Text:
71 case CK_Placeholder:
72 case CK_Informative:
73 delete [] Text;
74 break;
Douglas Gregore6e03612009-09-18 22:15:54 +000075 }
76}
77
78CodeCompletionString::~CodeCompletionString() {
79 std::for_each(Chunks.begin(), Chunks.end(),
80 std::mem_fun_ref(&Chunk::Destroy));
81}
82
83std::string CodeCompletionString::getAsString() const {
84 std::string Result;
85 llvm::raw_string_ostream OS(Result);
86
87 for (iterator C = begin(), CEnd = end(); C != CEnd; ++C) {
88 switch (C->Kind) {
89 case CK_Text: OS << C->Text; break;
90 case CK_Optional: OS << "{#" << C->Optional->getAsString() << "#}"; break;
Douglas Gregor0563c262009-09-22 23:15:58 +000091 case CK_Placeholder: OS << "<#" << C->Text << "#>"; break;
92 case CK_Informative: OS << "[#" << C->Text << "#]"; break;
Douglas Gregore6e03612009-09-18 22:15:54 +000093 }
94 }
Douglas Gregore2b7eea2009-09-29 15:13:39 +000095 OS.flush();
Douglas Gregore6e03612009-09-18 22:15:54 +000096 return Result;
97}
98
99//===----------------------------------------------------------------------===//
Douglas Gregor05944382009-09-23 00:16:58 +0000100// Code completion overload candidate implementation
101//===----------------------------------------------------------------------===//
102FunctionDecl *
103CodeCompleteConsumer::OverloadCandidate::getFunction() const {
104 if (getKind() == CK_Function)
105 return Function;
106 else if (getKind() == CK_FunctionTemplate)
107 return FunctionTemplate->getTemplatedDecl();
108 else
109 return 0;
110}
111
112const FunctionType *
113CodeCompleteConsumer::OverloadCandidate::getFunctionType() const {
114 switch (Kind) {
115 case CK_Function:
116 return Function->getType()->getAs<FunctionType>();
117
118 case CK_FunctionTemplate:
119 return FunctionTemplate->getTemplatedDecl()->getType()
120 ->getAs<FunctionType>();
121
122 case CK_FunctionType:
123 return Type;
124 }
125
126 return 0;
127}
128
129//===----------------------------------------------------------------------===//
Douglas Gregore6e03612009-09-18 22:15:54 +0000130// Code completion consumer implementation
131//===----------------------------------------------------------------------===//
132
Douglas Gregor86d9a522009-09-21 16:56:56 +0000133CodeCompleteConsumer::~CodeCompleteConsumer() { }
Douglas Gregore6e03612009-09-18 22:15:54 +0000134
Douglas Gregor81b747b2009-09-17 21:32:03 +0000135void
136PrintingCodeCompleteConsumer::ProcessCodeCompleteResults(Result *Results,
137 unsigned NumResults) {
Douglas Gregor81b747b2009-09-17 21:32:03 +0000138 // Print the results.
139 for (unsigned I = 0; I != NumResults; ++I) {
Douglas Gregorf52cede2009-10-09 22:16:47 +0000140 OS << "COMPLETION: ";
Douglas Gregor81b747b2009-09-17 21:32:03 +0000141 switch (Results[I].Kind) {
142 case Result::RK_Declaration:
143 OS << Results[I].Declaration->getNameAsString() << " : "
144 << Results[I].Rank;
145 if (Results[I].Hidden)
146 OS << " (Hidden)";
Douglas Gregor86d9a522009-09-21 16:56:56 +0000147 if (CodeCompletionString *CCS
148 = Results[I].CreateCodeCompletionString(SemaRef)) {
Douglas Gregore6e03612009-09-18 22:15:54 +0000149 OS << " : " << CCS->getAsString();
150 delete CCS;
151 }
152
Douglas Gregor81b747b2009-09-17 21:32:03 +0000153 OS << '\n';
154 break;
155
156 case Result::RK_Keyword:
157 OS << Results[I].Keyword << " : " << Results[I].Rank << '\n';
158 break;
159 }
160 }
161
162 // Once we've printed the code-completion results, suppress remaining
163 // diagnostics.
164 // FIXME: Move this somewhere else!
Douglas Gregor86d9a522009-09-21 16:56:56 +0000165 SemaRef.PP.getDiagnostics().setSuppressAllDiagnostics();
Douglas Gregor81b747b2009-09-17 21:32:03 +0000166}
Douglas Gregor05944382009-09-23 00:16:58 +0000167
168void
169PrintingCodeCompleteConsumer::ProcessOverloadCandidates(unsigned CurrentArg,
170 OverloadCandidate *Candidates,
171 unsigned NumCandidates) {
172 for (unsigned I = 0; I != NumCandidates; ++I) {
Douglas Gregor86d802e2009-09-23 00:34:09 +0000173 if (CodeCompletionString *CCS
174 = Candidates[I].CreateSignatureString(CurrentArg, SemaRef)) {
Douglas Gregorf52cede2009-10-09 22:16:47 +0000175 OS << "OVERLOAD: " << CCS->getAsString() << "\n";
Douglas Gregor86d802e2009-09-23 00:34:09 +0000176 delete CCS;
Douglas Gregor05944382009-09-23 00:16:58 +0000177 }
178 }
179
180 // Once we've printed the code-completion results, suppress remaining
181 // diagnostics.
182 // FIXME: Move this somewhere else!
183 SemaRef.PP.getDiagnostics().setSuppressAllDiagnostics();
184}