aboutsummaryrefslogtreecommitdiff
path: root/lib/Sema/CodeCompleteConsumer.cpp
diff options
context:
space:
mode:
authorDouglas Gregor <dgregor@apple.com>2009-09-18 22:47:56 +0000
committerDouglas Gregor <dgregor@apple.com>2009-09-18 22:47:56 +0000
commit56ff871ae74b1edccff44efe296f3167d694ce48 (patch)
tree72eb245f525f2e8f4acc721a89adb2803a932499 /lib/Sema/CodeCompleteConsumer.cpp
parente0a58073b76fc016325a35152533b8468df2bf4a (diff)
Introduce code completion patterns for templates, which provide the
angle brackets < > along with placeholder template arguments. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@82304 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Sema/CodeCompleteConsumer.cpp')
-rw-r--r--lib/Sema/CodeCompleteConsumer.cpp88
1 files changed, 88 insertions, 0 deletions
diff --git a/lib/Sema/CodeCompleteConsumer.cpp b/lib/Sema/CodeCompleteConsumer.cpp
index 9bf9e15253..2c682ebbfc 100644
--- a/lib/Sema/CodeCompleteConsumer.cpp
+++ b/lib/Sema/CodeCompleteConsumer.cpp
@@ -762,6 +762,71 @@ static void AddFunctionParameterChunks(ASTContext &Context,
}
}
+/// \brief Add template parameter chunks to the given code completion string.
+static void AddTemplateParameterChunks(ASTContext &Context,
+ TemplateDecl *Template,
+ CodeCompletionString *Result) {
+ CodeCompletionString *CCStr = Result;
+ bool FirstParameter = true;
+
+ TemplateParameterList *Params = Template->getTemplateParameters();
+ for (TemplateParameterList::iterator P = Params->begin(),
+ PEnd = Params->end();
+ P != PEnd; ++P) {
+ bool HasDefaultArg = false;
+ std::string PlaceholderStr;
+ if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*P)) {
+ if (TTP->wasDeclaredWithTypename())
+ PlaceholderStr = "typename";
+ else
+ PlaceholderStr = "class";
+
+ if (TTP->getIdentifier()) {
+ PlaceholderStr += ' ';
+ PlaceholderStr += TTP->getIdentifier()->getName();
+ }
+
+ HasDefaultArg = TTP->hasDefaultArgument();
+ } else if (NonTypeTemplateParmDecl *NTTP
+ = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
+ if (NTTP->getIdentifier())
+ PlaceholderStr = NTTP->getIdentifier()->getName();
+ NTTP->getType().getAsStringInternal(PlaceholderStr,
+ Context.PrintingPolicy);
+ HasDefaultArg = NTTP->hasDefaultArgument();
+ } else {
+ assert(isa<TemplateTemplateParmDecl>(*P));
+ TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
+
+ // Since putting the template argument list into the placeholder would
+ // be very, very long, we just use an abbreviation.
+ PlaceholderStr = "template<...> class";
+ if (TTP->getIdentifier()) {
+ PlaceholderStr += ' ';
+ PlaceholderStr += TTP->getIdentifier()->getName();
+ }
+
+ HasDefaultArg = TTP->hasDefaultArgument();
+ }
+
+ if (HasDefaultArg) {
+ // When we see an optional default argument, put that argument and
+ // the remaining default arguments into a new, optional string.
+ CodeCompletionString *Opt = new CodeCompletionString;
+ CCStr->AddOptionalChunk(std::auto_ptr<CodeCompletionString>(Opt));
+ CCStr = Opt;
+ }
+
+ if (FirstParameter)
+ FirstParameter = false;
+ else
+ CCStr->AddTextChunk(", ");
+
+ // Add the placeholder string.
+ CCStr->AddPlaceholderChunk(PlaceholderStr.c_str());
+ }
+}
+
/// \brief If possible, create a new code completion string for the given
/// result.
///
@@ -784,6 +849,29 @@ CodeCompleteConsumer::CreateCodeCompletionString(Result R) {
return Result;
}
+ if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND)) {
+ // FIXME: We treat these like functions for now, but it would be far
+ // better if we computed the template parameters that are non-deduced from
+ // a call, then printed only those template parameters in "<...>" before
+ // printing the function call arguments.
+ CodeCompletionString *Result = new CodeCompletionString;
+ FunctionDecl *Function = FunTmpl->getTemplatedDecl();
+ Result->AddTextChunk(Function->getNameAsString().c_str());
+ Result->AddTextChunk("(");
+ AddFunctionParameterChunks(getSema().Context, Function, Result);
+ Result->AddTextChunk(")");
+ return Result;
+ }
+
+ if (TemplateDecl *Template = dyn_cast<TemplateDecl>(ND)) {
+ CodeCompletionString *Result = new CodeCompletionString;
+ Result->AddTextChunk(Template->getNameAsString().c_str());
+ Result->AddTextChunk("<");
+ AddTemplateParameterChunks(getSema().Context, Template, Result);
+ Result->AddTextChunk(">");
+ return Result;
+ }
+
return 0;
}