Initial implementation of a code-completion interface in Clang. In
essence, code completion is triggered by a magic "code completion"
token produced by the lexer [*], which the parser recognizes at
certain points in the grammar. The parser then calls into the Action
object with the appropriate CodeCompletionXXX action.
Sema implements the CodeCompletionXXX callbacks by performing minimal
translation, then forwarding them to a CodeCompletionConsumer
subclass, which uses the results of semantic analysis to provide
code-completion results. At present, only a single, "printing" code
completion consumer is available, for regression testing and
debugging. However, the design is meant to permit other
code-completion consumers.
This initial commit contains two code-completion actions: one for
member access, e.g., "x." or "p->", and one for
nested-name-specifiers, e.g., "std::". More code-completion actions
will follow, along with improved gathering of code-completion results
for the various contexts.
[*] In the current -code-completion-dump testing/debugging mode, the
file is truncated at the completion point and EOF is translated into
"code completion".
llvm-svn: 82166
2009-09-18 05:32:03 +08:00
|
|
|
//===---------------- SemaCodeComplete.cpp - Code Completion ----*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the code-completion semantic actions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "Sema.h"
|
|
|
|
#include "clang/Sema/CodeCompleteConsumer.h"
|
2009-09-22 03:57:38 +08:00
|
|
|
#include "clang/AST/ExprCXX.h"
|
2009-11-18 01:59:40 +08:00
|
|
|
#include "clang/AST/ExprObjC.h"
|
2009-10-31 00:50:04 +08:00
|
|
|
#include "clang/Lex/MacroInfo.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2009-09-22 00:56:56 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2009-09-28 11:51:44 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2009-09-22 00:56:56 +08:00
|
|
|
#include <list>
|
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
Initial implementation of a code-completion interface in Clang. In
essence, code completion is triggered by a magic "code completion"
token produced by the lexer [*], which the parser recognizes at
certain points in the grammar. The parser then calls into the Action
object with the appropriate CodeCompletionXXX action.
Sema implements the CodeCompletionXXX callbacks by performing minimal
translation, then forwarding them to a CodeCompletionConsumer
subclass, which uses the results of semantic analysis to provide
code-completion results. At present, only a single, "printing" code
completion consumer is available, for regression testing and
debugging. However, the design is meant to permit other
code-completion consumers.
This initial commit contains two code-completion actions: one for
member access, e.g., "x." or "p->", and one for
nested-name-specifiers, e.g., "std::". More code-completion actions
will follow, along with improved gathering of code-completion results
for the various contexts.
[*] In the current -code-completion-dump testing/debugging mode, the
file is truncated at the completion point and EOF is translated into
"code completion".
llvm-svn: 82166
2009-09-18 05:32:03 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2009-09-22 00:56:56 +08:00
|
|
|
namespace {
|
|
|
|
/// \brief A container of code-completion results.
|
|
|
|
class ResultBuilder {
|
|
|
|
public:
|
|
|
|
/// \brief The type of a name-lookup filter, which can be provided to the
|
|
|
|
/// name-lookup routines to specify which declarations should be included in
|
|
|
|
/// the result set (when it returns true) and which declarations should be
|
|
|
|
/// filtered out (returns false).
|
|
|
|
typedef bool (ResultBuilder::*LookupFilter)(NamedDecl *) const;
|
|
|
|
|
|
|
|
typedef CodeCompleteConsumer::Result Result;
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// \brief The actual results we have found.
|
|
|
|
std::vector<Result> Results;
|
|
|
|
|
|
|
|
/// \brief A record of all of the declarations we have found and placed
|
|
|
|
/// into the result set, used to ensure that no declaration ever gets into
|
|
|
|
/// the result set twice.
|
|
|
|
llvm::SmallPtrSet<Decl*, 16> AllDeclsFound;
|
|
|
|
|
2009-12-07 04:23:50 +08:00
|
|
|
typedef std::pair<NamedDecl *, unsigned> DeclIndexPair;
|
|
|
|
|
|
|
|
/// \brief An entry in the shadow map, which is optimized to store
|
|
|
|
/// a single (declaration, index) mapping (the common case) but
|
|
|
|
/// can also store a list of (declaration, index) mappings.
|
|
|
|
class ShadowMapEntry {
|
|
|
|
typedef llvm::SmallVector<DeclIndexPair, 4> DeclIndexPairVector;
|
|
|
|
|
|
|
|
/// \brief Contains either the solitary NamedDecl * or a vector
|
|
|
|
/// of (declaration, index) pairs.
|
|
|
|
llvm::PointerUnion<NamedDecl *, DeclIndexPairVector*> DeclOrVector;
|
|
|
|
|
|
|
|
/// \brief When the entry contains a single declaration, this is
|
|
|
|
/// the index associated with that entry.
|
|
|
|
unsigned SingleDeclIndex;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ShadowMapEntry() : DeclOrVector(), SingleDeclIndex(0) { }
|
|
|
|
|
|
|
|
void Add(NamedDecl *ND, unsigned Index) {
|
|
|
|
if (DeclOrVector.isNull()) {
|
|
|
|
// 0 - > 1 elements: just set the single element information.
|
|
|
|
DeclOrVector = ND;
|
|
|
|
SingleDeclIndex = Index;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NamedDecl *PrevND = DeclOrVector.dyn_cast<NamedDecl *>()) {
|
|
|
|
// 1 -> 2 elements: create the vector of results and push in the
|
|
|
|
// existing declaration.
|
|
|
|
DeclIndexPairVector *Vec = new DeclIndexPairVector;
|
|
|
|
Vec->push_back(DeclIndexPair(PrevND, SingleDeclIndex));
|
|
|
|
DeclOrVector = Vec;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the new element to the end of the vector.
|
|
|
|
DeclOrVector.get<DeclIndexPairVector*>()->push_back(
|
|
|
|
DeclIndexPair(ND, Index));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Destroy() {
|
|
|
|
if (DeclIndexPairVector *Vec
|
|
|
|
= DeclOrVector.dyn_cast<DeclIndexPairVector *>()) {
|
|
|
|
delete Vec;
|
|
|
|
DeclOrVector = ((NamedDecl *)0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iteration.
|
|
|
|
class iterator;
|
|
|
|
iterator begin() const;
|
|
|
|
iterator end() const;
|
|
|
|
};
|
|
|
|
|
2009-09-22 00:56:56 +08:00
|
|
|
/// \brief A mapping from declaration names to the declarations that have
|
|
|
|
/// this name within a particular scope and their index within the list of
|
|
|
|
/// results.
|
2009-12-07 04:23:50 +08:00
|
|
|
typedef llvm::DenseMap<DeclarationName, ShadowMapEntry> ShadowMap;
|
2009-09-22 00:56:56 +08:00
|
|
|
|
|
|
|
/// \brief The semantic analysis object for which results are being
|
|
|
|
/// produced.
|
|
|
|
Sema &SemaRef;
|
|
|
|
|
|
|
|
/// \brief If non-NULL, a filter function used to remove any code-completion
|
|
|
|
/// results that are not desirable.
|
|
|
|
LookupFilter Filter;
|
|
|
|
|
|
|
|
/// \brief A list of shadow maps, which is used to model name hiding at
|
|
|
|
/// different levels of, e.g., the inheritance hierarchy.
|
|
|
|
std::list<ShadowMap> ShadowMaps;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit ResultBuilder(Sema &SemaRef, LookupFilter Filter = 0)
|
|
|
|
: SemaRef(SemaRef), Filter(Filter) { }
|
|
|
|
|
|
|
|
/// \brief Set the filter used for code-completion results.
|
|
|
|
void setFilter(LookupFilter Filter) {
|
|
|
|
this->Filter = Filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef std::vector<Result>::iterator iterator;
|
|
|
|
iterator begin() { return Results.begin(); }
|
|
|
|
iterator end() { return Results.end(); }
|
|
|
|
|
|
|
|
Result *data() { return Results.empty()? 0 : &Results.front(); }
|
|
|
|
unsigned size() const { return Results.size(); }
|
|
|
|
bool empty() const { return Results.empty(); }
|
|
|
|
|
|
|
|
/// \brief Add a new result to this result set (if it isn't already in one
|
|
|
|
/// of the shadow maps), or replace an existing result (for, e.g., a
|
|
|
|
/// redeclaration).
|
2009-09-22 04:12:40 +08:00
|
|
|
///
|
|
|
|
/// \param R the result to add (if it is unique).
|
|
|
|
///
|
|
|
|
/// \param R the context in which this result will be named.
|
|
|
|
void MaybeAddResult(Result R, DeclContext *CurContext = 0);
|
2009-09-22 00:56:56 +08:00
|
|
|
|
|
|
|
/// \brief Enter into a new scope.
|
|
|
|
void EnterNewScope();
|
|
|
|
|
|
|
|
/// \brief Exit from the current scope.
|
|
|
|
void ExitScope();
|
|
|
|
|
2009-11-18 12:19:12 +08:00
|
|
|
/// \brief Ignore this declaration, if it is seen again.
|
|
|
|
void Ignore(Decl *D) { AllDeclsFound.insert(D->getCanonicalDecl()); }
|
|
|
|
|
2009-09-22 00:56:56 +08:00
|
|
|
/// \name Name lookup predicates
|
|
|
|
///
|
|
|
|
/// These predicates can be passed to the name lookup functions to filter the
|
|
|
|
/// results of name lookup. All of the predicates have the same type, so that
|
|
|
|
///
|
|
|
|
//@{
|
2009-09-22 04:51:25 +08:00
|
|
|
bool IsOrdinaryName(NamedDecl *ND) const;
|
2009-09-22 00:56:56 +08:00
|
|
|
bool IsNestedNameSpecifier(NamedDecl *ND) const;
|
|
|
|
bool IsEnum(NamedDecl *ND) const;
|
|
|
|
bool IsClassOrStruct(NamedDecl *ND) const;
|
|
|
|
bool IsUnion(NamedDecl *ND) const;
|
|
|
|
bool IsNamespace(NamedDecl *ND) const;
|
|
|
|
bool IsNamespaceOrAlias(NamedDecl *ND) const;
|
|
|
|
bool IsType(NamedDecl *ND) const;
|
2009-09-24 06:26:46 +08:00
|
|
|
bool IsMember(NamedDecl *ND) const;
|
2009-09-22 00:56:56 +08:00
|
|
|
//@}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2009-12-07 04:23:50 +08:00
|
|
|
class ResultBuilder::ShadowMapEntry::iterator {
|
|
|
|
llvm::PointerUnion<NamedDecl*, const DeclIndexPair*> DeclOrIterator;
|
|
|
|
unsigned SingleDeclIndex;
|
|
|
|
|
|
|
|
public:
|
|
|
|
typedef DeclIndexPair value_type;
|
|
|
|
typedef value_type reference;
|
|
|
|
typedef std::ptrdiff_t difference_type;
|
|
|
|
typedef std::input_iterator_tag iterator_category;
|
|
|
|
|
|
|
|
class pointer {
|
|
|
|
DeclIndexPair Value;
|
|
|
|
|
|
|
|
public:
|
|
|
|
pointer(const DeclIndexPair &Value) : Value(Value) { }
|
|
|
|
|
|
|
|
const DeclIndexPair *operator->() const {
|
|
|
|
return &Value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
iterator() : DeclOrIterator((NamedDecl *)0), SingleDeclIndex(0) { }
|
|
|
|
|
|
|
|
iterator(NamedDecl *SingleDecl, unsigned Index)
|
|
|
|
: DeclOrIterator(SingleDecl), SingleDeclIndex(Index) { }
|
|
|
|
|
|
|
|
iterator(const DeclIndexPair *Iterator)
|
|
|
|
: DeclOrIterator(Iterator), SingleDeclIndex(0) { }
|
|
|
|
|
|
|
|
iterator &operator++() {
|
|
|
|
if (DeclOrIterator.is<NamedDecl *>()) {
|
|
|
|
DeclOrIterator = (NamedDecl *)0;
|
|
|
|
SingleDeclIndex = 0;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
const DeclIndexPair *I = DeclOrIterator.get<const DeclIndexPair*>();
|
|
|
|
++I;
|
|
|
|
DeclOrIterator = I;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator operator++(int) {
|
|
|
|
iterator tmp(*this);
|
|
|
|
++(*this);
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
reference operator*() const {
|
|
|
|
if (NamedDecl *ND = DeclOrIterator.dyn_cast<NamedDecl *>())
|
|
|
|
return reference(ND, SingleDeclIndex);
|
|
|
|
|
2009-12-07 05:27:58 +08:00
|
|
|
return *DeclOrIterator.get<const DeclIndexPair*>();
|
2009-12-07 04:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pointer operator->() const {
|
|
|
|
return pointer(**this);
|
|
|
|
}
|
|
|
|
|
|
|
|
friend bool operator==(const iterator &X, const iterator &Y) {
|
2009-12-07 05:27:58 +08:00
|
|
|
return X.DeclOrIterator.getOpaqueValue()
|
|
|
|
== Y.DeclOrIterator.getOpaqueValue() &&
|
2009-12-07 04:23:50 +08:00
|
|
|
X.SingleDeclIndex == Y.SingleDeclIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend bool operator!=(const iterator &X, const iterator &Y) {
|
2009-12-07 05:27:58 +08:00
|
|
|
return !(X == Y);
|
2009-12-07 04:23:50 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
template<>
|
|
|
|
struct DenseMapInfo<ResultBuilder::ShadowMapEntry> {
|
|
|
|
static bool isPod() { return false; }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ResultBuilder::ShadowMapEntry::iterator
|
|
|
|
ResultBuilder::ShadowMapEntry::begin() const {
|
|
|
|
if (DeclOrVector.isNull())
|
|
|
|
return iterator();
|
|
|
|
|
|
|
|
if (NamedDecl *ND = DeclOrVector.dyn_cast<NamedDecl *>())
|
|
|
|
return iterator(ND, SingleDeclIndex);
|
|
|
|
|
|
|
|
return iterator(DeclOrVector.get<DeclIndexPairVector *>()->begin());
|
|
|
|
}
|
|
|
|
|
|
|
|
ResultBuilder::ShadowMapEntry::iterator
|
|
|
|
ResultBuilder::ShadowMapEntry::end() const {
|
|
|
|
if (DeclOrVector.is<NamedDecl *>() || DeclOrVector.isNull())
|
|
|
|
return iterator();
|
|
|
|
|
|
|
|
return iterator(DeclOrVector.get<DeclIndexPairVector *>()->end());
|
|
|
|
}
|
|
|
|
|
2009-09-22 00:56:56 +08:00
|
|
|
/// \brief Determines whether the given hidden result could be found with
|
|
|
|
/// some extra work, e.g., by qualifying the name.
|
|
|
|
///
|
|
|
|
/// \param Hidden the declaration that is hidden by the currenly \p Visible
|
|
|
|
/// declaration.
|
|
|
|
///
|
|
|
|
/// \param Visible the declaration with the same name that is already visible.
|
|
|
|
///
|
|
|
|
/// \returns true if the hidden result can be found by some mechanism,
|
|
|
|
/// false otherwise.
|
|
|
|
static bool canHiddenResultBeFound(const LangOptions &LangOpts,
|
|
|
|
NamedDecl *Hidden, NamedDecl *Visible) {
|
|
|
|
// In C, there is no way to refer to a hidden name.
|
|
|
|
if (!LangOpts.CPlusPlus)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
DeclContext *HiddenCtx = Hidden->getDeclContext()->getLookupContext();
|
|
|
|
|
|
|
|
// There is no way to qualify a name declared in a function or method.
|
|
|
|
if (HiddenCtx->isFunctionOrMethod())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return HiddenCtx != Visible->getDeclContext()->getLookupContext();
|
|
|
|
}
|
|
|
|
|
2009-09-22 04:12:40 +08:00
|
|
|
/// \brief Compute the qualification required to get from the current context
|
|
|
|
/// (\p CurContext) to the target context (\p TargetContext).
|
|
|
|
///
|
|
|
|
/// \param Context the AST context in which the qualification will be used.
|
|
|
|
///
|
|
|
|
/// \param CurContext the context where an entity is being named, which is
|
|
|
|
/// typically based on the current scope.
|
|
|
|
///
|
|
|
|
/// \param TargetContext the context in which the named entity actually
|
|
|
|
/// resides.
|
|
|
|
///
|
|
|
|
/// \returns a nested name specifier that refers into the target context, or
|
|
|
|
/// NULL if no qualification is needed.
|
|
|
|
static NestedNameSpecifier *
|
|
|
|
getRequiredQualification(ASTContext &Context,
|
|
|
|
DeclContext *CurContext,
|
|
|
|
DeclContext *TargetContext) {
|
|
|
|
llvm::SmallVector<DeclContext *, 4> TargetParents;
|
|
|
|
|
|
|
|
for (DeclContext *CommonAncestor = TargetContext;
|
|
|
|
CommonAncestor && !CommonAncestor->Encloses(CurContext);
|
|
|
|
CommonAncestor = CommonAncestor->getLookupParent()) {
|
|
|
|
if (CommonAncestor->isTransparentContext() ||
|
|
|
|
CommonAncestor->isFunctionOrMethod())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
TargetParents.push_back(CommonAncestor);
|
|
|
|
}
|
|
|
|
|
|
|
|
NestedNameSpecifier *Result = 0;
|
|
|
|
while (!TargetParents.empty()) {
|
|
|
|
DeclContext *Parent = TargetParents.back();
|
|
|
|
TargetParents.pop_back();
|
|
|
|
|
|
|
|
if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Parent))
|
|
|
|
Result = NestedNameSpecifier::Create(Context, Result, Namespace);
|
|
|
|
else if (TagDecl *TD = dyn_cast<TagDecl>(Parent))
|
|
|
|
Result = NestedNameSpecifier::Create(Context, Result,
|
|
|
|
false,
|
|
|
|
Context.getTypeDeclType(TD).getTypePtr());
|
|
|
|
else
|
|
|
|
assert(Parent->isTranslationUnit());
|
2009-11-07 08:00:49 +08:00
|
|
|
}
|
2009-09-22 04:12:40 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResultBuilder::MaybeAddResult(Result R, DeclContext *CurContext) {
|
2009-09-23 07:31:26 +08:00
|
|
|
assert(!ShadowMaps.empty() && "Must enter into a results scope");
|
|
|
|
|
2009-09-22 00:56:56 +08:00
|
|
|
if (R.Kind != Result::RK_Declaration) {
|
|
|
|
// For non-declaration results, just add the result.
|
|
|
|
Results.push_back(R);
|
|
|
|
return;
|
|
|
|
}
|
2009-10-10 06:16:47 +08:00
|
|
|
|
|
|
|
// Skip unnamed entities.
|
|
|
|
if (!R.Declaration->getDeclName())
|
|
|
|
return;
|
|
|
|
|
2009-09-22 00:56:56 +08:00
|
|
|
// Look through using declarations.
|
2009-11-17 13:59:44 +08:00
|
|
|
if (UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(R.Declaration))
|
2009-09-23 07:15:58 +08:00
|
|
|
MaybeAddResult(Result(Using->getTargetDecl(), R.Rank, R.Qualifier),
|
|
|
|
CurContext);
|
2009-09-22 00:56:56 +08:00
|
|
|
|
|
|
|
Decl *CanonDecl = R.Declaration->getCanonicalDecl();
|
|
|
|
unsigned IDNS = CanonDecl->getIdentifierNamespace();
|
|
|
|
|
|
|
|
// Friend declarations and declarations introduced due to friends are never
|
|
|
|
// added as results.
|
|
|
|
if (isa<FriendDecl>(CanonDecl) ||
|
|
|
|
(IDNS & (Decl::IDNS_OrdinaryFriend | Decl::IDNS_TagFriend)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (const IdentifierInfo *Id = R.Declaration->getIdentifier()) {
|
|
|
|
// __va_list_tag is a freak of nature. Find it and skip it.
|
|
|
|
if (Id->isStr("__va_list_tag") || Id->isStr("__builtin_va_list"))
|
|
|
|
return;
|
|
|
|
|
2009-10-10 06:16:47 +08:00
|
|
|
// Filter out names reserved for the implementation (C99 7.1.3,
|
|
|
|
// C++ [lib.global.names]). Users don't need to see those.
|
2009-10-19 04:26:12 +08:00
|
|
|
//
|
|
|
|
// FIXME: Add predicate for this.
|
2009-10-10 06:16:47 +08:00
|
|
|
if (Id->getLength() >= 2) {
|
2009-10-19 04:26:12 +08:00
|
|
|
const char *Name = Id->getNameStart();
|
2009-10-10 06:16:47 +08:00
|
|
|
if (Name[0] == '_' &&
|
|
|
|
(Name[1] == '_' || (Name[1] >= 'A' && Name[1] <= 'Z')))
|
|
|
|
return;
|
|
|
|
}
|
2009-09-22 00:56:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// C++ constructors are never found by name lookup.
|
|
|
|
if (isa<CXXConstructorDecl>(CanonDecl))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Filter out any unwanted results.
|
|
|
|
if (Filter && !(this->*Filter)(R.Declaration))
|
|
|
|
return;
|
|
|
|
|
|
|
|
ShadowMap &SMap = ShadowMaps.back();
|
2009-12-07 04:23:50 +08:00
|
|
|
ShadowMapEntry::iterator I, IEnd;
|
|
|
|
ShadowMap::iterator NamePos = SMap.find(R.Declaration->getDeclName());
|
|
|
|
if (NamePos != SMap.end()) {
|
|
|
|
I = NamePos->second.begin();
|
|
|
|
IEnd = NamePos->second.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; I != IEnd; ++I) {
|
|
|
|
NamedDecl *ND = I->first;
|
|
|
|
unsigned Index = I->second;
|
2009-09-22 00:56:56 +08:00
|
|
|
if (ND->getCanonicalDecl() == CanonDecl) {
|
|
|
|
// This is a redeclaration. Always pick the newer declaration.
|
|
|
|
Results[Index].Declaration = R.Declaration;
|
|
|
|
|
|
|
|
// Pick the best rank of the two.
|
|
|
|
Results[Index].Rank = std::min(Results[Index].Rank, R.Rank);
|
|
|
|
|
|
|
|
// We're done.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is a new declaration in this scope. However, check whether this
|
|
|
|
// declaration name is hidden by a similarly-named declaration in an outer
|
|
|
|
// scope.
|
|
|
|
std::list<ShadowMap>::iterator SM, SMEnd = ShadowMaps.end();
|
|
|
|
--SMEnd;
|
|
|
|
for (SM = ShadowMaps.begin(); SM != SMEnd; ++SM) {
|
2009-12-07 04:23:50 +08:00
|
|
|
ShadowMapEntry::iterator I, IEnd;
|
|
|
|
ShadowMap::iterator NamePos = SM->find(R.Declaration->getDeclName());
|
|
|
|
if (NamePos != SM->end()) {
|
|
|
|
I = NamePos->second.begin();
|
|
|
|
IEnd = NamePos->second.end();
|
|
|
|
}
|
|
|
|
for (; I != IEnd; ++I) {
|
2009-09-22 00:56:56 +08:00
|
|
|
// A tag declaration does not hide a non-tag declaration.
|
2009-12-07 04:23:50 +08:00
|
|
|
if (I->first->getIdentifierNamespace() == Decl::IDNS_Tag &&
|
2009-09-22 00:56:56 +08:00
|
|
|
(IDNS & (Decl::IDNS_Member | Decl::IDNS_Ordinary |
|
|
|
|
Decl::IDNS_ObjCProtocol)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Protocols are in distinct namespaces from everything else.
|
2009-12-07 04:23:50 +08:00
|
|
|
if (((I->first->getIdentifierNamespace() & Decl::IDNS_ObjCProtocol)
|
2009-09-22 00:56:56 +08:00
|
|
|
|| (IDNS & Decl::IDNS_ObjCProtocol)) &&
|
2009-12-07 04:23:50 +08:00
|
|
|
I->first->getIdentifierNamespace() != IDNS)
|
2009-09-22 00:56:56 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// The newly-added result is hidden by an entry in the shadow map.
|
|
|
|
if (canHiddenResultBeFound(SemaRef.getLangOptions(), R.Declaration,
|
2009-12-07 04:23:50 +08:00
|
|
|
I->first)) {
|
2009-09-22 00:56:56 +08:00
|
|
|
// Note that this result was hidden.
|
|
|
|
R.Hidden = true;
|
2009-09-23 07:15:58 +08:00
|
|
|
R.QualifierIsInformative = false;
|
2009-09-22 04:12:40 +08:00
|
|
|
|
|
|
|
if (!R.Qualifier)
|
|
|
|
R.Qualifier = getRequiredQualification(SemaRef.Context,
|
|
|
|
CurContext,
|
|
|
|
R.Declaration->getDeclContext());
|
2009-09-22 00:56:56 +08:00
|
|
|
} else {
|
|
|
|
// This result was hidden and cannot be found; don't bother adding
|
|
|
|
// it.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that any given declaration only shows up in the result set once.
|
|
|
|
if (!AllDeclsFound.insert(CanonDecl))
|
|
|
|
return;
|
|
|
|
|
2009-09-24 06:26:46 +08:00
|
|
|
// If the filter is for nested-name-specifiers, then this result starts a
|
|
|
|
// nested-name-specifier.
|
|
|
|
if ((Filter == &ResultBuilder::IsNestedNameSpecifier) ||
|
|
|
|
(Filter == &ResultBuilder::IsMember &&
|
|
|
|
isa<CXXRecordDecl>(R.Declaration) &&
|
|
|
|
cast<CXXRecordDecl>(R.Declaration)->isInjectedClassName()))
|
|
|
|
R.StartsNestedNameSpecifier = true;
|
|
|
|
|
2009-09-23 07:15:58 +08:00
|
|
|
// If this result is supposed to have an informative qualifier, add one.
|
2009-09-24 06:26:46 +08:00
|
|
|
if (R.QualifierIsInformative && !R.Qualifier &&
|
|
|
|
!R.StartsNestedNameSpecifier) {
|
2009-09-23 07:15:58 +08:00
|
|
|
DeclContext *Ctx = R.Declaration->getDeclContext();
|
|
|
|
if (NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx))
|
|
|
|
R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, Namespace);
|
|
|
|
else if (TagDecl *Tag = dyn_cast<TagDecl>(Ctx))
|
|
|
|
R.Qualifier = NestedNameSpecifier::Create(SemaRef.Context, 0, false,
|
|
|
|
SemaRef.Context.getTypeDeclType(Tag).getTypePtr());
|
|
|
|
else
|
|
|
|
R.QualifierIsInformative = false;
|
|
|
|
}
|
2009-09-24 06:26:46 +08:00
|
|
|
|
2009-09-22 00:56:56 +08:00
|
|
|
// Insert this result into the set of results and into the current shadow
|
|
|
|
// map.
|
2009-12-07 04:23:50 +08:00
|
|
|
SMap[R.Declaration->getDeclName()].Add(R.Declaration, Results.size());
|
2009-09-22 00:56:56 +08:00
|
|
|
Results.push_back(R);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Enter into a new scope.
|
|
|
|
void ResultBuilder::EnterNewScope() {
|
|
|
|
ShadowMaps.push_back(ShadowMap());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Exit from the current scope.
|
|
|
|
void ResultBuilder::ExitScope() {
|
2009-12-07 04:23:50 +08:00
|
|
|
for (ShadowMap::iterator E = ShadowMaps.back().begin(),
|
|
|
|
EEnd = ShadowMaps.back().end();
|
|
|
|
E != EEnd;
|
|
|
|
++E)
|
|
|
|
E->second.Destroy();
|
|
|
|
|
2009-09-22 00:56:56 +08:00
|
|
|
ShadowMaps.pop_back();
|
|
|
|
}
|
|
|
|
|
2009-09-22 04:51:25 +08:00
|
|
|
/// \brief Determines whether this given declaration will be found by
|
|
|
|
/// ordinary name lookup.
|
|
|
|
bool ResultBuilder::IsOrdinaryName(NamedDecl *ND) const {
|
|
|
|
unsigned IDNS = Decl::IDNS_Ordinary;
|
|
|
|
if (SemaRef.getLangOptions().CPlusPlus)
|
|
|
|
IDNS |= Decl::IDNS_Tag;
|
|
|
|
|
|
|
|
return ND->getIdentifierNamespace() & IDNS;
|
|
|
|
}
|
|
|
|
|
2009-09-22 00:56:56 +08:00
|
|
|
/// \brief Determines whether the given declaration is suitable as the
|
|
|
|
/// start of a C++ nested-name-specifier, e.g., a class or namespace.
|
|
|
|
bool ResultBuilder::IsNestedNameSpecifier(NamedDecl *ND) const {
|
|
|
|
// Allow us to find class templates, too.
|
|
|
|
if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND))
|
|
|
|
ND = ClassTemplate->getTemplatedDecl();
|
|
|
|
|
|
|
|
return SemaRef.isAcceptableNestedNameSpecifier(ND);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Determines whether the given declaration is an enumeration.
|
|
|
|
bool ResultBuilder::IsEnum(NamedDecl *ND) const {
|
|
|
|
return isa<EnumDecl>(ND);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Determines whether the given declaration is a class or struct.
|
|
|
|
bool ResultBuilder::IsClassOrStruct(NamedDecl *ND) const {
|
|
|
|
// Allow us to find class templates, too.
|
|
|
|
if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND))
|
|
|
|
ND = ClassTemplate->getTemplatedDecl();
|
|
|
|
|
|
|
|
if (RecordDecl *RD = dyn_cast<RecordDecl>(ND))
|
|
|
|
return RD->getTagKind() == TagDecl::TK_class ||
|
|
|
|
RD->getTagKind() == TagDecl::TK_struct;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Determines whether the given declaration is a union.
|
|
|
|
bool ResultBuilder::IsUnion(NamedDecl *ND) const {
|
|
|
|
// Allow us to find class templates, too.
|
|
|
|
if (ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND))
|
|
|
|
ND = ClassTemplate->getTemplatedDecl();
|
|
|
|
|
|
|
|
if (RecordDecl *RD = dyn_cast<RecordDecl>(ND))
|
|
|
|
return RD->getTagKind() == TagDecl::TK_union;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Determines whether the given declaration is a namespace.
|
|
|
|
bool ResultBuilder::IsNamespace(NamedDecl *ND) const {
|
|
|
|
return isa<NamespaceDecl>(ND);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Determines whether the given declaration is a namespace or
|
|
|
|
/// namespace alias.
|
|
|
|
bool ResultBuilder::IsNamespaceOrAlias(NamedDecl *ND) const {
|
|
|
|
return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Brief determines whether the given declaration is a namespace or
|
|
|
|
/// namespace alias.
|
|
|
|
bool ResultBuilder::IsType(NamedDecl *ND) const {
|
|
|
|
return isa<TypeDecl>(ND);
|
|
|
|
}
|
|
|
|
|
2009-09-24 06:26:46 +08:00
|
|
|
/// \brief Since every declaration found within a class is a member that we
|
|
|
|
/// care about, always returns true. This predicate exists mostly to
|
|
|
|
/// communicate to the result builder that we are performing a lookup for
|
|
|
|
/// member access.
|
|
|
|
bool ResultBuilder::IsMember(NamedDecl *ND) const {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-09-22 00:56:56 +08:00
|
|
|
// Find the next outer declaration context corresponding to this scope.
|
|
|
|
static DeclContext *findOuterContext(Scope *S) {
|
|
|
|
for (S = S->getParent(); S; S = S->getParent())
|
|
|
|
if (S->getEntity())
|
|
|
|
return static_cast<DeclContext *>(S->getEntity())->getPrimaryContext();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Collect the results of searching for members within the given
|
|
|
|
/// declaration context.
|
|
|
|
///
|
|
|
|
/// \param Ctx the declaration context from which we will gather results.
|
|
|
|
///
|
2009-09-23 07:15:58 +08:00
|
|
|
/// \param Rank the rank given to results in this declaration context.
|
2009-09-22 00:56:56 +08:00
|
|
|
///
|
|
|
|
/// \param Visited the set of declaration contexts that have already been
|
|
|
|
/// visited. Declaration contexts will only be visited once.
|
|
|
|
///
|
|
|
|
/// \param Results the result set that will be extended with any results
|
|
|
|
/// found within this declaration context (and, for a C++ class, its bases).
|
|
|
|
///
|
2009-09-23 07:15:58 +08:00
|
|
|
/// \param InBaseClass whether we are in a base class.
|
|
|
|
///
|
2009-09-22 00:56:56 +08:00
|
|
|
/// \returns the next higher rank value, after considering all of the
|
|
|
|
/// names within this declaration context.
|
|
|
|
static unsigned CollectMemberLookupResults(DeclContext *Ctx,
|
2009-09-23 07:15:58 +08:00
|
|
|
unsigned Rank,
|
2009-09-22 04:12:40 +08:00
|
|
|
DeclContext *CurContext,
|
|
|
|
llvm::SmallPtrSet<DeclContext *, 16> &Visited,
|
2009-09-23 07:15:58 +08:00
|
|
|
ResultBuilder &Results,
|
|
|
|
bool InBaseClass = false) {
|
2009-09-22 00:56:56 +08:00
|
|
|
// Make sure we don't visit the same context twice.
|
|
|
|
if (!Visited.insert(Ctx->getPrimaryContext()))
|
2009-09-23 07:15:58 +08:00
|
|
|
return Rank;
|
2009-09-22 00:56:56 +08:00
|
|
|
|
|
|
|
// Enumerate all of the results in this context.
|
2009-09-23 07:15:58 +08:00
|
|
|
typedef CodeCompleteConsumer::Result Result;
|
2009-09-22 00:56:56 +08:00
|
|
|
Results.EnterNewScope();
|
|
|
|
for (DeclContext *CurCtx = Ctx->getPrimaryContext(); CurCtx;
|
|
|
|
CurCtx = CurCtx->getNextContext()) {
|
|
|
|
for (DeclContext::decl_iterator D = CurCtx->decls_begin(),
|
2009-11-10 05:35:27 +08:00
|
|
|
DEnd = CurCtx->decls_end();
|
2009-09-22 00:56:56 +08:00
|
|
|
D != DEnd; ++D) {
|
|
|
|
if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
|
2009-09-23 07:15:58 +08:00
|
|
|
Results.MaybeAddResult(Result(ND, Rank, 0, InBaseClass), CurContext);
|
2009-11-10 05:35:27 +08:00
|
|
|
|
|
|
|
// Visit transparent contexts inside this context.
|
|
|
|
if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D)) {
|
|
|
|
if (InnerCtx->isTransparentContext())
|
|
|
|
CollectMemberLookupResults(InnerCtx, Rank, CurContext, Visited,
|
|
|
|
Results, InBaseClass);
|
|
|
|
}
|
2009-09-22 00:56:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Traverse the contexts of inherited classes.
|
|
|
|
if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx)) {
|
|
|
|
for (CXXRecordDecl::base_class_iterator B = Record->bases_begin(),
|
2009-11-10 05:35:27 +08:00
|
|
|
BEnd = Record->bases_end();
|
2009-09-22 00:56:56 +08:00
|
|
|
B != BEnd; ++B) {
|
|
|
|
QualType BaseType = B->getType();
|
|
|
|
|
|
|
|
// Don't look into dependent bases, because name lookup can't look
|
|
|
|
// there anyway.
|
|
|
|
if (BaseType->isDependentType())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const RecordType *Record = BaseType->getAs<RecordType>();
|
|
|
|
if (!Record)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// FIXME: It would be nice to be able to determine whether referencing
|
|
|
|
// a particular member would be ambiguous. For example, given
|
|
|
|
//
|
|
|
|
// struct A { int member; };
|
|
|
|
// struct B { int member; };
|
|
|
|
// struct C : A, B { };
|
|
|
|
//
|
|
|
|
// void f(C *c) { c->### }
|
|
|
|
// accessing 'member' would result in an ambiguity. However, code
|
|
|
|
// completion could be smart enough to qualify the member with the
|
|
|
|
// base class, e.g.,
|
|
|
|
//
|
|
|
|
// c->B::member
|
|
|
|
//
|
|
|
|
// or
|
|
|
|
//
|
|
|
|
// c->A::member
|
|
|
|
|
|
|
|
// Collect results from this base class (and its bases).
|
2009-09-23 07:15:58 +08:00
|
|
|
CollectMemberLookupResults(Record->getDecl(), Rank, CurContext, Visited,
|
|
|
|
Results, /*InBaseClass=*/true);
|
2009-09-22 00:56:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Look into base classes in Objective-C!
|
|
|
|
|
|
|
|
Results.ExitScope();
|
2009-09-23 07:15:58 +08:00
|
|
|
return Rank + 1;
|
2009-09-22 00:56:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Collect the results of searching for members within the given
|
|
|
|
/// declaration context.
|
|
|
|
///
|
|
|
|
/// \param Ctx the declaration context from which we will gather results.
|
|
|
|
///
|
|
|
|
/// \param InitialRank the initial rank given to results in this declaration
|
|
|
|
/// context. Larger rank values will be used for, e.g., members found in
|
|
|
|
/// base classes.
|
|
|
|
///
|
|
|
|
/// \param Results the result set that will be extended with any results
|
|
|
|
/// found within this declaration context (and, for a C++ class, its bases).
|
|
|
|
///
|
|
|
|
/// \returns the next higher rank value, after considering all of the
|
|
|
|
/// names within this declaration context.
|
|
|
|
static unsigned CollectMemberLookupResults(DeclContext *Ctx,
|
|
|
|
unsigned InitialRank,
|
2009-09-22 04:12:40 +08:00
|
|
|
DeclContext *CurContext,
|
2009-09-22 00:56:56 +08:00
|
|
|
ResultBuilder &Results) {
|
|
|
|
llvm::SmallPtrSet<DeclContext *, 16> Visited;
|
2009-09-22 04:12:40 +08:00
|
|
|
return CollectMemberLookupResults(Ctx, InitialRank, CurContext, Visited,
|
|
|
|
Results);
|
2009-09-22 00:56:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Collect the results of searching for declarations within the given
|
|
|
|
/// scope and its parent scopes.
|
|
|
|
///
|
|
|
|
/// \param S the scope in which we will start looking for declarations.
|
|
|
|
///
|
|
|
|
/// \param InitialRank the initial rank given to results in this scope.
|
|
|
|
/// Larger rank values will be used for results found in parent scopes.
|
|
|
|
///
|
2009-09-22 04:12:40 +08:00
|
|
|
/// \param CurContext the context from which lookup results will be found.
|
|
|
|
///
|
2009-09-22 00:56:56 +08:00
|
|
|
/// \param Results the builder object that will receive each result.
|
|
|
|
static unsigned CollectLookupResults(Scope *S,
|
|
|
|
TranslationUnitDecl *TranslationUnit,
|
|
|
|
unsigned InitialRank,
|
2009-09-22 04:12:40 +08:00
|
|
|
DeclContext *CurContext,
|
2009-09-22 00:56:56 +08:00
|
|
|
ResultBuilder &Results) {
|
|
|
|
if (!S)
|
|
|
|
return InitialRank;
|
|
|
|
|
|
|
|
// FIXME: Using directives!
|
|
|
|
|
|
|
|
unsigned NextRank = InitialRank;
|
|
|
|
Results.EnterNewScope();
|
|
|
|
if (S->getEntity() &&
|
|
|
|
!((DeclContext *)S->getEntity())->isFunctionOrMethod()) {
|
|
|
|
// Look into this scope's declaration context, along with any of its
|
|
|
|
// parent lookup contexts (e.g., enclosing classes), up to the point
|
|
|
|
// where we hit the context stored in the next outer scope.
|
|
|
|
DeclContext *Ctx = (DeclContext *)S->getEntity();
|
|
|
|
DeclContext *OuterCtx = findOuterContext(S);
|
|
|
|
|
|
|
|
for (; Ctx && Ctx->getPrimaryContext() != OuterCtx;
|
|
|
|
Ctx = Ctx->getLookupParent()) {
|
|
|
|
if (Ctx->isFunctionOrMethod())
|
|
|
|
continue;
|
|
|
|
|
2009-09-22 04:12:40 +08:00
|
|
|
NextRank = CollectMemberLookupResults(Ctx, NextRank + 1, CurContext,
|
|
|
|
Results);
|
2009-09-22 00:56:56 +08:00
|
|
|
}
|
|
|
|
} else if (!S->getParent()) {
|
|
|
|
// Look into the translation unit scope. We walk through the translation
|
|
|
|
// unit's declaration context, because the Scope itself won't have all of
|
|
|
|
// the declarations if we loaded a precompiled header.
|
|
|
|
// FIXME: We would like the translation unit's Scope object to point to the
|
|
|
|
// translation unit, so we don't need this special "if" branch. However,
|
|
|
|
// doing so would force the normal C++ name-lookup code to look into the
|
|
|
|
// translation unit decl when the IdentifierInfo chains would suffice.
|
|
|
|
// Once we fix that problem (which is part of a more general "don't look
|
|
|
|
// in DeclContexts unless we have to" optimization), we can eliminate the
|
|
|
|
// TranslationUnit parameter entirely.
|
|
|
|
NextRank = CollectMemberLookupResults(TranslationUnit, NextRank + 1,
|
2009-09-22 04:12:40 +08:00
|
|
|
CurContext, Results);
|
2009-09-22 00:56:56 +08:00
|
|
|
} else {
|
|
|
|
// Walk through the declarations in this Scope.
|
|
|
|
for (Scope::decl_iterator D = S->decl_begin(), DEnd = S->decl_end();
|
|
|
|
D != DEnd; ++D) {
|
|
|
|
if (NamedDecl *ND = dyn_cast<NamedDecl>((Decl *)((*D).get())))
|
2009-09-22 04:12:40 +08:00
|
|
|
Results.MaybeAddResult(CodeCompleteConsumer::Result(ND, NextRank),
|
|
|
|
CurContext);
|
2009-09-22 00:56:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NextRank = NextRank + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup names in the parent scope.
|
|
|
|
NextRank = CollectLookupResults(S->getParent(), TranslationUnit, NextRank,
|
2009-09-22 04:12:40 +08:00
|
|
|
CurContext, Results);
|
2009-09-22 00:56:56 +08:00
|
|
|
Results.ExitScope();
|
|
|
|
|
|
|
|
return NextRank;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Add type specifiers for the current language as keyword results.
|
2009-12-07 17:51:25 +08:00
|
|
|
static void AddTypeSpecifierResults(const LangOptions &LangOpts, unsigned Rank,
|
2009-09-22 00:56:56 +08:00
|
|
|
ResultBuilder &Results) {
|
|
|
|
typedef CodeCompleteConsumer::Result Result;
|
|
|
|
Results.MaybeAddResult(Result("short", Rank));
|
|
|
|
Results.MaybeAddResult(Result("long", Rank));
|
|
|
|
Results.MaybeAddResult(Result("signed", Rank));
|
|
|
|
Results.MaybeAddResult(Result("unsigned", Rank));
|
|
|
|
Results.MaybeAddResult(Result("void", Rank));
|
|
|
|
Results.MaybeAddResult(Result("char", Rank));
|
|
|
|
Results.MaybeAddResult(Result("int", Rank));
|
|
|
|
Results.MaybeAddResult(Result("float", Rank));
|
|
|
|
Results.MaybeAddResult(Result("double", Rank));
|
|
|
|
Results.MaybeAddResult(Result("enum", Rank));
|
|
|
|
Results.MaybeAddResult(Result("struct", Rank));
|
|
|
|
Results.MaybeAddResult(Result("union", Rank));
|
|
|
|
|
|
|
|
if (LangOpts.C99) {
|
|
|
|
// C99-specific
|
|
|
|
Results.MaybeAddResult(Result("_Complex", Rank));
|
|
|
|
Results.MaybeAddResult(Result("_Imaginary", Rank));
|
|
|
|
Results.MaybeAddResult(Result("_Bool", Rank));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LangOpts.CPlusPlus) {
|
|
|
|
// C++-specific
|
|
|
|
Results.MaybeAddResult(Result("bool", Rank));
|
|
|
|
Results.MaybeAddResult(Result("class", Rank));
|
|
|
|
Results.MaybeAddResult(Result("typename", Rank));
|
|
|
|
Results.MaybeAddResult(Result("wchar_t", Rank));
|
|
|
|
|
|
|
|
if (LangOpts.CPlusPlus0x) {
|
|
|
|
Results.MaybeAddResult(Result("char16_t", Rank));
|
|
|
|
Results.MaybeAddResult(Result("char32_t", Rank));
|
|
|
|
Results.MaybeAddResult(Result("decltype", Rank));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GNU extensions
|
|
|
|
if (LangOpts.GNUMode) {
|
|
|
|
// FIXME: Enable when we actually support decimal floating point.
|
|
|
|
// Results.MaybeAddResult(Result("_Decimal32", Rank));
|
|
|
|
// Results.MaybeAddResult(Result("_Decimal64", Rank));
|
|
|
|
// Results.MaybeAddResult(Result("_Decimal128", Rank));
|
|
|
|
Results.MaybeAddResult(Result("typeof", Rank));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Add function parameter chunks to the given code completion string.
|
|
|
|
static void AddFunctionParameterChunks(ASTContext &Context,
|
|
|
|
FunctionDecl *Function,
|
|
|
|
CodeCompletionString *Result) {
|
2009-11-07 08:00:49 +08:00
|
|
|
typedef CodeCompletionString::Chunk Chunk;
|
|
|
|
|
2009-09-22 00:56:56 +08:00
|
|
|
CodeCompletionString *CCStr = Result;
|
|
|
|
|
|
|
|
for (unsigned P = 0, N = Function->getNumParams(); P != N; ++P) {
|
|
|
|
ParmVarDecl *Param = Function->getParamDecl(P);
|
|
|
|
|
|
|
|
if (Param->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 (P != 0)
|
2009-11-07 08:00:49 +08:00
|
|
|
CCStr->AddChunk(Chunk(CodeCompletionString::CK_Comma));
|
2009-09-22 00:56:56 +08:00
|
|
|
|
|
|
|
// Format the placeholder string.
|
|
|
|
std::string PlaceholderStr;
|
|
|
|
if (Param->getIdentifier())
|
|
|
|
PlaceholderStr = Param->getIdentifier()->getName();
|
|
|
|
|
|
|
|
Param->getType().getAsStringInternal(PlaceholderStr,
|
|
|
|
Context.PrintingPolicy);
|
|
|
|
|
|
|
|
// Add the placeholder string.
|
2009-11-30 04:18:50 +08:00
|
|
|
CCStr->AddPlaceholderChunk(PlaceholderStr);
|
2009-09-22 00:56:56 +08:00
|
|
|
}
|
2009-09-23 05:42:17 +08:00
|
|
|
|
|
|
|
if (const FunctionProtoType *Proto
|
|
|
|
= Function->getType()->getAs<FunctionProtoType>())
|
|
|
|
if (Proto->isVariadic())
|
|
|
|
CCStr->AddPlaceholderChunk(", ...");
|
2009-09-22 00:56:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Add template parameter chunks to the given code completion string.
|
|
|
|
static void AddTemplateParameterChunks(ASTContext &Context,
|
|
|
|
TemplateDecl *Template,
|
|
|
|
CodeCompletionString *Result,
|
|
|
|
unsigned MaxParameters = 0) {
|
2009-11-07 08:00:49 +08:00
|
|
|
typedef CodeCompletionString::Chunk Chunk;
|
|
|
|
|
2009-09-22 00:56:56 +08:00
|
|
|
CodeCompletionString *CCStr = Result;
|
|
|
|
bool FirstParameter = true;
|
|
|
|
|
|
|
|
TemplateParameterList *Params = Template->getTemplateParameters();
|
|
|
|
TemplateParameterList::iterator PEnd = Params->end();
|
|
|
|
if (MaxParameters)
|
|
|
|
PEnd = Params->begin() + MaxParameters;
|
|
|
|
for (TemplateParameterList::iterator P = Params->begin(); 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
|
2009-11-07 08:00:49 +08:00
|
|
|
CCStr->AddChunk(Chunk(CodeCompletionString::CK_Comma));
|
2009-09-22 00:56:56 +08:00
|
|
|
|
|
|
|
// Add the placeholder string.
|
2009-11-30 04:18:50 +08:00
|
|
|
CCStr->AddPlaceholderChunk(PlaceholderStr);
|
2009-09-22 00:56:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-22 03:57:38 +08:00
|
|
|
/// \brief Add a qualifier to the given code-completion string, if the
|
|
|
|
/// provided nested-name-specifier is non-NULL.
|
|
|
|
void AddQualifierToCompletionString(CodeCompletionString *Result,
|
|
|
|
NestedNameSpecifier *Qualifier,
|
2009-09-23 07:15:58 +08:00
|
|
|
bool QualifierIsInformative,
|
2009-09-22 03:57:38 +08:00
|
|
|
ASTContext &Context) {
|
|
|
|
if (!Qualifier)
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::string PrintedNNS;
|
|
|
|
{
|
|
|
|
llvm::raw_string_ostream OS(PrintedNNS);
|
|
|
|
Qualifier->print(OS, Context.PrintingPolicy);
|
|
|
|
}
|
2009-09-23 07:15:58 +08:00
|
|
|
if (QualifierIsInformative)
|
2009-11-30 04:18:50 +08:00
|
|
|
Result->AddInformativeChunk(PrintedNNS);
|
2009-09-23 07:15:58 +08:00
|
|
|
else
|
2009-11-30 04:18:50 +08:00
|
|
|
Result->AddTextChunk(PrintedNNS);
|
2009-09-22 03:57:38 +08:00
|
|
|
}
|
|
|
|
|
2009-09-22 00:56:56 +08:00
|
|
|
/// \brief If possible, create a new code completion string for the given
|
|
|
|
/// result.
|
|
|
|
///
|
|
|
|
/// \returns Either a new, heap-allocated code completion string describing
|
|
|
|
/// how to use this result, or NULL to indicate that the string or name of the
|
|
|
|
/// result is all that is needed.
|
|
|
|
CodeCompletionString *
|
|
|
|
CodeCompleteConsumer::Result::CreateCodeCompletionString(Sema &S) {
|
2009-11-07 08:00:49 +08:00
|
|
|
typedef CodeCompletionString::Chunk Chunk;
|
|
|
|
|
2009-12-01 13:55:20 +08:00
|
|
|
if (Kind == RK_Pattern)
|
|
|
|
return Pattern->Clone();
|
|
|
|
|
|
|
|
CodeCompletionString *Result = new CodeCompletionString;
|
|
|
|
|
|
|
|
if (Kind == RK_Keyword) {
|
|
|
|
Result->AddTypedTextChunk(Keyword);
|
|
|
|
return Result;
|
|
|
|
}
|
2009-09-22 00:56:56 +08:00
|
|
|
|
2009-10-31 00:50:04 +08:00
|
|
|
if (Kind == RK_Macro) {
|
|
|
|
MacroInfo *MI = S.PP.getMacroInfo(Macro);
|
2009-12-01 13:55:20 +08:00
|
|
|
assert(MI && "Not a macro?");
|
|
|
|
|
|
|
|
Result->AddTypedTextChunk(Macro->getName());
|
|
|
|
|
|
|
|
if (!MI->isFunctionLike())
|
|
|
|
return Result;
|
2009-10-31 00:50:04 +08:00
|
|
|
|
|
|
|
// Format a function-like macro with placeholders for the arguments.
|
2009-11-07 08:00:49 +08:00
|
|
|
Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen));
|
2009-10-31 00:50:04 +08:00
|
|
|
for (MacroInfo::arg_iterator A = MI->arg_begin(), AEnd = MI->arg_end();
|
|
|
|
A != AEnd; ++A) {
|
|
|
|
if (A != MI->arg_begin())
|
2009-11-07 08:00:49 +08:00
|
|
|
Result->AddChunk(Chunk(CodeCompletionString::CK_Comma));
|
2009-10-31 00:50:04 +08:00
|
|
|
|
|
|
|
if (!MI->isVariadic() || A != AEnd - 1) {
|
|
|
|
// Non-variadic argument.
|
2009-11-30 04:18:50 +08:00
|
|
|
Result->AddPlaceholderChunk((*A)->getName());
|
2009-10-31 00:50:04 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Variadic argument; cope with the different between GNU and C99
|
|
|
|
// variadic macros, providing a single placeholder for the rest of the
|
|
|
|
// arguments.
|
|
|
|
if ((*A)->isStr("__VA_ARGS__"))
|
|
|
|
Result->AddPlaceholderChunk("...");
|
|
|
|
else {
|
|
|
|
std::string Arg = (*A)->getName();
|
|
|
|
Arg += "...";
|
2009-11-30 04:18:50 +08:00
|
|
|
Result->AddPlaceholderChunk(Arg);
|
2009-10-31 00:50:04 +08:00
|
|
|
}
|
|
|
|
}
|
2009-11-07 08:00:49 +08:00
|
|
|
Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen));
|
2009-10-31 00:50:04 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(Kind == RK_Declaration && "Missed a macro kind?");
|
2009-09-22 00:56:56 +08:00
|
|
|
NamedDecl *ND = Declaration;
|
|
|
|
|
2009-11-07 08:00:49 +08:00
|
|
|
if (StartsNestedNameSpecifier) {
|
2009-11-30 04:18:50 +08:00
|
|
|
Result->AddTypedTextChunk(ND->getNameAsString());
|
2009-11-07 08:00:49 +08:00
|
|
|
Result->AddTextChunk("::");
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2009-09-22 00:56:56 +08:00
|
|
|
if (FunctionDecl *Function = dyn_cast<FunctionDecl>(ND)) {
|
2009-09-23 07:15:58 +08:00
|
|
|
AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative,
|
|
|
|
S.Context);
|
2009-11-30 04:18:50 +08:00
|
|
|
Result->AddTypedTextChunk(Function->getNameAsString());
|
2009-11-07 08:00:49 +08:00
|
|
|
Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen));
|
2009-09-22 00:56:56 +08:00
|
|
|
AddFunctionParameterChunks(S.Context, Function, Result);
|
2009-11-07 08:00:49 +08:00
|
|
|
Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen));
|
2009-09-22 00:56:56 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(ND)) {
|
2009-09-23 07:15:58 +08:00
|
|
|
AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative,
|
|
|
|
S.Context);
|
2009-09-22 00:56:56 +08:00
|
|
|
FunctionDecl *Function = FunTmpl->getTemplatedDecl();
|
2009-11-30 04:18:50 +08:00
|
|
|
Result->AddTypedTextChunk(Function->getNameAsString());
|
2009-09-22 00:56:56 +08:00
|
|
|
|
|
|
|
// Figure out which template parameters are deduced (or have default
|
|
|
|
// arguments).
|
|
|
|
llvm::SmallVector<bool, 16> Deduced;
|
|
|
|
S.MarkDeducedTemplateParameters(FunTmpl, Deduced);
|
|
|
|
unsigned LastDeducibleArgument;
|
|
|
|
for (LastDeducibleArgument = Deduced.size(); LastDeducibleArgument > 0;
|
|
|
|
--LastDeducibleArgument) {
|
|
|
|
if (!Deduced[LastDeducibleArgument - 1]) {
|
|
|
|
// C++0x: Figure out if the template argument has a default. If so,
|
|
|
|
// the user doesn't need to type this argument.
|
|
|
|
// FIXME: We need to abstract template parameters better!
|
|
|
|
bool HasDefaultArg = false;
|
|
|
|
NamedDecl *Param = FunTmpl->getTemplateParameters()->getParam(
|
|
|
|
LastDeducibleArgument - 1);
|
|
|
|
if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
|
|
|
|
HasDefaultArg = TTP->hasDefaultArgument();
|
|
|
|
else if (NonTypeTemplateParmDecl *NTTP
|
|
|
|
= dyn_cast<NonTypeTemplateParmDecl>(Param))
|
|
|
|
HasDefaultArg = NTTP->hasDefaultArgument();
|
|
|
|
else {
|
|
|
|
assert(isa<TemplateTemplateParmDecl>(Param));
|
|
|
|
HasDefaultArg
|
2009-11-07 08:00:49 +08:00
|
|
|
= cast<TemplateTemplateParmDecl>(Param)->hasDefaultArgument();
|
2009-09-22 00:56:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!HasDefaultArg)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LastDeducibleArgument) {
|
|
|
|
// Some of the function template arguments cannot be deduced from a
|
|
|
|
// function call, so we introduce an explicit template argument list
|
|
|
|
// containing all of the arguments up to the first deducible argument.
|
2009-11-07 08:00:49 +08:00
|
|
|
Result->AddChunk(Chunk(CodeCompletionString::CK_LeftAngle));
|
2009-09-22 00:56:56 +08:00
|
|
|
AddTemplateParameterChunks(S.Context, FunTmpl, Result,
|
|
|
|
LastDeducibleArgument);
|
2009-11-07 08:00:49 +08:00
|
|
|
Result->AddChunk(Chunk(CodeCompletionString::CK_RightAngle));
|
2009-09-22 00:56:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add the function parameters
|
2009-11-07 08:00:49 +08:00
|
|
|
Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen));
|
2009-09-22 00:56:56 +08:00
|
|
|
AddFunctionParameterChunks(S.Context, Function, Result);
|
2009-11-07 08:00:49 +08:00
|
|
|
Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen));
|
2009-09-22 00:56:56 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TemplateDecl *Template = dyn_cast<TemplateDecl>(ND)) {
|
2009-09-23 07:15:58 +08:00
|
|
|
AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative,
|
|
|
|
S.Context);
|
2009-11-30 04:18:50 +08:00
|
|
|
Result->AddTypedTextChunk(Template->getNameAsString());
|
2009-11-07 08:00:49 +08:00
|
|
|
Result->AddChunk(Chunk(CodeCompletionString::CK_LeftAngle));
|
2009-09-22 00:56:56 +08:00
|
|
|
AddTemplateParameterChunks(S.Context, Template, Result);
|
2009-11-07 08:00:49 +08:00
|
|
|
Result->AddChunk(Chunk(CodeCompletionString::CK_RightAngle));
|
2009-09-22 00:56:56 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2009-11-18 00:44:22 +08:00
|
|
|
if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(ND)) {
|
|
|
|
Selector Sel = Method->getSelector();
|
|
|
|
if (Sel.isUnarySelector()) {
|
|
|
|
Result->AddTypedTextChunk(Sel.getIdentifierInfoForSlot(0)->getName());
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2009-11-19 09:08:35 +08:00
|
|
|
std::string SelName = Sel.getIdentifierInfoForSlot(0)->getName().str();
|
|
|
|
SelName += ':';
|
|
|
|
if (StartParameter == 0)
|
|
|
|
Result->AddTypedTextChunk(SelName);
|
|
|
|
else {
|
|
|
|
Result->AddInformativeChunk(SelName);
|
|
|
|
|
|
|
|
// If there is only one parameter, and we're past it, add an empty
|
|
|
|
// typed-text chunk since there is nothing to type.
|
|
|
|
if (Method->param_size() == 1)
|
|
|
|
Result->AddTypedTextChunk("");
|
|
|
|
}
|
2009-11-18 00:44:22 +08:00
|
|
|
unsigned Idx = 0;
|
|
|
|
for (ObjCMethodDecl::param_iterator P = Method->param_begin(),
|
|
|
|
PEnd = Method->param_end();
|
|
|
|
P != PEnd; (void)++P, ++Idx) {
|
|
|
|
if (Idx > 0) {
|
2009-11-19 09:08:35 +08:00
|
|
|
std::string Keyword;
|
|
|
|
if (Idx > StartParameter)
|
|
|
|
Keyword = " ";
|
2009-11-18 00:44:22 +08:00
|
|
|
if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(Idx))
|
|
|
|
Keyword += II->getName().str();
|
|
|
|
Keyword += ":";
|
2009-11-19 15:41:15 +08:00
|
|
|
if (Idx < StartParameter || AllParametersAreInformative) {
|
2009-11-19 09:08:35 +08:00
|
|
|
Result->AddInformativeChunk(Keyword);
|
|
|
|
} else if (Idx == StartParameter)
|
|
|
|
Result->AddTypedTextChunk(Keyword);
|
|
|
|
else
|
|
|
|
Result->AddTextChunk(Keyword);
|
2009-11-18 00:44:22 +08:00
|
|
|
}
|
2009-11-19 09:08:35 +08:00
|
|
|
|
|
|
|
// If we're before the starting parameter, skip the placeholder.
|
|
|
|
if (Idx < StartParameter)
|
|
|
|
continue;
|
2009-11-18 00:44:22 +08:00
|
|
|
|
|
|
|
std::string Arg;
|
|
|
|
(*P)->getType().getAsStringInternal(Arg, S.Context.PrintingPolicy);
|
|
|
|
Arg = "(" + Arg + ")";
|
|
|
|
if (IdentifierInfo *II = (*P)->getIdentifier())
|
|
|
|
Arg += II->getName().str();
|
2009-11-19 15:41:15 +08:00
|
|
|
if (AllParametersAreInformative)
|
|
|
|
Result->AddInformativeChunk(Arg);
|
|
|
|
else
|
|
|
|
Result->AddPlaceholderChunk(Arg);
|
2009-11-18 00:44:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2009-12-01 13:55:20 +08:00
|
|
|
if (Qualifier)
|
2009-09-23 07:15:58 +08:00
|
|
|
AddQualifierToCompletionString(Result, Qualifier, QualifierIsInformative,
|
|
|
|
S.Context);
|
2009-12-01 13:55:20 +08:00
|
|
|
|
|
|
|
Result->AddTypedTextChunk(ND->getNameAsString());
|
|
|
|
return Result;
|
2009-09-22 00:56:56 +08:00
|
|
|
}
|
|
|
|
|
2009-09-23 08:34:09 +08:00
|
|
|
CodeCompletionString *
|
|
|
|
CodeCompleteConsumer::OverloadCandidate::CreateSignatureString(
|
|
|
|
unsigned CurrentArg,
|
|
|
|
Sema &S) const {
|
2009-11-07 08:00:49 +08:00
|
|
|
typedef CodeCompletionString::Chunk Chunk;
|
|
|
|
|
2009-09-23 08:34:09 +08:00
|
|
|
CodeCompletionString *Result = new CodeCompletionString;
|
|
|
|
FunctionDecl *FDecl = getFunction();
|
|
|
|
const FunctionProtoType *Proto
|
|
|
|
= dyn_cast<FunctionProtoType>(getFunctionType());
|
|
|
|
if (!FDecl && !Proto) {
|
|
|
|
// Function without a prototype. Just give the return type and a
|
|
|
|
// highlighted ellipsis.
|
|
|
|
const FunctionType *FT = getFunctionType();
|
|
|
|
Result->AddTextChunk(
|
2009-11-30 04:18:50 +08:00
|
|
|
FT->getResultType().getAsString(S.Context.PrintingPolicy));
|
2009-11-07 08:00:49 +08:00
|
|
|
Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen));
|
|
|
|
Result->AddChunk(Chunk(CodeCompletionString::CK_CurrentParameter, "..."));
|
|
|
|
Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen));
|
2009-09-23 08:34:09 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FDecl)
|
2009-11-30 04:18:50 +08:00
|
|
|
Result->AddTextChunk(FDecl->getNameAsString());
|
2009-09-23 08:34:09 +08:00
|
|
|
else
|
|
|
|
Result->AddTextChunk(
|
2009-11-30 04:18:50 +08:00
|
|
|
Proto->getResultType().getAsString(S.Context.PrintingPolicy));
|
2009-09-23 08:34:09 +08:00
|
|
|
|
2009-11-07 08:00:49 +08:00
|
|
|
Result->AddChunk(Chunk(CodeCompletionString::CK_LeftParen));
|
2009-09-23 08:34:09 +08:00
|
|
|
unsigned NumParams = FDecl? FDecl->getNumParams() : Proto->getNumArgs();
|
|
|
|
for (unsigned I = 0; I != NumParams; ++I) {
|
|
|
|
if (I)
|
2009-11-07 08:00:49 +08:00
|
|
|
Result->AddChunk(Chunk(CodeCompletionString::CK_Comma));
|
2009-09-23 08:34:09 +08:00
|
|
|
|
|
|
|
std::string ArgString;
|
|
|
|
QualType ArgType;
|
|
|
|
|
|
|
|
if (FDecl) {
|
|
|
|
ArgString = FDecl->getParamDecl(I)->getNameAsString();
|
|
|
|
ArgType = FDecl->getParamDecl(I)->getOriginalType();
|
|
|
|
} else {
|
|
|
|
ArgType = Proto->getArgType(I);
|
|
|
|
}
|
|
|
|
|
|
|
|
ArgType.getAsStringInternal(ArgString, S.Context.PrintingPolicy);
|
|
|
|
|
|
|
|
if (I == CurrentArg)
|
2009-11-07 08:00:49 +08:00
|
|
|
Result->AddChunk(Chunk(CodeCompletionString::CK_CurrentParameter,
|
2009-11-30 04:18:50 +08:00
|
|
|
ArgString));
|
2009-09-23 08:34:09 +08:00
|
|
|
else
|
2009-11-30 04:18:50 +08:00
|
|
|
Result->AddTextChunk(ArgString);
|
2009-09-23 08:34:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Proto && Proto->isVariadic()) {
|
2009-11-07 08:00:49 +08:00
|
|
|
Result->AddChunk(Chunk(CodeCompletionString::CK_Comma));
|
2009-09-23 08:34:09 +08:00
|
|
|
if (CurrentArg < NumParams)
|
|
|
|
Result->AddTextChunk("...");
|
|
|
|
else
|
2009-11-07 08:00:49 +08:00
|
|
|
Result->AddChunk(Chunk(CodeCompletionString::CK_CurrentParameter, "..."));
|
2009-09-23 08:34:09 +08:00
|
|
|
}
|
2009-11-07 08:00:49 +08:00
|
|
|
Result->AddChunk(Chunk(CodeCompletionString::CK_RightParen));
|
2009-09-23 08:34:09 +08:00
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2009-09-22 00:56:56 +08:00
|
|
|
namespace {
|
|
|
|
struct SortCodeCompleteResult {
|
|
|
|
typedef CodeCompleteConsumer::Result Result;
|
|
|
|
|
2009-09-28 11:51:44 +08:00
|
|
|
bool isEarlierDeclarationName(DeclarationName X, DeclarationName Y) const {
|
2009-12-05 17:08:56 +08:00
|
|
|
Selector XSel = X.getObjCSelector();
|
|
|
|
Selector YSel = Y.getObjCSelector();
|
|
|
|
if (!XSel.isNull() && !YSel.isNull()) {
|
|
|
|
// We are comparing two selectors.
|
|
|
|
unsigned N = std::min(XSel.getNumArgs(), YSel.getNumArgs());
|
|
|
|
if (N == 0)
|
|
|
|
++N;
|
|
|
|
for (unsigned I = 0; I != N; ++I) {
|
|
|
|
IdentifierInfo *XId = XSel.getIdentifierInfoForSlot(I);
|
|
|
|
IdentifierInfo *YId = YSel.getIdentifierInfoForSlot(I);
|
|
|
|
if (!XId || !YId)
|
|
|
|
return XId && !YId;
|
|
|
|
|
|
|
|
switch (XId->getName().compare_lower(YId->getName())) {
|
|
|
|
case -1: return true;
|
|
|
|
case 1: return false;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return XSel.getNumArgs() < YSel.getNumArgs();
|
|
|
|
}
|
|
|
|
|
|
|
|
// For non-selectors, order by kind.
|
|
|
|
if (X.getNameKind() != Y.getNameKind())
|
2009-09-28 11:51:44 +08:00
|
|
|
return X.getNameKind() < Y.getNameKind();
|
|
|
|
|
2009-12-05 17:08:56 +08:00
|
|
|
// Order identifiers by comparison of their lowercased names.
|
|
|
|
if (IdentifierInfo *XId = X.getAsIdentifierInfo())
|
|
|
|
return XId->getName().compare_lower(
|
|
|
|
Y.getAsIdentifierInfo()->getName()) < 0;
|
|
|
|
|
|
|
|
// Order overloaded operators by the order in which they appear
|
|
|
|
// in our list of operators.
|
|
|
|
if (OverloadedOperatorKind XOp = X.getCXXOverloadedOperator())
|
|
|
|
return XOp < Y.getCXXOverloadedOperator();
|
|
|
|
|
|
|
|
// Order C++0x user-defined literal operators lexically by their
|
|
|
|
// lowercased suffixes.
|
|
|
|
if (IdentifierInfo *XLit = X.getCXXLiteralIdentifier())
|
|
|
|
return XLit->getName().compare_lower(
|
|
|
|
Y.getCXXLiteralIdentifier()->getName()) < 0;
|
|
|
|
|
|
|
|
// The only stable ordering we have is to turn the name into a
|
|
|
|
// string and then compare the lower-case strings. This is
|
|
|
|
// inefficient, but thankfully does not happen too often.
|
2009-12-05 18:22:15 +08:00
|
|
|
return llvm::StringRef(X.getAsString()).compare_lower(
|
|
|
|
Y.getAsString()) < 0;
|
2009-09-28 11:51:44 +08:00
|
|
|
}
|
|
|
|
|
2009-09-22 00:56:56 +08:00
|
|
|
bool operator()(const Result &X, const Result &Y) const {
|
|
|
|
// Sort first by rank.
|
|
|
|
if (X.Rank < Y.Rank)
|
|
|
|
return true;
|
|
|
|
else if (X.Rank > Y.Rank)
|
|
|
|
return false;
|
|
|
|
|
2009-11-19 08:01:57 +08:00
|
|
|
// We use a special ordering for keywords and patterns, based on the
|
|
|
|
// typed text.
|
|
|
|
if ((X.Kind == Result::RK_Keyword || X.Kind == Result::RK_Pattern) &&
|
|
|
|
(Y.Kind == Result::RK_Keyword || Y.Kind == Result::RK_Pattern)) {
|
|
|
|
const char *XStr = (X.Kind == Result::RK_Keyword)? X.Keyword
|
|
|
|
: X.Pattern->getTypedText();
|
|
|
|
const char *YStr = (Y.Kind == Result::RK_Keyword)? Y.Keyword
|
|
|
|
: Y.Pattern->getTypedText();
|
2009-12-05 18:07:04 +08:00
|
|
|
return llvm::StringRef(XStr).compare_lower(YStr) < 0;
|
2009-11-19 08:01:57 +08:00
|
|
|
}
|
|
|
|
|
2009-09-22 00:56:56 +08:00
|
|
|
// Result kinds are ordered by decreasing importance.
|
|
|
|
if (X.Kind < Y.Kind)
|
|
|
|
return true;
|
|
|
|
else if (X.Kind > Y.Kind)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Non-hidden names precede hidden names.
|
|
|
|
if (X.Hidden != Y.Hidden)
|
|
|
|
return !X.Hidden;
|
|
|
|
|
2009-09-24 06:26:46 +08:00
|
|
|
// Non-nested-name-specifiers precede nested-name-specifiers.
|
|
|
|
if (X.StartsNestedNameSpecifier != Y.StartsNestedNameSpecifier)
|
|
|
|
return !X.StartsNestedNameSpecifier;
|
|
|
|
|
2009-09-22 00:56:56 +08:00
|
|
|
// Ordering depends on the kind of result.
|
|
|
|
switch (X.Kind) {
|
|
|
|
case Result::RK_Declaration:
|
|
|
|
// Order based on the declaration names.
|
2009-09-28 11:51:44 +08:00
|
|
|
return isEarlierDeclarationName(X.Declaration->getDeclName(),
|
|
|
|
Y.Declaration->getDeclName());
|
2009-09-22 00:56:56 +08:00
|
|
|
|
2009-10-31 00:50:04 +08:00
|
|
|
case Result::RK_Macro:
|
2009-12-05 17:08:56 +08:00
|
|
|
return X.Macro->getName().compare_lower(Y.Macro->getName()) < 0;
|
2009-11-19 08:01:57 +08:00
|
|
|
|
|
|
|
case Result::RK_Keyword:
|
|
|
|
case Result::RK_Pattern:
|
|
|
|
llvm::llvm_unreachable("Result kinds handled above");
|
|
|
|
break;
|
2009-09-22 00:56:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Silence GCC warning.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2009-10-31 00:50:04 +08:00
|
|
|
static void AddMacroResults(Preprocessor &PP, unsigned Rank,
|
|
|
|
ResultBuilder &Results) {
|
|
|
|
Results.EnterNewScope();
|
2009-11-07 08:00:49 +08:00
|
|
|
for (Preprocessor::macro_iterator M = PP.macro_begin(),
|
|
|
|
MEnd = PP.macro_end();
|
2009-10-31 00:50:04 +08:00
|
|
|
M != MEnd; ++M)
|
|
|
|
Results.MaybeAddResult(CodeCompleteConsumer::Result(M->first, Rank));
|
|
|
|
Results.ExitScope();
|
|
|
|
}
|
|
|
|
|
2009-11-13 16:58:20 +08:00
|
|
|
static void HandleCodeCompleteResults(Sema *S,
|
|
|
|
CodeCompleteConsumer *CodeCompleter,
|
|
|
|
CodeCompleteConsumer::Result *Results,
|
|
|
|
unsigned NumResults) {
|
2009-09-22 00:56:56 +08:00
|
|
|
// Sort the results by rank/kind/etc.
|
|
|
|
std::stable_sort(Results, Results + NumResults, SortCodeCompleteResult());
|
|
|
|
|
|
|
|
if (CodeCompleter)
|
2009-11-13 16:58:20 +08:00
|
|
|
CodeCompleter->ProcessCodeCompleteResults(*S, Results, NumResults);
|
2009-11-19 08:01:57 +08:00
|
|
|
|
|
|
|
for (unsigned I = 0; I != NumResults; ++I)
|
|
|
|
Results[I].Destroy();
|
2009-09-22 00:56:56 +08:00
|
|
|
}
|
|
|
|
|
2009-09-22 04:51:25 +08:00
|
|
|
void Sema::CodeCompleteOrdinaryName(Scope *S) {
|
|
|
|
ResultBuilder Results(*this, &ResultBuilder::IsOrdinaryName);
|
2009-10-31 00:50:04 +08:00
|
|
|
unsigned NextRank = CollectLookupResults(S, Context.getTranslationUnitDecl(),
|
|
|
|
0, CurContext, Results);
|
2009-11-07 08:00:49 +08:00
|
|
|
if (CodeCompleter->includeMacros())
|
|
|
|
AddMacroResults(PP, NextRank, Results);
|
2009-11-13 16:58:20 +08:00
|
|
|
HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
|
2009-09-22 04:51:25 +08:00
|
|
|
}
|
|
|
|
|
2009-11-18 09:29:26 +08:00
|
|
|
static void AddObjCProperties(ObjCContainerDecl *Container,
|
2009-11-19 06:32:06 +08:00
|
|
|
bool AllowCategories,
|
2009-11-18 09:29:26 +08:00
|
|
|
DeclContext *CurContext,
|
|
|
|
ResultBuilder &Results) {
|
|
|
|
typedef CodeCompleteConsumer::Result Result;
|
|
|
|
|
|
|
|
// Add properties in this container.
|
|
|
|
for (ObjCContainerDecl::prop_iterator P = Container->prop_begin(),
|
|
|
|
PEnd = Container->prop_end();
|
|
|
|
P != PEnd;
|
|
|
|
++P)
|
|
|
|
Results.MaybeAddResult(Result(*P, 0), CurContext);
|
|
|
|
|
|
|
|
// Add properties in referenced protocols.
|
|
|
|
if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
|
|
|
|
for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
|
|
|
|
PEnd = Protocol->protocol_end();
|
|
|
|
P != PEnd; ++P)
|
2009-11-19 06:32:06 +08:00
|
|
|
AddObjCProperties(*P, AllowCategories, CurContext, Results);
|
2009-11-18 09:29:26 +08:00
|
|
|
} else if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container)){
|
2009-11-19 06:32:06 +08:00
|
|
|
if (AllowCategories) {
|
|
|
|
// Look through categories.
|
|
|
|
for (ObjCCategoryDecl *Category = IFace->getCategoryList();
|
|
|
|
Category; Category = Category->getNextClassCategory())
|
|
|
|
AddObjCProperties(Category, AllowCategories, CurContext, Results);
|
|
|
|
}
|
2009-11-18 09:29:26 +08:00
|
|
|
|
|
|
|
// Look through protocols.
|
|
|
|
for (ObjCInterfaceDecl::protocol_iterator I = IFace->protocol_begin(),
|
|
|
|
E = IFace->protocol_end();
|
|
|
|
I != E; ++I)
|
2009-11-19 06:32:06 +08:00
|
|
|
AddObjCProperties(*I, AllowCategories, CurContext, Results);
|
2009-11-18 09:29:26 +08:00
|
|
|
|
|
|
|
// Look in the superclass.
|
|
|
|
if (IFace->getSuperClass())
|
2009-11-19 06:32:06 +08:00
|
|
|
AddObjCProperties(IFace->getSuperClass(), AllowCategories, CurContext,
|
|
|
|
Results);
|
2009-11-18 09:29:26 +08:00
|
|
|
} else if (const ObjCCategoryDecl *Category
|
|
|
|
= dyn_cast<ObjCCategoryDecl>(Container)) {
|
|
|
|
// Look through protocols.
|
|
|
|
for (ObjCInterfaceDecl::protocol_iterator P = Category->protocol_begin(),
|
|
|
|
PEnd = Category->protocol_end();
|
|
|
|
P != PEnd; ++P)
|
2009-11-19 06:32:06 +08:00
|
|
|
AddObjCProperties(*P, AllowCategories, CurContext, Results);
|
2009-11-18 09:29:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Initial implementation of a code-completion interface in Clang. In
essence, code completion is triggered by a magic "code completion"
token produced by the lexer [*], which the parser recognizes at
certain points in the grammar. The parser then calls into the Action
object with the appropriate CodeCompletionXXX action.
Sema implements the CodeCompletionXXX callbacks by performing minimal
translation, then forwarding them to a CodeCompletionConsumer
subclass, which uses the results of semantic analysis to provide
code-completion results. At present, only a single, "printing" code
completion consumer is available, for regression testing and
debugging. However, the design is meant to permit other
code-completion consumers.
This initial commit contains two code-completion actions: one for
member access, e.g., "x." or "p->", and one for
nested-name-specifiers, e.g., "std::". More code-completion actions
will follow, along with improved gathering of code-completion results
for the various contexts.
[*] In the current -code-completion-dump testing/debugging mode, the
file is truncated at the completion point and EOF is translated into
"code completion".
llvm-svn: 82166
2009-09-18 05:32:03 +08:00
|
|
|
void Sema::CodeCompleteMemberReferenceExpr(Scope *S, ExprTy *BaseE,
|
|
|
|
SourceLocation OpLoc,
|
|
|
|
bool IsArrow) {
|
|
|
|
if (!BaseE || !CodeCompleter)
|
|
|
|
return;
|
|
|
|
|
2009-09-22 00:56:56 +08:00
|
|
|
typedef CodeCompleteConsumer::Result Result;
|
|
|
|
|
Initial implementation of a code-completion interface in Clang. In
essence, code completion is triggered by a magic "code completion"
token produced by the lexer [*], which the parser recognizes at
certain points in the grammar. The parser then calls into the Action
object with the appropriate CodeCompletionXXX action.
Sema implements the CodeCompletionXXX callbacks by performing minimal
translation, then forwarding them to a CodeCompletionConsumer
subclass, which uses the results of semantic analysis to provide
code-completion results. At present, only a single, "printing" code
completion consumer is available, for regression testing and
debugging. However, the design is meant to permit other
code-completion consumers.
This initial commit contains two code-completion actions: one for
member access, e.g., "x." or "p->", and one for
nested-name-specifiers, e.g., "std::". More code-completion actions
will follow, along with improved gathering of code-completion results
for the various contexts.
[*] In the current -code-completion-dump testing/debugging mode, the
file is truncated at the completion point and EOF is translated into
"code completion".
llvm-svn: 82166
2009-09-18 05:32:03 +08:00
|
|
|
Expr *Base = static_cast<Expr *>(BaseE);
|
|
|
|
QualType BaseType = Base->getType();
|
2009-09-22 00:56:56 +08:00
|
|
|
|
|
|
|
if (IsArrow) {
|
|
|
|
if (const PointerType *Ptr = BaseType->getAs<PointerType>())
|
|
|
|
BaseType = Ptr->getPointeeType();
|
|
|
|
else if (BaseType->isObjCObjectPointerType())
|
|
|
|
/*Do nothing*/ ;
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-09-24 06:26:46 +08:00
|
|
|
ResultBuilder Results(*this, &ResultBuilder::IsMember);
|
2009-09-22 00:56:56 +08:00
|
|
|
unsigned NextRank = 0;
|
2009-11-13 16:58:20 +08:00
|
|
|
|
2009-11-18 09:29:26 +08:00
|
|
|
Results.EnterNewScope();
|
|
|
|
if (const RecordType *Record = BaseType->getAs<RecordType>()) {
|
|
|
|
// Access to a C/C++ class, struct, or union.
|
|
|
|
NextRank = CollectMemberLookupResults(Record->getDecl(), NextRank,
|
|
|
|
Record->getDecl(), Results);
|
2009-11-13 16:58:20 +08:00
|
|
|
|
2009-11-18 09:29:26 +08:00
|
|
|
if (getLangOptions().CPlusPlus) {
|
|
|
|
if (!Results.empty()) {
|
|
|
|
// The "template" keyword can follow "->" or "." in the grammar.
|
|
|
|
// However, we only want to suggest the template keyword if something
|
|
|
|
// is dependent.
|
|
|
|
bool IsDependent = BaseType->isDependentType();
|
|
|
|
if (!IsDependent) {
|
|
|
|
for (Scope *DepScope = S; DepScope; DepScope = DepScope->getParent())
|
|
|
|
if (DeclContext *Ctx = (DeclContext *)DepScope->getEntity()) {
|
|
|
|
IsDependent = Ctx->isDependentContext();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-11-13 16:58:20 +08:00
|
|
|
|
2009-11-18 09:29:26 +08:00
|
|
|
if (IsDependent)
|
|
|
|
Results.MaybeAddResult(Result("template", NextRank++));
|
2009-09-22 00:56:56 +08:00
|
|
|
}
|
2009-11-13 16:58:20 +08:00
|
|
|
|
2009-11-18 09:29:26 +08:00
|
|
|
// We could have the start of a nested-name-specifier. Add those
|
|
|
|
// results as well.
|
|
|
|
Results.setFilter(&ResultBuilder::IsNestedNameSpecifier);
|
|
|
|
CollectLookupResults(S, Context.getTranslationUnitDecl(), NextRank,
|
|
|
|
CurContext, Results);
|
|
|
|
}
|
|
|
|
} else if (!IsArrow && BaseType->getAsObjCInterfacePointerType()) {
|
|
|
|
// Objective-C property reference.
|
|
|
|
|
|
|
|
// Add property results based on our interface.
|
|
|
|
const ObjCObjectPointerType *ObjCPtr
|
|
|
|
= BaseType->getAsObjCInterfacePointerType();
|
|
|
|
assert(ObjCPtr && "Non-NULL pointer guaranteed above!");
|
2009-11-19 06:32:06 +08:00
|
|
|
AddObjCProperties(ObjCPtr->getInterfaceDecl(), true, CurContext, Results);
|
2009-11-18 09:29:26 +08:00
|
|
|
|
|
|
|
// Add properties from the protocols in a qualified interface.
|
|
|
|
for (ObjCObjectPointerType::qual_iterator I = ObjCPtr->qual_begin(),
|
|
|
|
E = ObjCPtr->qual_end();
|
|
|
|
I != E; ++I)
|
2009-11-19 06:32:06 +08:00
|
|
|
AddObjCProperties(*I, true, CurContext, Results);
|
2009-11-18 09:29:26 +08:00
|
|
|
|
|
|
|
// FIXME: We could (should?) also look for "implicit" properties, identified
|
|
|
|
// only by the presence of nullary and unary selectors.
|
|
|
|
} else if ((IsArrow && BaseType->isObjCObjectPointerType()) ||
|
|
|
|
(!IsArrow && BaseType->isObjCInterfaceType())) {
|
|
|
|
// Objective-C instance variable access.
|
|
|
|
ObjCInterfaceDecl *Class = 0;
|
|
|
|
if (const ObjCObjectPointerType *ObjCPtr
|
|
|
|
= BaseType->getAs<ObjCObjectPointerType>())
|
|
|
|
Class = ObjCPtr->getInterfaceDecl();
|
|
|
|
else
|
|
|
|
Class = BaseType->getAs<ObjCInterfaceType>()->getDecl();
|
|
|
|
|
|
|
|
// Add all ivars from this class and its superclasses.
|
|
|
|
for (; Class; Class = Class->getSuperClass()) {
|
|
|
|
for (ObjCInterfaceDecl::ivar_iterator IVar = Class->ivar_begin(),
|
|
|
|
IVarEnd = Class->ivar_end();
|
|
|
|
IVar != IVarEnd; ++IVar)
|
|
|
|
Results.MaybeAddResult(Result(*IVar, 0), CurContext);
|
2009-09-22 00:56:56 +08:00
|
|
|
}
|
|
|
|
}
|
2009-11-18 09:29:26 +08:00
|
|
|
|
|
|
|
// FIXME: How do we cope with isa?
|
|
|
|
|
|
|
|
Results.ExitScope();
|
2009-11-13 16:58:20 +08:00
|
|
|
|
|
|
|
// Add macros
|
|
|
|
if (CodeCompleter->includeMacros())
|
|
|
|
AddMacroResults(PP, NextRank, Results);
|
|
|
|
|
|
|
|
// Hand off the results found for code completion.
|
|
|
|
HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
|
Initial implementation of a code-completion interface in Clang. In
essence, code completion is triggered by a magic "code completion"
token produced by the lexer [*], which the parser recognizes at
certain points in the grammar. The parser then calls into the Action
object with the appropriate CodeCompletionXXX action.
Sema implements the CodeCompletionXXX callbacks by performing minimal
translation, then forwarding them to a CodeCompletionConsumer
subclass, which uses the results of semantic analysis to provide
code-completion results. At present, only a single, "printing" code
completion consumer is available, for regression testing and
debugging. However, the design is meant to permit other
code-completion consumers.
This initial commit contains two code-completion actions: one for
member access, e.g., "x." or "p->", and one for
nested-name-specifiers, e.g., "std::". More code-completion actions
will follow, along with improved gathering of code-completion results
for the various contexts.
[*] In the current -code-completion-dump testing/debugging mode, the
file is truncated at the completion point and EOF is translated into
"code completion".
llvm-svn: 82166
2009-09-18 05:32:03 +08:00
|
|
|
}
|
|
|
|
|
2009-09-18 23:37:17 +08:00
|
|
|
void Sema::CodeCompleteTag(Scope *S, unsigned TagSpec) {
|
|
|
|
if (!CodeCompleter)
|
|
|
|
return;
|
|
|
|
|
2009-09-22 00:56:56 +08:00
|
|
|
typedef CodeCompleteConsumer::Result Result;
|
|
|
|
ResultBuilder::LookupFilter Filter = 0;
|
2009-09-18 23:37:17 +08:00
|
|
|
switch ((DeclSpec::TST)TagSpec) {
|
|
|
|
case DeclSpec::TST_enum:
|
2009-09-22 00:56:56 +08:00
|
|
|
Filter = &ResultBuilder::IsEnum;
|
2009-09-18 23:37:17 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DeclSpec::TST_union:
|
2009-09-22 00:56:56 +08:00
|
|
|
Filter = &ResultBuilder::IsUnion;
|
2009-09-18 23:37:17 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DeclSpec::TST_struct:
|
|
|
|
case DeclSpec::TST_class:
|
2009-09-22 00:56:56 +08:00
|
|
|
Filter = &ResultBuilder::IsClassOrStruct;
|
2009-09-18 23:37:17 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(false && "Unknown type specifier kind in CodeCompleteTag");
|
|
|
|
return;
|
|
|
|
}
|
2009-09-22 00:56:56 +08:00
|
|
|
|
|
|
|
ResultBuilder Results(*this, Filter);
|
|
|
|
unsigned NextRank = CollectLookupResults(S, Context.getTranslationUnitDecl(),
|
2009-09-22 04:12:40 +08:00
|
|
|
0, CurContext, Results);
|
2009-09-22 00:56:56 +08:00
|
|
|
|
|
|
|
if (getLangOptions().CPlusPlus) {
|
|
|
|
// We could have the start of a nested-name-specifier. Add those
|
|
|
|
// results as well.
|
|
|
|
Results.setFilter(&ResultBuilder::IsNestedNameSpecifier);
|
2009-10-31 00:50:04 +08:00
|
|
|
NextRank = CollectLookupResults(S, Context.getTranslationUnitDecl(),
|
|
|
|
NextRank, CurContext, Results);
|
2009-09-22 00:56:56 +08:00
|
|
|
}
|
|
|
|
|
2009-11-07 08:00:49 +08:00
|
|
|
if (CodeCompleter->includeMacros())
|
|
|
|
AddMacroResults(PP, NextRank, Results);
|
2009-11-13 16:58:20 +08:00
|
|
|
HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
|
2009-09-18 23:37:17 +08:00
|
|
|
}
|
|
|
|
|
2009-09-22 02:10:23 +08:00
|
|
|
void Sema::CodeCompleteCase(Scope *S) {
|
|
|
|
if (getSwitchStack().empty() || !CodeCompleter)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SwitchStmt *Switch = getSwitchStack().back();
|
|
|
|
if (!Switch->getCond()->getType()->isEnumeralType())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Code-complete the cases of a switch statement over an enumeration type
|
|
|
|
// by providing the list of
|
|
|
|
EnumDecl *Enum = Switch->getCond()->getType()->getAs<EnumType>()->getDecl();
|
|
|
|
|
|
|
|
// Determine which enumerators we have already seen in the switch statement.
|
|
|
|
// FIXME: Ideally, we would also be able to look *past* the code-completion
|
|
|
|
// token, in case we are code-completing in the middle of the switch and not
|
|
|
|
// at the end. However, we aren't able to do so at the moment.
|
|
|
|
llvm::SmallPtrSet<EnumConstantDecl *, 8> EnumeratorsSeen;
|
2009-09-22 03:57:38 +08:00
|
|
|
NestedNameSpecifier *Qualifier = 0;
|
2009-09-22 02:10:23 +08:00
|
|
|
for (SwitchCase *SC = Switch->getSwitchCaseList(); SC;
|
|
|
|
SC = SC->getNextSwitchCase()) {
|
|
|
|
CaseStmt *Case = dyn_cast<CaseStmt>(SC);
|
|
|
|
if (!Case)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Expr *CaseVal = Case->getLHS()->IgnoreParenCasts();
|
|
|
|
if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CaseVal))
|
|
|
|
if (EnumConstantDecl *Enumerator
|
|
|
|
= dyn_cast<EnumConstantDecl>(DRE->getDecl())) {
|
|
|
|
// We look into the AST of the case statement to determine which
|
|
|
|
// enumerator was named. Alternatively, we could compute the value of
|
|
|
|
// the integral constant expression, then compare it against the
|
|
|
|
// values of each enumerator. However, value-based approach would not
|
|
|
|
// work as well with C++ templates where enumerators declared within a
|
|
|
|
// template are type- and value-dependent.
|
|
|
|
EnumeratorsSeen.insert(Enumerator);
|
|
|
|
|
2009-09-22 03:57:38 +08:00
|
|
|
// If this is a qualified-id, keep track of the nested-name-specifier
|
|
|
|
// so that we can reproduce it as part of code completion, e.g.,
|
2009-09-22 02:10:23 +08:00
|
|
|
//
|
|
|
|
// switch (TagD.getKind()) {
|
|
|
|
// case TagDecl::TK_enum:
|
|
|
|
// break;
|
|
|
|
// case XXX
|
|
|
|
//
|
2009-09-22 03:57:38 +08:00
|
|
|
// At the XXX, our completions are TagDecl::TK_union,
|
2009-09-22 02:10:23 +08:00
|
|
|
// TagDecl::TK_struct, and TagDecl::TK_class, rather than TK_union,
|
|
|
|
// TK_struct, and TK_class.
|
2009-10-24 02:54:35 +08:00
|
|
|
Qualifier = DRE->getQualifier();
|
2009-09-22 02:10:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-22 03:57:38 +08:00
|
|
|
if (getLangOptions().CPlusPlus && !Qualifier && EnumeratorsSeen.empty()) {
|
|
|
|
// If there are no prior enumerators in C++, check whether we have to
|
|
|
|
// qualify the names of the enumerators that we suggest, because they
|
|
|
|
// may not be visible in this scope.
|
|
|
|
Qualifier = getRequiredQualification(Context, CurContext,
|
|
|
|
Enum->getDeclContext());
|
|
|
|
|
|
|
|
// FIXME: Scoped enums need to start with "EnumDecl" as the context!
|
|
|
|
}
|
|
|
|
|
2009-09-22 02:10:23 +08:00
|
|
|
// Add any enumerators that have not yet been mentioned.
|
|
|
|
ResultBuilder Results(*this);
|
|
|
|
Results.EnterNewScope();
|
|
|
|
for (EnumDecl::enumerator_iterator E = Enum->enumerator_begin(),
|
|
|
|
EEnd = Enum->enumerator_end();
|
|
|
|
E != EEnd; ++E) {
|
|
|
|
if (EnumeratorsSeen.count(*E))
|
|
|
|
continue;
|
|
|
|
|
2009-09-22 03:57:38 +08:00
|
|
|
Results.MaybeAddResult(CodeCompleteConsumer::Result(*E, 0, Qualifier));
|
2009-09-22 02:10:23 +08:00
|
|
|
}
|
|
|
|
Results.ExitScope();
|
|
|
|
|
2009-11-07 08:00:49 +08:00
|
|
|
if (CodeCompleter->includeMacros())
|
|
|
|
AddMacroResults(PP, 1, Results);
|
2009-11-13 16:58:20 +08:00
|
|
|
HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
|
2009-09-22 02:10:23 +08:00
|
|
|
}
|
|
|
|
|
2009-09-22 23:41:20 +08:00
|
|
|
namespace {
|
|
|
|
struct IsBetterOverloadCandidate {
|
|
|
|
Sema &S;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit IsBetterOverloadCandidate(Sema &S) : S(S) { }
|
|
|
|
|
|
|
|
bool
|
|
|
|
operator()(const OverloadCandidate &X, const OverloadCandidate &Y) const {
|
|
|
|
return S.isBetterOverloadCandidate(X, Y);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sema::CodeCompleteCall(Scope *S, ExprTy *FnIn,
|
|
|
|
ExprTy **ArgsIn, unsigned NumArgs) {
|
|
|
|
if (!CodeCompleter)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Expr *Fn = (Expr *)FnIn;
|
|
|
|
Expr **Args = (Expr **)ArgsIn;
|
|
|
|
|
|
|
|
// Ignore type-dependent call expressions entirely.
|
|
|
|
if (Fn->isTypeDependent() ||
|
|
|
|
Expr::hasAnyTypeDependentArguments(Args, NumArgs))
|
|
|
|
return;
|
|
|
|
|
2009-11-21 16:51:07 +08:00
|
|
|
llvm::SmallVector<NamedDecl*,8> Fns;
|
2009-09-22 23:41:20 +08:00
|
|
|
DeclarationName UnqualifiedName;
|
|
|
|
NestedNameSpecifier *Qualifier;
|
|
|
|
SourceRange QualifierRange;
|
|
|
|
bool ArgumentDependentLookup;
|
2009-11-22 08:44:51 +08:00
|
|
|
bool Overloaded;
|
2009-09-22 23:41:20 +08:00
|
|
|
bool HasExplicitTemplateArgs;
|
2009-11-23 09:53:49 +08:00
|
|
|
TemplateArgumentListInfo ExplicitTemplateArgs;
|
2009-09-22 23:41:20 +08:00
|
|
|
|
2009-11-21 16:51:07 +08:00
|
|
|
DeconstructCallFunction(Fn, Fns, UnqualifiedName, Qualifier, QualifierRange,
|
2009-11-22 08:44:51 +08:00
|
|
|
ArgumentDependentLookup, Overloaded,
|
2009-11-23 09:53:49 +08:00
|
|
|
HasExplicitTemplateArgs, ExplicitTemplateArgs);
|
2009-09-22 23:41:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
// FIXME: What if we're calling something that isn't a function declaration?
|
|
|
|
// FIXME: What if we're calling a pseudo-destructor?
|
|
|
|
// FIXME: What if we're calling a member function?
|
|
|
|
|
|
|
|
// Build an overload candidate set based on the functions we find.
|
|
|
|
OverloadCandidateSet CandidateSet;
|
2009-11-21 16:51:07 +08:00
|
|
|
AddOverloadedCallCandidates(Fns, UnqualifiedName,
|
2009-11-23 09:53:49 +08:00
|
|
|
ArgumentDependentLookup,
|
|
|
|
(HasExplicitTemplateArgs ? &ExplicitTemplateArgs : 0),
|
2009-09-22 23:41:20 +08:00
|
|
|
Args, NumArgs,
|
|
|
|
CandidateSet,
|
|
|
|
/*PartialOverloading=*/true);
|
|
|
|
|
|
|
|
// Sort the overload candidate set by placing the best overloads first.
|
|
|
|
std::stable_sort(CandidateSet.begin(), CandidateSet.end(),
|
|
|
|
IsBetterOverloadCandidate(*this));
|
|
|
|
|
|
|
|
// Add the remaining viable overload candidates as code-completion reslults.
|
2009-09-23 08:16:58 +08:00
|
|
|
typedef CodeCompleteConsumer::OverloadCandidate ResultCandidate;
|
|
|
|
llvm::SmallVector<ResultCandidate, 8> Results;
|
2009-09-23 01:29:51 +08:00
|
|
|
|
2009-09-22 23:41:20 +08:00
|
|
|
for (OverloadCandidateSet::iterator Cand = CandidateSet.begin(),
|
|
|
|
CandEnd = CandidateSet.end();
|
|
|
|
Cand != CandEnd; ++Cand) {
|
|
|
|
if (Cand->Viable)
|
2009-09-23 08:16:58 +08:00
|
|
|
Results.push_back(ResultCandidate(Cand->Function));
|
2009-09-22 23:41:20 +08:00
|
|
|
}
|
2009-11-13 16:58:20 +08:00
|
|
|
CodeCompleter->ProcessOverloadCandidates(*this, NumArgs, Results.data(),
|
2009-09-23 08:16:58 +08:00
|
|
|
Results.size());
|
2009-09-22 23:41:20 +08:00
|
|
|
}
|
|
|
|
|
Initial implementation of a code-completion interface in Clang. In
essence, code completion is triggered by a magic "code completion"
token produced by the lexer [*], which the parser recognizes at
certain points in the grammar. The parser then calls into the Action
object with the appropriate CodeCompletionXXX action.
Sema implements the CodeCompletionXXX callbacks by performing minimal
translation, then forwarding them to a CodeCompletionConsumer
subclass, which uses the results of semantic analysis to provide
code-completion results. At present, only a single, "printing" code
completion consumer is available, for regression testing and
debugging. However, the design is meant to permit other
code-completion consumers.
This initial commit contains two code-completion actions: one for
member access, e.g., "x." or "p->", and one for
nested-name-specifiers, e.g., "std::". More code-completion actions
will follow, along with improved gathering of code-completion results
for the various contexts.
[*] In the current -code-completion-dump testing/debugging mode, the
file is truncated at the completion point and EOF is translated into
"code completion".
llvm-svn: 82166
2009-09-18 05:32:03 +08:00
|
|
|
void Sema::CodeCompleteQualifiedId(Scope *S, const CXXScopeSpec &SS,
|
|
|
|
bool EnteringContext) {
|
|
|
|
if (!SS.getScopeRep() || !CodeCompleter)
|
|
|
|
return;
|
|
|
|
|
2009-09-22 00:56:56 +08:00
|
|
|
DeclContext *Ctx = computeDeclContext(SS, EnteringContext);
|
|
|
|
if (!Ctx)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ResultBuilder Results(*this);
|
2009-09-22 04:12:40 +08:00
|
|
|
unsigned NextRank = CollectMemberLookupResults(Ctx, 0, Ctx, Results);
|
2009-09-22 00:56:56 +08:00
|
|
|
|
|
|
|
// The "template" keyword can follow "::" in the grammar, but only
|
|
|
|
// put it into the grammar if the nested-name-specifier is dependent.
|
|
|
|
NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
|
|
|
|
if (!Results.empty() && NNS->isDependent())
|
|
|
|
Results.MaybeAddResult(CodeCompleteConsumer::Result("template", NextRank));
|
|
|
|
|
2009-11-07 08:00:49 +08:00
|
|
|
if (CodeCompleter->includeMacros())
|
|
|
|
AddMacroResults(PP, NextRank + 1, Results);
|
2009-11-13 16:58:20 +08:00
|
|
|
HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
|
Initial implementation of a code-completion interface in Clang. In
essence, code completion is triggered by a magic "code completion"
token produced by the lexer [*], which the parser recognizes at
certain points in the grammar. The parser then calls into the Action
object with the appropriate CodeCompletionXXX action.
Sema implements the CodeCompletionXXX callbacks by performing minimal
translation, then forwarding them to a CodeCompletionConsumer
subclass, which uses the results of semantic analysis to provide
code-completion results. At present, only a single, "printing" code
completion consumer is available, for regression testing and
debugging. However, the design is meant to permit other
code-completion consumers.
This initial commit contains two code-completion actions: one for
member access, e.g., "x." or "p->", and one for
nested-name-specifiers, e.g., "std::". More code-completion actions
will follow, along with improved gathering of code-completion results
for the various contexts.
[*] In the current -code-completion-dump testing/debugging mode, the
file is truncated at the completion point and EOF is translated into
"code completion".
llvm-svn: 82166
2009-09-18 05:32:03 +08:00
|
|
|
}
|
2009-09-19 03:03:04 +08:00
|
|
|
|
|
|
|
void Sema::CodeCompleteUsing(Scope *S) {
|
|
|
|
if (!CodeCompleter)
|
|
|
|
return;
|
|
|
|
|
2009-09-22 00:56:56 +08:00
|
|
|
ResultBuilder Results(*this, &ResultBuilder::IsNestedNameSpecifier);
|
2009-09-23 07:31:26 +08:00
|
|
|
Results.EnterNewScope();
|
2009-09-22 00:56:56 +08:00
|
|
|
|
|
|
|
// If we aren't in class scope, we could see the "namespace" keyword.
|
|
|
|
if (!S->isClassScope())
|
|
|
|
Results.MaybeAddResult(CodeCompleteConsumer::Result("namespace", 0));
|
|
|
|
|
|
|
|
// After "using", we can see anything that would start a
|
|
|
|
// nested-name-specifier.
|
2009-10-31 00:50:04 +08:00
|
|
|
unsigned NextRank = CollectLookupResults(S, Context.getTranslationUnitDecl(),
|
|
|
|
0, CurContext, Results);
|
2009-09-23 07:31:26 +08:00
|
|
|
Results.ExitScope();
|
2009-09-22 00:56:56 +08:00
|
|
|
|
2009-11-07 08:00:49 +08:00
|
|
|
if (CodeCompleter->includeMacros())
|
|
|
|
AddMacroResults(PP, NextRank, Results);
|
2009-11-13 16:58:20 +08:00
|
|
|
HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
|
2009-09-19 03:03:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sema::CodeCompleteUsingDirective(Scope *S) {
|
|
|
|
if (!CodeCompleter)
|
|
|
|
return;
|
|
|
|
|
2009-09-22 00:56:56 +08:00
|
|
|
// After "using namespace", we expect to see a namespace name or namespace
|
|
|
|
// alias.
|
|
|
|
ResultBuilder Results(*this, &ResultBuilder::IsNamespaceOrAlias);
|
2009-09-23 07:31:26 +08:00
|
|
|
Results.EnterNewScope();
|
2009-10-31 00:50:04 +08:00
|
|
|
unsigned NextRank = CollectLookupResults(S, Context.getTranslationUnitDecl(),
|
|
|
|
0, CurContext, Results);
|
2009-09-23 07:31:26 +08:00
|
|
|
Results.ExitScope();
|
2009-11-07 08:00:49 +08:00
|
|
|
if (CodeCompleter->includeMacros())
|
|
|
|
AddMacroResults(PP, NextRank, Results);
|
2009-11-13 16:58:20 +08:00
|
|
|
HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
|
2009-09-19 03:03:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sema::CodeCompleteNamespaceDecl(Scope *S) {
|
|
|
|
if (!CodeCompleter)
|
|
|
|
return;
|
|
|
|
|
2009-09-22 00:56:56 +08:00
|
|
|
ResultBuilder Results(*this, &ResultBuilder::IsNamespace);
|
|
|
|
DeclContext *Ctx = (DeclContext *)S->getEntity();
|
|
|
|
if (!S->getParent())
|
|
|
|
Ctx = Context.getTranslationUnitDecl();
|
|
|
|
|
|
|
|
if (Ctx && Ctx->isFileContext()) {
|
|
|
|
// We only want to see those namespaces that have already been defined
|
|
|
|
// within this scope, because its likely that the user is creating an
|
|
|
|
// extended namespace declaration. Keep track of the most recent
|
|
|
|
// definition of each namespace.
|
|
|
|
std::map<NamespaceDecl *, NamespaceDecl *> OrigToLatest;
|
|
|
|
for (DeclContext::specific_decl_iterator<NamespaceDecl>
|
|
|
|
NS(Ctx->decls_begin()), NSEnd(Ctx->decls_end());
|
|
|
|
NS != NSEnd; ++NS)
|
|
|
|
OrigToLatest[NS->getOriginalNamespace()] = *NS;
|
|
|
|
|
|
|
|
// Add the most recent definition (or extended definition) of each
|
|
|
|
// namespace to the list of results.
|
2009-09-23 07:31:26 +08:00
|
|
|
Results.EnterNewScope();
|
2009-09-22 00:56:56 +08:00
|
|
|
for (std::map<NamespaceDecl *, NamespaceDecl *>::iterator
|
|
|
|
NS = OrigToLatest.begin(), NSEnd = OrigToLatest.end();
|
|
|
|
NS != NSEnd; ++NS)
|
2009-09-22 04:12:40 +08:00
|
|
|
Results.MaybeAddResult(CodeCompleteConsumer::Result(NS->second, 0),
|
|
|
|
CurContext);
|
2009-09-23 07:31:26 +08:00
|
|
|
Results.ExitScope();
|
2009-09-22 00:56:56 +08:00
|
|
|
}
|
|
|
|
|
2009-11-07 08:00:49 +08:00
|
|
|
if (CodeCompleter->includeMacros())
|
|
|
|
AddMacroResults(PP, 1, Results);
|
2009-11-13 16:58:20 +08:00
|
|
|
HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
|
2009-09-19 03:03:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sema::CodeCompleteNamespaceAliasDecl(Scope *S) {
|
|
|
|
if (!CodeCompleter)
|
|
|
|
return;
|
|
|
|
|
2009-09-22 00:56:56 +08:00
|
|
|
// After "namespace", we expect to see a namespace or alias.
|
|
|
|
ResultBuilder Results(*this, &ResultBuilder::IsNamespaceOrAlias);
|
2009-10-31 00:50:04 +08:00
|
|
|
unsigned NextRank = CollectLookupResults(S, Context.getTranslationUnitDecl(),
|
|
|
|
0, CurContext, Results);
|
2009-11-07 08:00:49 +08:00
|
|
|
if (CodeCompleter->includeMacros())
|
|
|
|
AddMacroResults(PP, NextRank, Results);
|
2009-11-13 16:58:20 +08:00
|
|
|
HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
|
2009-09-19 03:03:04 +08:00
|
|
|
}
|
|
|
|
|
2009-09-19 04:05:18 +08:00
|
|
|
void Sema::CodeCompleteOperatorName(Scope *S) {
|
|
|
|
if (!CodeCompleter)
|
|
|
|
return;
|
2009-09-22 00:56:56 +08:00
|
|
|
|
|
|
|
typedef CodeCompleteConsumer::Result Result;
|
|
|
|
ResultBuilder Results(*this, &ResultBuilder::IsType);
|
2009-09-23 07:31:26 +08:00
|
|
|
Results.EnterNewScope();
|
2009-09-22 00:56:56 +08:00
|
|
|
|
|
|
|
// Add the names of overloadable operators.
|
|
|
|
#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) \
|
|
|
|
if (std::strcmp(Spelling, "?")) \
|
|
|
|
Results.MaybeAddResult(Result(Spelling, 0));
|
|
|
|
#include "clang/Basic/OperatorKinds.def"
|
|
|
|
|
|
|
|
// Add any type names visible from the current scope
|
|
|
|
unsigned NextRank = CollectLookupResults(S, Context.getTranslationUnitDecl(),
|
2009-09-22 04:12:40 +08:00
|
|
|
0, CurContext, Results);
|
2009-09-22 00:56:56 +08:00
|
|
|
|
|
|
|
// Add any type specifiers
|
|
|
|
AddTypeSpecifierResults(getLangOptions(), 0, Results);
|
|
|
|
|
|
|
|
// Add any nested-name-specifiers
|
|
|
|
Results.setFilter(&ResultBuilder::IsNestedNameSpecifier);
|
2009-10-31 00:50:04 +08:00
|
|
|
NextRank = CollectLookupResults(S, Context.getTranslationUnitDecl(),
|
|
|
|
NextRank + 1, CurContext, Results);
|
2009-09-23 07:31:26 +08:00
|
|
|
Results.ExitScope();
|
2009-09-19 04:05:18 +08:00
|
|
|
|
2009-11-07 08:00:49 +08:00
|
|
|
if (CodeCompleter->includeMacros())
|
|
|
|
AddMacroResults(PP, NextRank, Results);
|
2009-11-13 16:58:20 +08:00
|
|
|
HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
|
2009-12-07 17:27:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sema::CodeCompleteObjCAtDirective(Scope *S, DeclPtrTy ObjCImpDecl,
|
|
|
|
bool InInterface) {
|
|
|
|
typedef CodeCompleteConsumer::Result Result;
|
|
|
|
ResultBuilder Results(*this);
|
|
|
|
Results.EnterNewScope();
|
|
|
|
if (ObjCImpDecl) {
|
|
|
|
// Since we have an implementation, we can end it.
|
|
|
|
Results.MaybeAddResult(Result("end", 0));
|
|
|
|
|
|
|
|
CodeCompletionString *Pattern = 0;
|
|
|
|
Decl *ImpDecl = ObjCImpDecl.getAs<Decl>();
|
|
|
|
if (isa<ObjCImplementationDecl>(ImpDecl) ||
|
|
|
|
isa<ObjCCategoryImplDecl>(ImpDecl)) {
|
|
|
|
// @dynamic
|
|
|
|
Pattern = new CodeCompletionString;
|
|
|
|
Pattern->AddTypedTextChunk("dynamic");
|
|
|
|
Pattern->AddTextChunk(" ");
|
|
|
|
Pattern->AddPlaceholderChunk("property");
|
|
|
|
Results.MaybeAddResult(Result(Pattern, 0));
|
|
|
|
|
|
|
|
// @synthesize
|
|
|
|
Pattern = new CodeCompletionString;
|
|
|
|
Pattern->AddTypedTextChunk("synthesize");
|
|
|
|
Pattern->AddTextChunk(" ");
|
|
|
|
Pattern->AddPlaceholderChunk("property");
|
|
|
|
Results.MaybeAddResult(Result(Pattern, 0));
|
|
|
|
}
|
|
|
|
} else if (InInterface) {
|
|
|
|
// Since we have an interface or protocol, we can end it.
|
|
|
|
Results.MaybeAddResult(Result("end", 0));
|
|
|
|
|
|
|
|
if (LangOpts.ObjC2) {
|
|
|
|
// @property
|
|
|
|
Results.MaybeAddResult(Result("property", 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
// @required
|
|
|
|
Results.MaybeAddResult(Result("required", 0));
|
|
|
|
|
|
|
|
// @optional
|
|
|
|
Results.MaybeAddResult(Result("optional", 0));
|
|
|
|
} else {
|
|
|
|
CodeCompletionString *Pattern = 0;
|
|
|
|
|
|
|
|
// @class name ;
|
|
|
|
Pattern = new CodeCompletionString;
|
|
|
|
Pattern->AddTypedTextChunk("class");
|
|
|
|
Pattern->AddTextChunk(" ");
|
|
|
|
Pattern->AddPlaceholderChunk("identifier");
|
|
|
|
Pattern->AddTextChunk(";"); // add ';' chunk
|
|
|
|
Results.MaybeAddResult(Result(Pattern, 0));
|
|
|
|
|
|
|
|
// @interface name
|
|
|
|
// FIXME: Could introduce the whole pattern, including superclasses and
|
|
|
|
// such.
|
|
|
|
Pattern = new CodeCompletionString;
|
|
|
|
Pattern->AddTypedTextChunk("interface");
|
|
|
|
Pattern->AddTextChunk(" ");
|
|
|
|
Pattern->AddPlaceholderChunk("class");
|
|
|
|
Results.MaybeAddResult(Result(Pattern, 0));
|
|
|
|
|
|
|
|
// @protocol name
|
|
|
|
Pattern = new CodeCompletionString;
|
|
|
|
Pattern->AddTypedTextChunk("protocol");
|
|
|
|
Pattern->AddTextChunk(" ");
|
|
|
|
Pattern->AddPlaceholderChunk("protocol");
|
|
|
|
Results.MaybeAddResult(Result(Pattern, 0));
|
|
|
|
|
|
|
|
// @implementation name
|
|
|
|
Pattern = new CodeCompletionString;
|
|
|
|
Pattern->AddTypedTextChunk("implementation");
|
|
|
|
Pattern->AddTextChunk(" ");
|
|
|
|
Pattern->AddPlaceholderChunk("class");
|
|
|
|
Results.MaybeAddResult(Result(Pattern, 0));
|
|
|
|
|
|
|
|
// @compatibility_alias name
|
|
|
|
Pattern = new CodeCompletionString;
|
|
|
|
Pattern->AddTypedTextChunk("compatibility_alias");
|
|
|
|
Pattern->AddTextChunk(" ");
|
|
|
|
Pattern->AddPlaceholderChunk("alias");
|
|
|
|
Pattern->AddTextChunk(" ");
|
|
|
|
Pattern->AddPlaceholderChunk("class");
|
|
|
|
Results.MaybeAddResult(Result(Pattern, 0));
|
|
|
|
}
|
|
|
|
Results.ExitScope();
|
|
|
|
HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
|
2009-12-07 17:51:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void AddObjCExpressionResults(unsigned Rank, ResultBuilder &Results) {
|
|
|
|
typedef CodeCompleteConsumer::Result Result;
|
|
|
|
CodeCompletionString *Pattern = 0;
|
|
|
|
|
|
|
|
// @encode ( type-name )
|
|
|
|
Pattern = new CodeCompletionString;
|
|
|
|
Pattern->AddTypedTextChunk("encode");
|
|
|
|
Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
|
|
|
|
Pattern->AddPlaceholderChunk("type-name");
|
|
|
|
Pattern->AddChunk(CodeCompletionString::CK_RightParen);
|
|
|
|
Results.MaybeAddResult(Result(Pattern, Rank));
|
|
|
|
|
|
|
|
// @protocol ( protocol-name )
|
|
|
|
Pattern = new CodeCompletionString;
|
|
|
|
Pattern->AddTypedTextChunk("protocol");
|
|
|
|
Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
|
|
|
|
Pattern->AddPlaceholderChunk("protocol-name");
|
|
|
|
Pattern->AddChunk(CodeCompletionString::CK_RightParen);
|
|
|
|
Results.MaybeAddResult(Result(Pattern, Rank));
|
|
|
|
|
|
|
|
// @selector ( selector )
|
|
|
|
Pattern = new CodeCompletionString;
|
|
|
|
Pattern->AddTypedTextChunk("selector");
|
|
|
|
Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
|
|
|
|
Pattern->AddPlaceholderChunk("selector");
|
|
|
|
Pattern->AddChunk(CodeCompletionString::CK_RightParen);
|
|
|
|
Results.MaybeAddResult(Result(Pattern, Rank));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sema::CodeCompleteObjCAtStatement(Scope *S) {
|
|
|
|
typedef CodeCompleteConsumer::Result Result;
|
|
|
|
ResultBuilder Results(*this);
|
|
|
|
Results.EnterNewScope();
|
|
|
|
|
|
|
|
CodeCompletionString *Pattern = 0;
|
|
|
|
|
|
|
|
// @try { statements } @catch ( declaration ) { statements } @finally
|
|
|
|
// { statements }
|
|
|
|
Pattern = new CodeCompletionString;
|
|
|
|
Pattern->AddTypedTextChunk("try");
|
|
|
|
Pattern->AddChunk(CodeCompletionString::CK_LeftBrace);
|
|
|
|
Pattern->AddPlaceholderChunk("statements");
|
|
|
|
Pattern->AddChunk(CodeCompletionString::CK_RightBrace);
|
|
|
|
Pattern->AddTextChunk("@catch");
|
|
|
|
Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
|
|
|
|
Pattern->AddPlaceholderChunk("parameter");
|
|
|
|
Pattern->AddChunk(CodeCompletionString::CK_RightParen);
|
|
|
|
Pattern->AddChunk(CodeCompletionString::CK_LeftBrace);
|
|
|
|
Pattern->AddPlaceholderChunk("statements");
|
|
|
|
Pattern->AddChunk(CodeCompletionString::CK_RightBrace);
|
|
|
|
Pattern->AddTextChunk("@finally");
|
|
|
|
Pattern->AddChunk(CodeCompletionString::CK_LeftBrace);
|
|
|
|
Pattern->AddPlaceholderChunk("statements");
|
|
|
|
Pattern->AddChunk(CodeCompletionString::CK_RightBrace);
|
|
|
|
Results.MaybeAddResult(Result(Pattern, 0));
|
|
|
|
|
|
|
|
// @throw
|
|
|
|
Pattern = new CodeCompletionString;
|
|
|
|
Pattern->AddTypedTextChunk("throw");
|
|
|
|
Pattern->AddTextChunk(" ");
|
|
|
|
Pattern->AddPlaceholderChunk("expression");
|
|
|
|
Pattern->AddTextChunk(";");
|
|
|
|
Results.MaybeAddResult(Result(Pattern, 0)); // FIXME: add ';' chunk
|
|
|
|
|
|
|
|
// @synchronized ( expression ) { statements }
|
|
|
|
Pattern = new CodeCompletionString;
|
|
|
|
Pattern->AddTypedTextChunk("synchronized");
|
|
|
|
Pattern->AddTextChunk(" ");
|
|
|
|
Pattern->AddChunk(CodeCompletionString::CK_LeftParen);
|
|
|
|
Pattern->AddPlaceholderChunk("expression");
|
|
|
|
Pattern->AddChunk(CodeCompletionString::CK_RightParen);
|
|
|
|
Pattern->AddChunk(CodeCompletionString::CK_LeftBrace);
|
|
|
|
Pattern->AddPlaceholderChunk("statements");
|
|
|
|
Pattern->AddChunk(CodeCompletionString::CK_RightBrace);
|
|
|
|
Results.MaybeAddResult(Result(Pattern, 0)); // FIXME: add ';' chunk
|
|
|
|
|
|
|
|
AddObjCExpressionResults(0, Results);
|
|
|
|
Results.ExitScope();
|
|
|
|
HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sema::CodeCompleteObjCAtExpression(Scope *S) {
|
|
|
|
ResultBuilder Results(*this);
|
|
|
|
Results.EnterNewScope();
|
|
|
|
AddObjCExpressionResults(0, Results);
|
|
|
|
Results.ExitScope();
|
|
|
|
HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
|
2009-09-19 04:05:18 +08:00
|
|
|
}
|
2009-09-19 03:03:04 +08:00
|
|
|
|
2009-11-19 08:14:45 +08:00
|
|
|
/// \brief Determine whether the addition of the given flag to an Objective-C
|
|
|
|
/// property's attributes will cause a conflict.
|
|
|
|
static bool ObjCPropertyFlagConflicts(unsigned Attributes, unsigned NewFlag) {
|
|
|
|
// Check if we've already added this flag.
|
|
|
|
if (Attributes & NewFlag)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
Attributes |= NewFlag;
|
|
|
|
|
|
|
|
// Check for collisions with "readonly".
|
|
|
|
if ((Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
|
|
|
|
(Attributes & (ObjCDeclSpec::DQ_PR_readwrite |
|
|
|
|
ObjCDeclSpec::DQ_PR_assign |
|
|
|
|
ObjCDeclSpec::DQ_PR_copy |
|
|
|
|
ObjCDeclSpec::DQ_PR_retain)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Check for more than one of { assign, copy, retain }.
|
|
|
|
unsigned AssignCopyRetMask = Attributes & (ObjCDeclSpec::DQ_PR_assign |
|
|
|
|
ObjCDeclSpec::DQ_PR_copy |
|
|
|
|
ObjCDeclSpec::DQ_PR_retain);
|
|
|
|
if (AssignCopyRetMask &&
|
|
|
|
AssignCopyRetMask != ObjCDeclSpec::DQ_PR_assign &&
|
|
|
|
AssignCopyRetMask != ObjCDeclSpec::DQ_PR_copy &&
|
|
|
|
AssignCopyRetMask != ObjCDeclSpec::DQ_PR_retain)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-11-19 07:08:07 +08:00
|
|
|
void Sema::CodeCompleteObjCPropertyFlags(Scope *S, ObjCDeclSpec &ODS) {
|
2009-10-09 05:55:05 +08:00
|
|
|
if (!CodeCompleter)
|
|
|
|
return;
|
2009-11-19 09:08:35 +08:00
|
|
|
|
2009-10-09 05:55:05 +08:00
|
|
|
unsigned Attributes = ODS.getPropertyAttributes();
|
|
|
|
|
|
|
|
typedef CodeCompleteConsumer::Result Result;
|
|
|
|
ResultBuilder Results(*this);
|
|
|
|
Results.EnterNewScope();
|
2009-11-19 08:14:45 +08:00
|
|
|
if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readonly))
|
2009-10-09 05:55:05 +08:00
|
|
|
Results.MaybeAddResult(CodeCompleteConsumer::Result("readonly", 0));
|
2009-11-19 08:14:45 +08:00
|
|
|
if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_assign))
|
2009-10-09 05:55:05 +08:00
|
|
|
Results.MaybeAddResult(CodeCompleteConsumer::Result("assign", 0));
|
2009-11-19 08:14:45 +08:00
|
|
|
if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_readwrite))
|
2009-10-09 05:55:05 +08:00
|
|
|
Results.MaybeAddResult(CodeCompleteConsumer::Result("readwrite", 0));
|
2009-11-19 08:14:45 +08:00
|
|
|
if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_retain))
|
2009-10-09 05:55:05 +08:00
|
|
|
Results.MaybeAddResult(CodeCompleteConsumer::Result("retain", 0));
|
2009-11-19 08:14:45 +08:00
|
|
|
if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_copy))
|
2009-10-09 05:55:05 +08:00
|
|
|
Results.MaybeAddResult(CodeCompleteConsumer::Result("copy", 0));
|
2009-11-19 08:14:45 +08:00
|
|
|
if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_nonatomic))
|
2009-10-09 05:55:05 +08:00
|
|
|
Results.MaybeAddResult(CodeCompleteConsumer::Result("nonatomic", 0));
|
2009-11-19 08:14:45 +08:00
|
|
|
if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_setter)) {
|
2009-11-19 08:01:57 +08:00
|
|
|
CodeCompletionString *Setter = new CodeCompletionString;
|
|
|
|
Setter->AddTypedTextChunk("setter");
|
|
|
|
Setter->AddTextChunk(" = ");
|
|
|
|
Setter->AddPlaceholderChunk("method");
|
|
|
|
Results.MaybeAddResult(CodeCompleteConsumer::Result(Setter, 0));
|
|
|
|
}
|
2009-11-19 08:14:45 +08:00
|
|
|
if (!ObjCPropertyFlagConflicts(Attributes, ObjCDeclSpec::DQ_PR_getter)) {
|
2009-11-19 08:01:57 +08:00
|
|
|
CodeCompletionString *Getter = new CodeCompletionString;
|
|
|
|
Getter->AddTypedTextChunk("getter");
|
|
|
|
Getter->AddTextChunk(" = ");
|
|
|
|
Getter->AddPlaceholderChunk("method");
|
|
|
|
Results.MaybeAddResult(CodeCompleteConsumer::Result(Getter, 0));
|
|
|
|
}
|
2009-10-09 05:55:05 +08:00
|
|
|
Results.ExitScope();
|
2009-11-13 16:58:20 +08:00
|
|
|
HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
|
2009-10-09 05:55:05 +08:00
|
|
|
}
|
2009-11-07 10:08:14 +08:00
|
|
|
|
2009-11-19 15:41:15 +08:00
|
|
|
/// \brief Descripts the kind of Objective-C method that we want to find
|
|
|
|
/// via code completion.
|
|
|
|
enum ObjCMethodKind {
|
|
|
|
MK_Any, //< Any kind of method, provided it means other specified criteria.
|
|
|
|
MK_ZeroArgSelector, //< Zero-argument (unary) selector.
|
|
|
|
MK_OneArgSelector //< One-argument selector.
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool isAcceptableObjCMethod(ObjCMethodDecl *Method,
|
|
|
|
ObjCMethodKind WantKind,
|
|
|
|
IdentifierInfo **SelIdents,
|
|
|
|
unsigned NumSelIdents) {
|
|
|
|
Selector Sel = Method->getSelector();
|
|
|
|
if (NumSelIdents > Sel.getNumArgs())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (WantKind) {
|
|
|
|
case MK_Any: break;
|
|
|
|
case MK_ZeroArgSelector: return Sel.isUnarySelector();
|
|
|
|
case MK_OneArgSelector: return Sel.getNumArgs() == 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned I = 0; I != NumSelIdents; ++I)
|
|
|
|
if (SelIdents[I] != Sel.getIdentifierInfoForSlot(I))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-11-18 07:22:23 +08:00
|
|
|
/// \brief Add all of the Objective-C methods in the given Objective-C
|
|
|
|
/// container to the set of results.
|
|
|
|
///
|
|
|
|
/// The container will be a class, protocol, category, or implementation of
|
|
|
|
/// any of the above. This mether will recurse to include methods from
|
|
|
|
/// the superclasses of classes along with their categories, protocols, and
|
|
|
|
/// implementations.
|
|
|
|
///
|
|
|
|
/// \param Container the container in which we'll look to find methods.
|
|
|
|
///
|
|
|
|
/// \param WantInstance whether to add instance methods (only); if false, this
|
|
|
|
/// routine will add factory methods (only).
|
|
|
|
///
|
|
|
|
/// \param CurContext the context in which we're performing the lookup that
|
|
|
|
/// finds methods.
|
|
|
|
///
|
|
|
|
/// \param Results the structure into which we'll add results.
|
|
|
|
static void AddObjCMethods(ObjCContainerDecl *Container,
|
|
|
|
bool WantInstanceMethods,
|
2009-11-19 15:41:15 +08:00
|
|
|
ObjCMethodKind WantKind,
|
2009-11-19 09:08:35 +08:00
|
|
|
IdentifierInfo **SelIdents,
|
|
|
|
unsigned NumSelIdents,
|
2009-11-18 07:22:23 +08:00
|
|
|
DeclContext *CurContext,
|
|
|
|
ResultBuilder &Results) {
|
|
|
|
typedef CodeCompleteConsumer::Result Result;
|
|
|
|
for (ObjCContainerDecl::method_iterator M = Container->meth_begin(),
|
|
|
|
MEnd = Container->meth_end();
|
|
|
|
M != MEnd; ++M) {
|
2009-11-19 09:08:35 +08:00
|
|
|
if ((*M)->isInstanceMethod() == WantInstanceMethods) {
|
|
|
|
// Check whether the selector identifiers we've been given are a
|
|
|
|
// subset of the identifiers for this particular method.
|
2009-11-19 15:41:15 +08:00
|
|
|
if (!isAcceptableObjCMethod(*M, WantKind, SelIdents, NumSelIdents))
|
2009-11-19 09:08:35 +08:00
|
|
|
continue;
|
2009-11-19 15:41:15 +08:00
|
|
|
|
2009-11-19 09:08:35 +08:00
|
|
|
Result R = Result(*M, 0);
|
|
|
|
R.StartParameter = NumSelIdents;
|
2009-11-19 15:41:15 +08:00
|
|
|
R.AllParametersAreInformative = (WantKind != MK_Any);
|
2009-11-19 09:08:35 +08:00
|
|
|
Results.MaybeAddResult(R, CurContext);
|
|
|
|
}
|
2009-11-18 07:22:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Container);
|
|
|
|
if (!IFace)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Add methods in protocols.
|
|
|
|
const ObjCList<ObjCProtocolDecl> &Protocols= IFace->getReferencedProtocols();
|
|
|
|
for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
|
|
|
|
E = Protocols.end();
|
|
|
|
I != E; ++I)
|
2009-11-19 15:41:15 +08:00
|
|
|
AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents, NumSelIdents,
|
2009-11-19 09:08:35 +08:00
|
|
|
CurContext, Results);
|
2009-11-18 07:22:23 +08:00
|
|
|
|
|
|
|
// Add methods in categories.
|
|
|
|
for (ObjCCategoryDecl *CatDecl = IFace->getCategoryList(); CatDecl;
|
|
|
|
CatDecl = CatDecl->getNextClassCategory()) {
|
2009-11-19 15:41:15 +08:00
|
|
|
AddObjCMethods(CatDecl, WantInstanceMethods, WantKind, SelIdents,
|
|
|
|
NumSelIdents, CurContext, Results);
|
2009-11-18 07:22:23 +08:00
|
|
|
|
|
|
|
// Add a categories protocol methods.
|
|
|
|
const ObjCList<ObjCProtocolDecl> &Protocols
|
|
|
|
= CatDecl->getReferencedProtocols();
|
|
|
|
for (ObjCList<ObjCProtocolDecl>::iterator I = Protocols.begin(),
|
|
|
|
E = Protocols.end();
|
|
|
|
I != E; ++I)
|
2009-11-19 15:41:15 +08:00
|
|
|
AddObjCMethods(*I, WantInstanceMethods, WantKind, SelIdents,
|
|
|
|
NumSelIdents, CurContext, Results);
|
2009-11-18 07:22:23 +08:00
|
|
|
|
|
|
|
// Add methods in category implementations.
|
|
|
|
if (ObjCCategoryImplDecl *Impl = CatDecl->getImplementation())
|
2009-11-19 15:41:15 +08:00
|
|
|
AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents,
|
|
|
|
NumSelIdents, CurContext, Results);
|
2009-11-18 07:22:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add methods in superclass.
|
|
|
|
if (IFace->getSuperClass())
|
2009-11-19 15:41:15 +08:00
|
|
|
AddObjCMethods(IFace->getSuperClass(), WantInstanceMethods, WantKind,
|
|
|
|
SelIdents, NumSelIdents, CurContext, Results);
|
2009-11-18 07:22:23 +08:00
|
|
|
|
|
|
|
// Add methods in our implementation, if any.
|
|
|
|
if (ObjCImplementationDecl *Impl = IFace->getImplementation())
|
2009-11-19 15:41:15 +08:00
|
|
|
AddObjCMethods(Impl, WantInstanceMethods, WantKind, SelIdents,
|
|
|
|
NumSelIdents, CurContext, Results);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Sema::CodeCompleteObjCPropertyGetter(Scope *S, DeclPtrTy ClassDecl,
|
|
|
|
DeclPtrTy *Methods,
|
|
|
|
unsigned NumMethods) {
|
|
|
|
typedef CodeCompleteConsumer::Result Result;
|
|
|
|
|
|
|
|
// Try to find the interface where getters might live.
|
|
|
|
ObjCInterfaceDecl *Class
|
|
|
|
= dyn_cast_or_null<ObjCInterfaceDecl>(ClassDecl.getAs<Decl>());
|
|
|
|
if (!Class) {
|
|
|
|
if (ObjCCategoryDecl *Category
|
|
|
|
= dyn_cast_or_null<ObjCCategoryDecl>(ClassDecl.getAs<Decl>()))
|
|
|
|
Class = Category->getClassInterface();
|
|
|
|
|
|
|
|
if (!Class)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find all of the potential getters.
|
|
|
|
ResultBuilder Results(*this);
|
|
|
|
Results.EnterNewScope();
|
|
|
|
|
|
|
|
// FIXME: We need to do this because Objective-C methods don't get
|
|
|
|
// pushed into DeclContexts early enough. Argh!
|
|
|
|
for (unsigned I = 0; I != NumMethods; ++I) {
|
|
|
|
if (ObjCMethodDecl *Method
|
|
|
|
= dyn_cast_or_null<ObjCMethodDecl>(Methods[I].getAs<Decl>()))
|
|
|
|
if (Method->isInstanceMethod() &&
|
|
|
|
isAcceptableObjCMethod(Method, MK_ZeroArgSelector, 0, 0)) {
|
|
|
|
Result R = Result(Method, 0);
|
|
|
|
R.AllParametersAreInformative = true;
|
|
|
|
Results.MaybeAddResult(R, CurContext);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AddObjCMethods(Class, true, MK_ZeroArgSelector, 0, 0, CurContext, Results);
|
|
|
|
Results.ExitScope();
|
|
|
|
HandleCodeCompleteResults(this, CodeCompleter,Results.data(),Results.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sema::CodeCompleteObjCPropertySetter(Scope *S, DeclPtrTy ObjCImplDecl,
|
|
|
|
DeclPtrTy *Methods,
|
|
|
|
unsigned NumMethods) {
|
|
|
|
typedef CodeCompleteConsumer::Result Result;
|
|
|
|
|
|
|
|
// Try to find the interface where setters might live.
|
|
|
|
ObjCInterfaceDecl *Class
|
|
|
|
= dyn_cast_or_null<ObjCInterfaceDecl>(ObjCImplDecl.getAs<Decl>());
|
|
|
|
if (!Class) {
|
|
|
|
if (ObjCCategoryDecl *Category
|
|
|
|
= dyn_cast_or_null<ObjCCategoryDecl>(ObjCImplDecl.getAs<Decl>()))
|
|
|
|
Class = Category->getClassInterface();
|
|
|
|
|
|
|
|
if (!Class)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find all of the potential getters.
|
|
|
|
ResultBuilder Results(*this);
|
|
|
|
Results.EnterNewScope();
|
|
|
|
|
|
|
|
// FIXME: We need to do this because Objective-C methods don't get
|
|
|
|
// pushed into DeclContexts early enough. Argh!
|
|
|
|
for (unsigned I = 0; I != NumMethods; ++I) {
|
|
|
|
if (ObjCMethodDecl *Method
|
|
|
|
= dyn_cast_or_null<ObjCMethodDecl>(Methods[I].getAs<Decl>()))
|
|
|
|
if (Method->isInstanceMethod() &&
|
|
|
|
isAcceptableObjCMethod(Method, MK_OneArgSelector, 0, 0)) {
|
|
|
|
Result R = Result(Method, 0);
|
|
|
|
R.AllParametersAreInformative = true;
|
|
|
|
Results.MaybeAddResult(R, CurContext);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AddObjCMethods(Class, true, MK_OneArgSelector, 0, 0, CurContext, Results);
|
|
|
|
|
|
|
|
Results.ExitScope();
|
|
|
|
HandleCodeCompleteResults(this, CodeCompleter,Results.data(),Results.size());
|
2009-11-18 07:22:23 +08:00
|
|
|
}
|
|
|
|
|
2009-11-18 07:31:36 +08:00
|
|
|
void Sema::CodeCompleteObjCClassMessage(Scope *S, IdentifierInfo *FName,
|
2009-11-19 09:08:35 +08:00
|
|
|
SourceLocation FNameLoc,
|
|
|
|
IdentifierInfo **SelIdents,
|
|
|
|
unsigned NumSelIdents) {
|
2009-11-07 10:08:14 +08:00
|
|
|
typedef CodeCompleteConsumer::Result Result;
|
2009-11-18 01:59:40 +08:00
|
|
|
ObjCInterfaceDecl *CDecl = 0;
|
|
|
|
|
|
|
|
if (FName->isStr("super")) {
|
|
|
|
// We're sending a message to "super".
|
|
|
|
if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) {
|
|
|
|
// Figure out which interface we're in.
|
|
|
|
CDecl = CurMethod->getClassInterface();
|
|
|
|
if (!CDecl)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Find the superclass of this class.
|
|
|
|
CDecl = CDecl->getSuperClass();
|
|
|
|
if (!CDecl)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (CurMethod->isInstanceMethod()) {
|
|
|
|
// We are inside an instance method, which means that the message
|
|
|
|
// send [super ...] is actually calling an instance method on the
|
|
|
|
// current object. Build the super expression and handle this like
|
|
|
|
// an instance method.
|
|
|
|
QualType SuperTy = Context.getObjCInterfaceType(CDecl);
|
|
|
|
SuperTy = Context.getObjCObjectPointerType(SuperTy);
|
|
|
|
OwningExprResult Super
|
2009-11-18 07:31:36 +08:00
|
|
|
= Owned(new (Context) ObjCSuperExpr(FNameLoc, SuperTy));
|
2009-11-19 09:08:35 +08:00
|
|
|
return CodeCompleteObjCInstanceMessage(S, (Expr *)Super.get(),
|
|
|
|
SelIdents, NumSelIdents);
|
2009-11-18 01:59:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Okay, we're calling a factory method in our superclass.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the given name refers to an interface type, retrieve the
|
|
|
|
// corresponding declaration.
|
|
|
|
if (!CDecl)
|
2009-11-18 07:31:36 +08:00
|
|
|
if (TypeTy *Ty = getTypeName(*FName, FNameLoc, S, 0, false)) {
|
2009-11-18 01:59:40 +08:00
|
|
|
QualType T = GetTypeFromParser(Ty, 0);
|
|
|
|
if (!T.isNull())
|
|
|
|
if (const ObjCInterfaceType *Interface = T->getAs<ObjCInterfaceType>())
|
|
|
|
CDecl = Interface->getDecl();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CDecl && FName->isStr("super")) {
|
|
|
|
// "super" may be the name of a variable, in which case we are
|
|
|
|
// probably calling an instance method.
|
2009-11-25 03:00:30 +08:00
|
|
|
CXXScopeSpec SS;
|
|
|
|
UnqualifiedId id;
|
|
|
|
id.setIdentifier(FName, FNameLoc);
|
|
|
|
OwningExprResult Super = ActOnIdExpression(S, SS, id, false, false);
|
2009-11-19 09:08:35 +08:00
|
|
|
return CodeCompleteObjCInstanceMessage(S, (Expr *)Super.get(),
|
|
|
|
SelIdents, NumSelIdents);
|
2009-11-18 01:59:40 +08:00
|
|
|
}
|
|
|
|
|
2009-11-18 07:22:23 +08:00
|
|
|
// Add all of the factory methods in this Objective-C class, its protocols,
|
|
|
|
// superclasses, categories, implementation, etc.
|
2009-11-07 10:08:14 +08:00
|
|
|
ResultBuilder Results(*this);
|
|
|
|
Results.EnterNewScope();
|
2009-11-19 15:41:15 +08:00
|
|
|
AddObjCMethods(CDecl, false, MK_Any, SelIdents, NumSelIdents, CurContext,
|
|
|
|
Results);
|
2009-11-07 10:08:14 +08:00
|
|
|
Results.ExitScope();
|
2009-11-18 07:22:23 +08:00
|
|
|
|
2009-11-07 10:08:14 +08:00
|
|
|
// This also suppresses remaining diagnostics.
|
2009-11-13 16:58:20 +08:00
|
|
|
HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
|
2009-11-07 10:08:14 +08:00
|
|
|
}
|
|
|
|
|
2009-11-19 09:08:35 +08:00
|
|
|
void Sema::CodeCompleteObjCInstanceMessage(Scope *S, ExprTy *Receiver,
|
|
|
|
IdentifierInfo **SelIdents,
|
|
|
|
unsigned NumSelIdents) {
|
2009-11-07 10:08:14 +08:00
|
|
|
typedef CodeCompleteConsumer::Result Result;
|
|
|
|
|
|
|
|
Expr *RecExpr = static_cast<Expr *>(Receiver);
|
|
|
|
QualType RecType = RecExpr->getType();
|
|
|
|
|
2009-11-18 07:22:23 +08:00
|
|
|
// If necessary, apply function/array conversion to the receiver.
|
|
|
|
// C99 6.7.5.3p[7,8].
|
|
|
|
DefaultFunctionArrayConversion(RecExpr);
|
|
|
|
QualType ReceiverType = RecExpr->getType();
|
2009-11-07 10:08:14 +08:00
|
|
|
|
2009-11-18 07:22:23 +08:00
|
|
|
if (ReceiverType->isObjCIdType() || ReceiverType->isBlockPointerType()) {
|
|
|
|
// FIXME: We're messaging 'id'. Do we actually want to look up every method
|
|
|
|
// in the universe?
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the set of methods we can see.
|
|
|
|
ResultBuilder Results(*this);
|
|
|
|
Results.EnterNewScope();
|
|
|
|
|
2009-11-18 08:06:18 +08:00
|
|
|
// Handle messages to Class. This really isn't a message to an instance
|
|
|
|
// method, so we treat it the same way we would treat a message send to a
|
|
|
|
// class method.
|
|
|
|
if (ReceiverType->isObjCClassType() ||
|
|
|
|
ReceiverType->isObjCQualifiedClassType()) {
|
|
|
|
if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) {
|
|
|
|
if (ObjCInterfaceDecl *ClassDecl = CurMethod->getClassInterface())
|
2009-11-19 15:41:15 +08:00
|
|
|
AddObjCMethods(ClassDecl, false, MK_Any, SelIdents, NumSelIdents,
|
|
|
|
CurContext, Results);
|
2009-11-18 08:06:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Handle messages to a qualified ID ("id<foo>").
|
|
|
|
else if (const ObjCObjectPointerType *QualID
|
|
|
|
= ReceiverType->getAsObjCQualifiedIdType()) {
|
|
|
|
// Search protocols for instance methods.
|
|
|
|
for (ObjCObjectPointerType::qual_iterator I = QualID->qual_begin(),
|
|
|
|
E = QualID->qual_end();
|
|
|
|
I != E; ++I)
|
2009-11-19 15:41:15 +08:00
|
|
|
AddObjCMethods(*I, true, MK_Any, SelIdents, NumSelIdents, CurContext,
|
|
|
|
Results);
|
2009-11-18 08:06:18 +08:00
|
|
|
}
|
|
|
|
// Handle messages to a pointer to interface type.
|
|
|
|
else if (const ObjCObjectPointerType *IFacePtr
|
|
|
|
= ReceiverType->getAsObjCInterfacePointerType()) {
|
|
|
|
// Search the class, its superclasses, etc., for instance methods.
|
2009-11-19 15:41:15 +08:00
|
|
|
AddObjCMethods(IFacePtr->getInterfaceDecl(), true, MK_Any, SelIdents,
|
|
|
|
NumSelIdents, CurContext, Results);
|
2009-11-18 08:06:18 +08:00
|
|
|
|
|
|
|
// Search protocols for instance methods.
|
|
|
|
for (ObjCObjectPointerType::qual_iterator I = IFacePtr->qual_begin(),
|
|
|
|
E = IFacePtr->qual_end();
|
|
|
|
I != E; ++I)
|
2009-11-19 15:41:15 +08:00
|
|
|
AddObjCMethods(*I, true, MK_Any, SelIdents, NumSelIdents, CurContext,
|
|
|
|
Results);
|
2009-11-18 08:06:18 +08:00
|
|
|
}
|
2009-11-18 07:22:23 +08:00
|
|
|
|
2009-11-18 08:06:18 +08:00
|
|
|
Results.ExitScope();
|
2009-11-13 16:58:20 +08:00
|
|
|
HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
|
2009-11-07 10:08:14 +08:00
|
|
|
}
|
2009-11-18 12:19:12 +08:00
|
|
|
|
|
|
|
/// \brief Add all of the protocol declarations that we find in the given
|
|
|
|
/// (translation unit) context.
|
|
|
|
static void AddProtocolResults(DeclContext *Ctx, DeclContext *CurContext,
|
2009-11-18 12:49:41 +08:00
|
|
|
bool OnlyForwardDeclarations,
|
2009-11-18 12:19:12 +08:00
|
|
|
ResultBuilder &Results) {
|
|
|
|
typedef CodeCompleteConsumer::Result Result;
|
|
|
|
|
|
|
|
for (DeclContext::decl_iterator D = Ctx->decls_begin(),
|
|
|
|
DEnd = Ctx->decls_end();
|
|
|
|
D != DEnd; ++D) {
|
|
|
|
// Record any protocols we find.
|
|
|
|
if (ObjCProtocolDecl *Proto = dyn_cast<ObjCProtocolDecl>(*D))
|
2009-11-18 12:49:41 +08:00
|
|
|
if (!OnlyForwardDeclarations || Proto->isForwardDecl())
|
|
|
|
Results.MaybeAddResult(Result(Proto, 0), CurContext);
|
2009-11-18 12:19:12 +08:00
|
|
|
|
|
|
|
// Record any forward-declared protocols we find.
|
|
|
|
if (ObjCForwardProtocolDecl *Forward
|
|
|
|
= dyn_cast<ObjCForwardProtocolDecl>(*D)) {
|
|
|
|
for (ObjCForwardProtocolDecl::protocol_iterator
|
|
|
|
P = Forward->protocol_begin(),
|
|
|
|
PEnd = Forward->protocol_end();
|
|
|
|
P != PEnd; ++P)
|
2009-11-18 12:49:41 +08:00
|
|
|
if (!OnlyForwardDeclarations || (*P)->isForwardDecl())
|
|
|
|
Results.MaybeAddResult(Result(*P, 0), CurContext);
|
2009-11-18 12:19:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sema::CodeCompleteObjCProtocolReferences(IdentifierLocPair *Protocols,
|
|
|
|
unsigned NumProtocols) {
|
|
|
|
ResultBuilder Results(*this);
|
|
|
|
Results.EnterNewScope();
|
|
|
|
|
|
|
|
// Tell the result set to ignore all of the protocols we have
|
|
|
|
// already seen.
|
|
|
|
for (unsigned I = 0; I != NumProtocols; ++I)
|
|
|
|
if (ObjCProtocolDecl *Protocol = LookupProtocol(Protocols[I].first))
|
|
|
|
Results.Ignore(Protocol);
|
|
|
|
|
|
|
|
// Add all protocols.
|
2009-11-18 12:49:41 +08:00
|
|
|
AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, false,
|
|
|
|
Results);
|
|
|
|
|
|
|
|
Results.ExitScope();
|
|
|
|
HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sema::CodeCompleteObjCProtocolDecl(Scope *) {
|
|
|
|
ResultBuilder Results(*this);
|
|
|
|
Results.EnterNewScope();
|
|
|
|
|
|
|
|
// Add all protocols.
|
|
|
|
AddProtocolResults(Context.getTranslationUnitDecl(), CurContext, true,
|
|
|
|
Results);
|
2009-11-18 12:19:12 +08:00
|
|
|
|
|
|
|
Results.ExitScope();
|
|
|
|
HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
|
|
|
|
}
|
2009-11-19 00:26:39 +08:00
|
|
|
|
|
|
|
/// \brief Add all of the Objective-C interface declarations that we find in
|
|
|
|
/// the given (translation unit) context.
|
|
|
|
static void AddInterfaceResults(DeclContext *Ctx, DeclContext *CurContext,
|
|
|
|
bool OnlyForwardDeclarations,
|
|
|
|
bool OnlyUnimplemented,
|
|
|
|
ResultBuilder &Results) {
|
|
|
|
typedef CodeCompleteConsumer::Result Result;
|
|
|
|
|
|
|
|
for (DeclContext::decl_iterator D = Ctx->decls_begin(),
|
|
|
|
DEnd = Ctx->decls_end();
|
|
|
|
D != DEnd; ++D) {
|
|
|
|
// Record any interfaces we find.
|
|
|
|
if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(*D))
|
|
|
|
if ((!OnlyForwardDeclarations || Class->isForwardDecl()) &&
|
|
|
|
(!OnlyUnimplemented || !Class->getImplementation()))
|
|
|
|
Results.MaybeAddResult(Result(Class, 0), CurContext);
|
|
|
|
|
|
|
|
// Record any forward-declared interfaces we find.
|
|
|
|
if (ObjCClassDecl *Forward = dyn_cast<ObjCClassDecl>(*D)) {
|
|
|
|
for (ObjCClassDecl::iterator C = Forward->begin(), CEnd = Forward->end();
|
|
|
|
C != CEnd; ++C)
|
|
|
|
if ((!OnlyForwardDeclarations || C->getInterface()->isForwardDecl()) &&
|
|
|
|
(!OnlyUnimplemented || !C->getInterface()->getImplementation()))
|
|
|
|
Results.MaybeAddResult(Result(C->getInterface(), 0), CurContext);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sema::CodeCompleteObjCInterfaceDecl(Scope *S) {
|
|
|
|
ResultBuilder Results(*this);
|
|
|
|
Results.EnterNewScope();
|
|
|
|
|
|
|
|
// Add all classes.
|
|
|
|
AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, true,
|
|
|
|
false, Results);
|
|
|
|
|
|
|
|
Results.ExitScope();
|
|
|
|
HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sema::CodeCompleteObjCSuperclass(Scope *S, IdentifierInfo *ClassName) {
|
|
|
|
ResultBuilder Results(*this);
|
|
|
|
Results.EnterNewScope();
|
|
|
|
|
|
|
|
// Make sure that we ignore the class we're currently defining.
|
|
|
|
NamedDecl *CurClass
|
|
|
|
= LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
|
2009-11-19 03:08:43 +08:00
|
|
|
if (CurClass && isa<ObjCInterfaceDecl>(CurClass))
|
2009-11-19 00:26:39 +08:00
|
|
|
Results.Ignore(CurClass);
|
|
|
|
|
|
|
|
// Add all classes.
|
|
|
|
AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false,
|
|
|
|
false, Results);
|
|
|
|
|
|
|
|
Results.ExitScope();
|
|
|
|
HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sema::CodeCompleteObjCImplementationDecl(Scope *S) {
|
|
|
|
ResultBuilder Results(*this);
|
|
|
|
Results.EnterNewScope();
|
|
|
|
|
|
|
|
// Add all unimplemented classes.
|
|
|
|
AddInterfaceResults(Context.getTranslationUnitDecl(), CurContext, false,
|
|
|
|
true, Results);
|
|
|
|
|
|
|
|
Results.ExitScope();
|
|
|
|
HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
|
|
|
|
}
|
2009-11-19 03:08:43 +08:00
|
|
|
|
|
|
|
void Sema::CodeCompleteObjCInterfaceCategory(Scope *S,
|
|
|
|
IdentifierInfo *ClassName) {
|
|
|
|
typedef CodeCompleteConsumer::Result Result;
|
|
|
|
|
|
|
|
ResultBuilder Results(*this);
|
|
|
|
|
|
|
|
// Ignore any categories we find that have already been implemented by this
|
|
|
|
// interface.
|
|
|
|
llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames;
|
|
|
|
NamedDecl *CurClass
|
|
|
|
= LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
|
|
|
|
if (ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass))
|
|
|
|
for (ObjCCategoryDecl *Category = Class->getCategoryList(); Category;
|
|
|
|
Category = Category->getNextClassCategory())
|
|
|
|
CategoryNames.insert(Category->getIdentifier());
|
|
|
|
|
|
|
|
// Add all of the categories we know about.
|
|
|
|
Results.EnterNewScope();
|
|
|
|
TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
|
|
|
|
for (DeclContext::decl_iterator D = TU->decls_begin(),
|
|
|
|
DEnd = TU->decls_end();
|
|
|
|
D != DEnd; ++D)
|
|
|
|
if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(*D))
|
|
|
|
if (CategoryNames.insert(Category->getIdentifier()))
|
|
|
|
Results.MaybeAddResult(Result(Category, 0), CurContext);
|
|
|
|
Results.ExitScope();
|
|
|
|
|
|
|
|
HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sema::CodeCompleteObjCImplementationCategory(Scope *S,
|
|
|
|
IdentifierInfo *ClassName) {
|
|
|
|
typedef CodeCompleteConsumer::Result Result;
|
|
|
|
|
|
|
|
// Find the corresponding interface. If we couldn't find the interface, the
|
|
|
|
// program itself is ill-formed. However, we'll try to be helpful still by
|
|
|
|
// providing the list of all of the categories we know about.
|
|
|
|
NamedDecl *CurClass
|
|
|
|
= LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
|
|
|
|
ObjCInterfaceDecl *Class = dyn_cast_or_null<ObjCInterfaceDecl>(CurClass);
|
|
|
|
if (!Class)
|
|
|
|
return CodeCompleteObjCInterfaceCategory(S, ClassName);
|
|
|
|
|
|
|
|
ResultBuilder Results(*this);
|
|
|
|
|
|
|
|
// Add all of the categories that have have corresponding interface
|
|
|
|
// declarations in this class and any of its superclasses, except for
|
|
|
|
// already-implemented categories in the class itself.
|
|
|
|
llvm::SmallPtrSet<IdentifierInfo *, 16> CategoryNames;
|
|
|
|
Results.EnterNewScope();
|
|
|
|
bool IgnoreImplemented = true;
|
|
|
|
while (Class) {
|
|
|
|
for (ObjCCategoryDecl *Category = Class->getCategoryList(); Category;
|
|
|
|
Category = Category->getNextClassCategory())
|
|
|
|
if ((!IgnoreImplemented || !Category->getImplementation()) &&
|
|
|
|
CategoryNames.insert(Category->getIdentifier()))
|
|
|
|
Results.MaybeAddResult(Result(Category, 0), CurContext);
|
|
|
|
|
|
|
|
Class = Class->getSuperClass();
|
|
|
|
IgnoreImplemented = false;
|
|
|
|
}
|
|
|
|
Results.ExitScope();
|
|
|
|
|
|
|
|
HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
|
|
|
|
}
|
2009-11-19 06:32:06 +08:00
|
|
|
|
2009-11-19 06:56:13 +08:00
|
|
|
void Sema::CodeCompleteObjCPropertyDefinition(Scope *S, DeclPtrTy ObjCImpDecl) {
|
2009-11-19 06:32:06 +08:00
|
|
|
typedef CodeCompleteConsumer::Result Result;
|
|
|
|
ResultBuilder Results(*this);
|
|
|
|
|
|
|
|
// Figure out where this @synthesize lives.
|
|
|
|
ObjCContainerDecl *Container
|
|
|
|
= dyn_cast_or_null<ObjCContainerDecl>(ObjCImpDecl.getAs<Decl>());
|
|
|
|
if (!Container ||
|
|
|
|
(!isa<ObjCImplementationDecl>(Container) &&
|
|
|
|
!isa<ObjCCategoryImplDecl>(Container)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Ignore any properties that have already been implemented.
|
|
|
|
for (DeclContext::decl_iterator D = Container->decls_begin(),
|
|
|
|
DEnd = Container->decls_end();
|
|
|
|
D != DEnd; ++D)
|
|
|
|
if (ObjCPropertyImplDecl *PropertyImpl = dyn_cast<ObjCPropertyImplDecl>(*D))
|
|
|
|
Results.Ignore(PropertyImpl->getPropertyDecl());
|
|
|
|
|
|
|
|
// Add any properties that we find.
|
|
|
|
Results.EnterNewScope();
|
|
|
|
if (ObjCImplementationDecl *ClassImpl
|
|
|
|
= dyn_cast<ObjCImplementationDecl>(Container))
|
|
|
|
AddObjCProperties(ClassImpl->getClassInterface(), false, CurContext,
|
|
|
|
Results);
|
|
|
|
else
|
|
|
|
AddObjCProperties(cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl(),
|
|
|
|
false, CurContext, Results);
|
|
|
|
Results.ExitScope();
|
|
|
|
|
|
|
|
HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sema::CodeCompleteObjCPropertySynthesizeIvar(Scope *S,
|
|
|
|
IdentifierInfo *PropertyName,
|
|
|
|
DeclPtrTy ObjCImpDecl) {
|
|
|
|
typedef CodeCompleteConsumer::Result Result;
|
|
|
|
ResultBuilder Results(*this);
|
|
|
|
|
|
|
|
// Figure out where this @synthesize lives.
|
|
|
|
ObjCContainerDecl *Container
|
|
|
|
= dyn_cast_or_null<ObjCContainerDecl>(ObjCImpDecl.getAs<Decl>());
|
|
|
|
if (!Container ||
|
|
|
|
(!isa<ObjCImplementationDecl>(Container) &&
|
|
|
|
!isa<ObjCCategoryImplDecl>(Container)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Figure out which interface we're looking into.
|
|
|
|
ObjCInterfaceDecl *Class = 0;
|
|
|
|
if (ObjCImplementationDecl *ClassImpl
|
|
|
|
= dyn_cast<ObjCImplementationDecl>(Container))
|
|
|
|
Class = ClassImpl->getClassInterface();
|
|
|
|
else
|
|
|
|
Class = cast<ObjCCategoryImplDecl>(Container)->getCategoryDecl()
|
|
|
|
->getClassInterface();
|
|
|
|
|
|
|
|
// Add all of the instance variables in this class and its superclasses.
|
|
|
|
Results.EnterNewScope();
|
|
|
|
for(; Class; Class = Class->getSuperClass()) {
|
|
|
|
// FIXME: We could screen the type of each ivar for compatibility with
|
|
|
|
// the property, but is that being too paternal?
|
|
|
|
for (ObjCInterfaceDecl::ivar_iterator IVar = Class->ivar_begin(),
|
|
|
|
IVarEnd = Class->ivar_end();
|
|
|
|
IVar != IVarEnd; ++IVar)
|
|
|
|
Results.MaybeAddResult(Result(*IVar, 0), CurContext);
|
|
|
|
}
|
|
|
|
Results.ExitScope();
|
|
|
|
|
|
|
|
HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
|
|
|
|
}
|