2015-10-19 04:32:12 +08:00
|
|
|
//===--------------------- SemaLookup.cpp - Name Lookup ------------------===//
|
2009-01-15 06:20:51 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements name lookup for C, C++, Objective-C, and
|
|
|
|
// Objective-C++.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2015-10-07 07:40:43 +08:00
|
|
|
|
2010-08-13 04:07:10 +08:00
|
|
|
#include "clang/Sema/Lookup.h"
|
2009-01-15 08:26:24 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2015-05-07 11:54:19 +08:00
|
|
|
#include "clang/AST/ASTMutationListener.h"
|
2009-10-07 01:59:45 +08:00
|
|
|
#include "clang/AST/CXXInheritance.h"
|
2009-01-15 06:20:51 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
|
|
|
#include "clang/AST/DeclCXX.h"
|
2012-04-04 05:44:08 +08:00
|
|
|
#include "clang/AST/DeclLookups.h"
|
2009-01-15 06:20:51 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2009-05-12 03:58:34 +08:00
|
|
|
#include "clang/AST/DeclTemplate.h"
|
2009-02-04 08:32:51 +08:00
|
|
|
#include "clang/AST/Expr.h"
|
2009-07-08 18:57:20 +08:00
|
|
|
#include "clang/AST/ExprCXX.h"
|
2009-06-14 09:54:56 +08:00
|
|
|
#include "clang/Basic/Builtins.h"
|
2009-01-15 06:20:51 +08:00
|
|
|
#include "clang/Basic/LangOptions.h"
|
2015-05-16 04:05:43 +08:00
|
|
|
#include "clang/Lex/HeaderSearch.h"
|
2014-05-03 11:45:55 +08:00
|
|
|
#include "clang/Lex/ModuleLoader.h"
|
2015-05-15 10:34:32 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Sema/DeclSpec.h"
|
|
|
|
#include "clang/Sema/ExternalSemaSource.h"
|
|
|
|
#include "clang/Sema/Overload.h"
|
|
|
|
#include "clang/Sema/Scope.h"
|
|
|
|
#include "clang/Sema/ScopeInfo.h"
|
|
|
|
#include "clang/Sema/Sema.h"
|
|
|
|
#include "clang/Sema/SemaInternal.h"
|
|
|
|
#include "clang/Sema/TemplateDeduction.h"
|
|
|
|
#include "clang/Sema/TypoCorrection.h"
|
2009-01-15 06:20:51 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/ADT/SetVector.h"
|
2009-02-04 08:32:51 +08:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2010-10-15 04:34:08 +08:00
|
|
|
#include "llvm/ADT/StringMap.h"
|
2011-07-18 09:54:02 +08:00
|
|
|
#include "llvm/ADT/TinyPtrVector.h"
|
2012-02-16 06:14:18 +08:00
|
|
|
#include "llvm/ADT/edit_distance.h"
|
2009-10-10 13:48:19 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2012-04-04 04:26:45 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <iterator>
|
2010-10-15 04:34:08 +08:00
|
|
|
#include <limits>
|
2009-12-31 01:04:44 +08:00
|
|
|
#include <list>
|
2012-04-04 04:26:45 +08:00
|
|
|
#include <map>
|
2009-01-16 08:38:09 +08:00
|
|
|
#include <set>
|
2009-02-04 03:21:40 +08:00
|
|
|
#include <utility>
|
2012-04-04 04:26:45 +08:00
|
|
|
#include <vector>
|
2009-01-15 06:20:51 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-08-25 13:32:35 +08:00
|
|
|
using namespace sema;
|
2009-01-15 06:20:51 +08:00
|
|
|
|
2009-11-10 15:01:13 +08:00
|
|
|
namespace {
|
|
|
|
class UnqualUsingEntry {
|
|
|
|
const DeclContext *Nominated;
|
|
|
|
const DeclContext *CommonAncestor;
|
|
|
|
|
|
|
|
public:
|
|
|
|
UnqualUsingEntry(const DeclContext *Nominated,
|
|
|
|
const DeclContext *CommonAncestor)
|
|
|
|
: Nominated(Nominated), CommonAncestor(CommonAncestor) {
|
|
|
|
}
|
2009-02-04 03:21:40 +08:00
|
|
|
|
2009-11-10 15:01:13 +08:00
|
|
|
const DeclContext *getCommonAncestor() const {
|
|
|
|
return CommonAncestor;
|
|
|
|
}
|
2009-02-04 03:21:40 +08:00
|
|
|
|
2009-11-10 15:01:13 +08:00
|
|
|
const DeclContext *getNominatedNamespace() const {
|
|
|
|
return Nominated;
|
|
|
|
}
|
2009-02-04 03:21:40 +08:00
|
|
|
|
2009-11-10 15:01:13 +08:00
|
|
|
// Sort by the pointer value of the common ancestor.
|
|
|
|
struct Comparator {
|
|
|
|
bool operator()(const UnqualUsingEntry &L, const UnqualUsingEntry &R) {
|
|
|
|
return L.getCommonAncestor() < R.getCommonAncestor();
|
|
|
|
}
|
2009-02-04 03:21:40 +08:00
|
|
|
|
2009-11-10 15:01:13 +08:00
|
|
|
bool operator()(const UnqualUsingEntry &E, const DeclContext *DC) {
|
|
|
|
return E.getCommonAncestor() < DC;
|
|
|
|
}
|
2009-02-04 03:21:40 +08:00
|
|
|
|
2009-11-10 15:01:13 +08:00
|
|
|
bool operator()(const DeclContext *DC, const UnqualUsingEntry &E) {
|
|
|
|
return DC < E.getCommonAncestor();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/// A collection of using directives, as used by C++ unqualified
|
|
|
|
/// lookup.
|
|
|
|
class UnqualUsingDirectiveSet {
|
2011-07-23 18:55:15 +08:00
|
|
|
typedef SmallVector<UnqualUsingEntry, 8> ListTy;
|
2009-11-10 15:01:13 +08:00
|
|
|
|
|
|
|
ListTy list;
|
|
|
|
llvm::SmallPtrSet<DeclContext*, 8> visited;
|
|
|
|
|
|
|
|
public:
|
|
|
|
UnqualUsingDirectiveSet() {}
|
|
|
|
|
|
|
|
void visitScopeChain(Scope *S, Scope *InnermostFileScope) {
|
2011-01-27 15:10:08 +08:00
|
|
|
// C++ [namespace.udir]p1:
|
2009-11-10 15:01:13 +08:00
|
|
|
// During unqualified name lookup, the names appear as if they
|
|
|
|
// were declared in the nearest enclosing namespace which contains
|
|
|
|
// both the using-directive and the nominated namespace.
|
2013-10-09 01:08:03 +08:00
|
|
|
DeclContext *InnermostFileDC = InnermostFileScope->getEntity();
|
2009-11-10 15:01:13 +08:00
|
|
|
assert(InnermostFileDC && InnermostFileDC->isFileContext());
|
|
|
|
|
|
|
|
for (; S; S = S->getParent()) {
|
2012-03-13 12:12:34 +08:00
|
|
|
// C++ [namespace.udir]p1:
|
|
|
|
// A using-directive shall not appear in class scope, but may
|
|
|
|
// appear in namespace scope or in block scope.
|
2013-10-09 01:08:03 +08:00
|
|
|
DeclContext *Ctx = S->getEntity();
|
2012-03-13 12:12:34 +08:00
|
|
|
if (Ctx && Ctx->isFileContext()) {
|
|
|
|
visit(Ctx, Ctx);
|
|
|
|
} else if (!Ctx || Ctx->isFunctionOrMethod()) {
|
2014-03-18 01:03:37 +08:00
|
|
|
for (auto *I : S->using_directives())
|
|
|
|
visit(I, InnermostFileDC);
|
2009-11-10 15:01:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-02-04 03:21:40 +08:00
|
|
|
|
2009-11-10 15:01:13 +08:00
|
|
|
// Visits a context and collect all of its using directives
|
|
|
|
// recursively. Treats all using directives as if they were
|
|
|
|
// declared in the context.
|
|
|
|
//
|
|
|
|
// A given context is only every visited once, so it is important
|
|
|
|
// that contexts be visited from the inside out in order to get
|
|
|
|
// the effective DCs right.
|
|
|
|
void visit(DeclContext *DC, DeclContext *EffectiveDC) {
|
2014-11-19 15:49:47 +08:00
|
|
|
if (!visited.insert(DC).second)
|
2009-11-10 15:01:13 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
addUsingDirectives(DC, EffectiveDC);
|
|
|
|
}
|
2009-02-04 03:21:40 +08:00
|
|
|
|
2009-11-10 15:01:13 +08:00
|
|
|
// Visits a using directive and collects all of its using
|
|
|
|
// directives recursively. Treats all using directives as if they
|
|
|
|
// were declared in the effective DC.
|
|
|
|
void visit(UsingDirectiveDecl *UD, DeclContext *EffectiveDC) {
|
|
|
|
DeclContext *NS = UD->getNominatedNamespace();
|
2014-11-19 15:49:47 +08:00
|
|
|
if (!visited.insert(NS).second)
|
2009-11-10 15:01:13 +08:00
|
|
|
return;
|
2009-02-04 03:21:40 +08:00
|
|
|
|
2009-11-10 15:01:13 +08:00
|
|
|
addUsingDirective(UD, EffectiveDC);
|
|
|
|
addUsingDirectives(NS, EffectiveDC);
|
|
|
|
}
|
2009-02-04 03:21:40 +08:00
|
|
|
|
2009-11-10 15:01:13 +08:00
|
|
|
// Adds all the using directives in a context (and those nominated
|
|
|
|
// by its using directives, transitively) as if they appeared in
|
|
|
|
// the given effective context.
|
|
|
|
void addUsingDirectives(DeclContext *DC, DeclContext *EffectiveDC) {
|
2015-10-19 04:32:12 +08:00
|
|
|
SmallVector<DeclContext*, 4> queue;
|
2009-11-10 15:01:13 +08:00
|
|
|
while (true) {
|
2014-03-18 01:14:12 +08:00
|
|
|
for (auto UD : DC->using_directives()) {
|
2009-11-10 15:01:13 +08:00
|
|
|
DeclContext *NS = UD->getNominatedNamespace();
|
2014-11-19 15:49:47 +08:00
|
|
|
if (visited.insert(NS).second) {
|
2009-11-10 15:01:13 +08:00
|
|
|
addUsingDirective(UD, EffectiveDC);
|
|
|
|
queue.push_back(NS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (queue.empty())
|
|
|
|
return;
|
|
|
|
|
2013-08-24 00:11:15 +08:00
|
|
|
DC = queue.pop_back_val();
|
2009-02-04 03:21:40 +08:00
|
|
|
}
|
|
|
|
}
|
2009-11-10 15:01:13 +08:00
|
|
|
|
|
|
|
// Add a using directive as if it had been declared in the given
|
|
|
|
// context. This helps implement C++ [namespace.udir]p3:
|
|
|
|
// The using-directive is transitive: if a scope contains a
|
|
|
|
// using-directive that nominates a second namespace that itself
|
|
|
|
// contains using-directives, the effect is as if the
|
|
|
|
// using-directives from the second namespace also appeared in
|
|
|
|
// the first.
|
|
|
|
void addUsingDirective(UsingDirectiveDecl *UD, DeclContext *EffectiveDC) {
|
|
|
|
// Find the common ancestor between the effective context and
|
|
|
|
// the nominated namespace.
|
|
|
|
DeclContext *Common = UD->getNominatedNamespace();
|
|
|
|
while (!Common->Encloses(EffectiveDC))
|
|
|
|
Common = Common->getParent();
|
2009-11-10 17:20:04 +08:00
|
|
|
Common = Common->getPrimaryContext();
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2009-11-10 15:01:13 +08:00
|
|
|
list.push_back(UnqualUsingEntry(UD->getNominatedNamespace(), Common));
|
|
|
|
}
|
|
|
|
|
|
|
|
void done() {
|
|
|
|
std::sort(list.begin(), list.end(), UnqualUsingEntry::Comparator());
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef ListTy::const_iterator const_iterator;
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2009-11-10 15:01:13 +08:00
|
|
|
const_iterator begin() const { return list.begin(); }
|
|
|
|
const_iterator end() const { return list.end(); }
|
|
|
|
|
2015-02-07 01:25:10 +08:00
|
|
|
llvm::iterator_range<const_iterator>
|
2009-11-10 15:01:13 +08:00
|
|
|
getNamespacesFor(DeclContext *DC) const {
|
2015-02-07 01:25:10 +08:00
|
|
|
return llvm::make_range(std::equal_range(begin(), end(),
|
|
|
|
DC->getPrimaryContext(),
|
|
|
|
UnqualUsingEntry::Comparator()));
|
2009-11-10 15:01:13 +08:00
|
|
|
}
|
|
|
|
};
|
2015-10-07 07:40:43 +08:00
|
|
|
} // end anonymous namespace
|
2009-02-04 03:21:40 +08:00
|
|
|
|
|
|
|
// Retrieve the set of identifier namespaces that correspond to a
|
|
|
|
// specific kind of name lookup.
|
2009-12-18 18:40:03 +08:00
|
|
|
static inline unsigned getIDNS(Sema::LookupNameKind NameKind,
|
|
|
|
bool CPlusPlus,
|
|
|
|
bool Redeclaration) {
|
2009-02-04 03:21:40 +08:00
|
|
|
unsigned IDNS = 0;
|
|
|
|
switch (NameKind) {
|
2011-07-13 01:16:56 +08:00
|
|
|
case Sema::LookupObjCImplicitSelfParam:
|
2009-02-04 03:21:40 +08:00
|
|
|
case Sema::LookupOrdinaryName:
|
2009-02-25 04:03:32 +08:00
|
|
|
case Sema::LookupRedeclarationWithLinkage:
|
2013-08-09 12:35:01 +08:00
|
|
|
case Sema::LookupLocalFriendName:
|
2009-02-04 03:21:40 +08:00
|
|
|
IDNS = Decl::IDNS_Ordinary;
|
2009-12-18 18:40:03 +08:00
|
|
|
if (CPlusPlus) {
|
2010-04-24 02:46:30 +08:00
|
|
|
IDNS |= Decl::IDNS_Tag | Decl::IDNS_Member | Decl::IDNS_Namespace;
|
2011-02-18 09:27:55 +08:00
|
|
|
if (Redeclaration)
|
|
|
|
IDNS |= Decl::IDNS_TagFriend | Decl::IDNS_OrdinaryFriend;
|
2009-12-18 18:40:03 +08:00
|
|
|
}
|
2013-09-20 09:15:31 +08:00
|
|
|
if (Redeclaration)
|
|
|
|
IDNS |= Decl::IDNS_LocalExtern;
|
2009-02-04 03:21:40 +08:00
|
|
|
break;
|
|
|
|
|
2010-04-24 09:30:58 +08:00
|
|
|
case Sema::LookupOperatorName:
|
|
|
|
// Operator lookup is its own crazy thing; it is not the same
|
|
|
|
// as (e.g.) looking up an operator name for redeclaration.
|
|
|
|
assert(!Redeclaration && "cannot do redeclaration operator lookup");
|
|
|
|
IDNS = Decl::IDNS_NonMemberOperator;
|
|
|
|
break;
|
|
|
|
|
2009-02-04 03:21:40 +08:00
|
|
|
case Sema::LookupTagName:
|
2010-04-24 02:46:30 +08:00
|
|
|
if (CPlusPlus) {
|
|
|
|
IDNS = Decl::IDNS_Type;
|
|
|
|
|
|
|
|
// When looking for a redeclaration of a tag name, we add:
|
|
|
|
// 1) TagFriend to find undeclared friend decls
|
|
|
|
// 2) Namespace because they can't "overload" with tag decls.
|
|
|
|
// 3) Tag because it includes class templates, which can't
|
|
|
|
// "overload" with tag decls.
|
|
|
|
if (Redeclaration)
|
|
|
|
IDNS |= Decl::IDNS_Tag | Decl::IDNS_TagFriend | Decl::IDNS_Namespace;
|
|
|
|
} else {
|
|
|
|
IDNS = Decl::IDNS_Tag;
|
|
|
|
}
|
2009-02-04 03:21:40 +08:00
|
|
|
break;
|
2014-04-11 09:03:38 +08:00
|
|
|
|
2011-02-18 09:27:55 +08:00
|
|
|
case Sema::LookupLabel:
|
|
|
|
IDNS = Decl::IDNS_Label;
|
|
|
|
break;
|
2014-04-11 09:03:38 +08:00
|
|
|
|
2009-02-04 03:21:40 +08:00
|
|
|
case Sema::LookupMemberName:
|
|
|
|
IDNS = Decl::IDNS_Member;
|
|
|
|
if (CPlusPlus)
|
2009-09-09 23:08:12 +08:00
|
|
|
IDNS |= Decl::IDNS_Tag | Decl::IDNS_Ordinary;
|
2009-02-04 03:21:40 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Sema::LookupNestedNameSpecifierName:
|
2010-04-24 02:46:30 +08:00
|
|
|
IDNS = Decl::IDNS_Type | Decl::IDNS_Namespace;
|
|
|
|
break;
|
|
|
|
|
2009-02-04 03:21:40 +08:00
|
|
|
case Sema::LookupNamespaceName:
|
2010-04-24 02:46:30 +08:00
|
|
|
IDNS = Decl::IDNS_Namespace;
|
2009-02-04 03:21:40 +08:00
|
|
|
break;
|
2009-04-24 07:18:26 +08:00
|
|
|
|
2009-12-10 17:41:52 +08:00
|
|
|
case Sema::LookupUsingDeclName:
|
2014-04-11 09:03:38 +08:00
|
|
|
assert(Redeclaration && "should only be used for redecl lookup");
|
|
|
|
IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Member |
|
|
|
|
Decl::IDNS_Using | Decl::IDNS_TagFriend | Decl::IDNS_OrdinaryFriend |
|
|
|
|
Decl::IDNS_LocalExtern;
|
2009-12-10 17:41:52 +08:00
|
|
|
break;
|
|
|
|
|
2009-04-24 08:11:27 +08:00
|
|
|
case Sema::LookupObjCProtocolName:
|
|
|
|
IDNS = Decl::IDNS_ObjCProtocol;
|
|
|
|
break;
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2010-08-15 14:18:01 +08:00
|
|
|
case Sema::LookupAnyName:
|
2011-01-27 15:10:08 +08:00
|
|
|
IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Member
|
2010-08-15 14:18:01 +08:00
|
|
|
| Decl::IDNS_Using | Decl::IDNS_Namespace | Decl::IDNS_ObjCProtocol
|
|
|
|
| Decl::IDNS_Type;
|
|
|
|
break;
|
2009-02-04 03:21:40 +08:00
|
|
|
}
|
|
|
|
return IDNS;
|
|
|
|
}
|
|
|
|
|
2009-12-18 18:40:03 +08:00
|
|
|
void LookupResult::configure() {
|
2014-11-12 07:00:42 +08:00
|
|
|
IDNS = getIDNS(LookupKind, getSema().getLangOpts().CPlusPlus,
|
2009-12-18 18:40:03 +08:00
|
|
|
isForRedeclaration());
|
2010-03-24 13:07:21 +08:00
|
|
|
|
2014-02-04 09:14:30 +08:00
|
|
|
// If we're looking for one of the allocation or deallocation
|
|
|
|
// operators, make sure that the implicitly-declared new and delete
|
|
|
|
// operators can be found.
|
|
|
|
switch (NameInfo.getName().getCXXOverloadedOperator()) {
|
|
|
|
case OO_New:
|
|
|
|
case OO_Delete:
|
|
|
|
case OO_Array_New:
|
|
|
|
case OO_Array_Delete:
|
2014-11-12 07:00:42 +08:00
|
|
|
getSema().DeclareGlobalNewDelete();
|
2014-02-04 09:14:30 +08:00
|
|
|
break;
|
2013-04-04 07:06:26 +08:00
|
|
|
|
2014-02-04 09:14:30 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compiler builtins are always visible, regardless of where they end
|
|
|
|
// up being declared.
|
|
|
|
if (IdentifierInfo *Id = NameInfo.getName().getAsIdentifierInfo()) {
|
|
|
|
if (unsigned BuiltinID = Id->getBuiltinID()) {
|
2014-11-12 07:00:42 +08:00
|
|
|
if (!getSema().Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
|
2014-02-04 09:14:30 +08:00
|
|
|
AllowHidden = true;
|
2013-04-04 07:06:26 +08:00
|
|
|
}
|
2010-03-24 13:07:21 +08:00
|
|
|
}
|
2009-12-18 18:40:03 +08:00
|
|
|
}
|
|
|
|
|
2013-12-07 21:51:35 +08:00
|
|
|
bool LookupResult::sanity() const {
|
2014-04-02 02:33:50 +08:00
|
|
|
// This function is never called by NDEBUG builds.
|
2010-08-25 13:32:35 +08:00
|
|
|
assert(ResultKind != NotFound || Decls.size() == 0);
|
|
|
|
assert(ResultKind != Found || Decls.size() == 1);
|
|
|
|
assert(ResultKind != FoundOverloaded || Decls.size() > 1 ||
|
|
|
|
(Decls.size() == 1 &&
|
|
|
|
isa<FunctionTemplateDecl>((*begin())->getUnderlyingDecl())));
|
|
|
|
assert(ResultKind != FoundUnresolvedValue || sanityCheckUnresolved());
|
|
|
|
assert(ResultKind != Ambiguous || Decls.size() > 1 ||
|
2010-10-23 06:08:47 +08:00
|
|
|
(Decls.size() == 1 && (Ambiguity == AmbiguousBaseSubobjects ||
|
|
|
|
Ambiguity == AmbiguousBaseSubobjectTypes)));
|
2014-05-26 14:22:03 +08:00
|
|
|
assert((Paths != nullptr) == (ResultKind == Ambiguous &&
|
|
|
|
(Ambiguity == AmbiguousBaseSubobjectTypes ||
|
|
|
|
Ambiguity == AmbiguousBaseSubobjects)));
|
2013-12-07 21:51:35 +08:00
|
|
|
return true;
|
2010-08-25 13:32:35 +08:00
|
|
|
}
|
|
|
|
|
2009-10-10 05:13:30 +08:00
|
|
|
// Necessary because CXXBasePaths is not complete in Sema.h
|
2009-11-18 15:57:50 +08:00
|
|
|
void LookupResult::deletePaths(CXXBasePaths *Paths) {
|
2009-10-10 05:13:30 +08:00
|
|
|
delete Paths;
|
2009-02-04 03:21:40 +08:00
|
|
|
}
|
|
|
|
|
2013-10-30 09:02:04 +08:00
|
|
|
/// Get a representative context for a declaration such that two declarations
|
|
|
|
/// will have the same context if they were found within the same scope.
|
2013-11-01 19:50:55 +08:00
|
|
|
static DeclContext *getContextForScopeMatching(Decl *D) {
|
2013-10-30 09:02:04 +08:00
|
|
|
// For function-local declarations, use that function as the context. This
|
|
|
|
// doesn't account for scopes within the function; the caller must deal with
|
|
|
|
// those.
|
|
|
|
DeclContext *DC = D->getLexicalDeclContext();
|
|
|
|
if (DC->isFunctionOrMethod())
|
|
|
|
return DC;
|
|
|
|
|
|
|
|
// Otherwise, look at the semantic context of the declaration. The
|
|
|
|
// declaration must have been found there.
|
|
|
|
return D->getDeclContext()->getRedeclContext();
|
|
|
|
}
|
|
|
|
|
2015-07-30 07:38:25 +08:00
|
|
|
/// \brief Determine whether \p D is a better lookup result than \p Existing,
|
|
|
|
/// given that they declare the same entity.
|
2015-08-23 05:37:34 +08:00
|
|
|
static bool isPreferredLookupResult(Sema &S, Sema::LookupNameKind Kind,
|
2015-07-30 07:38:25 +08:00
|
|
|
NamedDecl *D, NamedDecl *Existing) {
|
|
|
|
// When looking up redeclarations of a using declaration, prefer a using
|
|
|
|
// shadow declaration over any other declaration of the same entity.
|
|
|
|
if (Kind == Sema::LookupUsingDeclName && isa<UsingShadowDecl>(D) &&
|
|
|
|
!isa<UsingShadowDecl>(Existing))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
auto *DUnderlying = D->getUnderlyingDecl();
|
|
|
|
auto *EUnderlying = Existing->getUnderlyingDecl();
|
|
|
|
|
2015-11-13 06:04:34 +08:00
|
|
|
// If they have different underlying declarations, prefer a typedef over the
|
|
|
|
// original type (this happens when two type declarations denote the same
|
|
|
|
// type), per a generous reading of C++ [dcl.typedef]p3 and p4. The typedef
|
|
|
|
// might carry additional semantic information, such as an alignment override.
|
|
|
|
// However, per C++ [dcl.typedef]p5, when looking up a tag name, prefer a tag
|
|
|
|
// declaration over a typedef.
|
2015-07-30 07:38:25 +08:00
|
|
|
if (DUnderlying->getCanonicalDecl() != EUnderlying->getCanonicalDecl()) {
|
|
|
|
assert(isa<TypeDecl>(DUnderlying) && isa<TypeDecl>(EUnderlying));
|
2015-11-13 06:04:34 +08:00
|
|
|
bool HaveTag = isa<TagDecl>(EUnderlying);
|
|
|
|
bool WantTag = Kind == Sema::LookupTagName;
|
|
|
|
return HaveTag != WantTag;
|
2015-07-30 07:38:25 +08:00
|
|
|
}
|
|
|
|
|
2015-08-23 05:37:34 +08:00
|
|
|
// Pick the function with more default arguments.
|
|
|
|
// FIXME: In the presence of ambiguous default arguments, we should keep both,
|
|
|
|
// so we can diagnose the ambiguity if the default argument is needed.
|
|
|
|
// See C++ [over.match.best]p3.
|
|
|
|
if (auto *DFD = dyn_cast<FunctionDecl>(DUnderlying)) {
|
|
|
|
auto *EFD = cast<FunctionDecl>(EUnderlying);
|
|
|
|
unsigned DMin = DFD->getMinRequiredArguments();
|
|
|
|
unsigned EMin = EFD->getMinRequiredArguments();
|
|
|
|
// If D has more default arguments, it is preferred.
|
|
|
|
if (DMin != EMin)
|
|
|
|
return DMin < EMin;
|
2015-09-12 06:39:35 +08:00
|
|
|
// FIXME: When we track visibility for default function arguments, check
|
|
|
|
// that we pick the declaration with more visible default arguments.
|
2015-08-23 05:37:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pick the template with more default template arguments.
|
|
|
|
if (auto *DTD = dyn_cast<TemplateDecl>(DUnderlying)) {
|
|
|
|
auto *ETD = cast<TemplateDecl>(EUnderlying);
|
|
|
|
unsigned DMin = DTD->getTemplateParameters()->getMinRequiredArguments();
|
|
|
|
unsigned EMin = ETD->getTemplateParameters()->getMinRequiredArguments();
|
2015-09-12 06:39:35 +08:00
|
|
|
// If D has more default arguments, it is preferred. Note that default
|
|
|
|
// arguments (and their visibility) is monotonically increasing across the
|
|
|
|
// redeclaration chain, so this is a quick proxy for "is more recent".
|
2015-08-23 05:37:34 +08:00
|
|
|
if (DMin != EMin)
|
|
|
|
return DMin < EMin;
|
2015-09-12 06:39:35 +08:00
|
|
|
// If D has more *visible* default arguments, it is preferred. Note, an
|
|
|
|
// earlier default argument being visible does not imply that a later
|
|
|
|
// default argument is visible, so we can't just check the first one.
|
|
|
|
for (unsigned I = DMin, N = DTD->getTemplateParameters()->size();
|
|
|
|
I != N; ++I) {
|
|
|
|
if (!S.hasVisibleDefaultArgument(
|
|
|
|
ETD->getTemplateParameters()->getParam(I)) &&
|
|
|
|
S.hasVisibleDefaultArgument(
|
|
|
|
DTD->getTemplateParameters()->getParam(I)))
|
|
|
|
return true;
|
|
|
|
}
|
2015-08-23 05:37:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// For most kinds of declaration, it doesn't really matter which one we pick.
|
|
|
|
if (!isa<FunctionDecl>(DUnderlying) && !isa<VarDecl>(DUnderlying)) {
|
|
|
|
// If the existing declaration is hidden, prefer the new one. Otherwise,
|
|
|
|
// keep what we've got.
|
|
|
|
return !S.isVisible(Existing);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pick the newer declaration; it might have a more precise type.
|
2015-07-30 07:38:25 +08:00
|
|
|
for (Decl *Prev = DUnderlying->getPreviousDecl(); Prev;
|
|
|
|
Prev = Prev->getPreviousDecl())
|
|
|
|
if (Prev == EUnderlying)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-13 06:04:34 +08:00
|
|
|
/// Determine whether \p D can hide a tag declaration.
|
|
|
|
static bool canHideTag(NamedDecl *D) {
|
|
|
|
// C++ [basic.scope.declarative]p4:
|
|
|
|
// Given a set of declarations in a single declarative region [...]
|
|
|
|
// exactly one declaration shall declare a class name or enumeration name
|
|
|
|
// that is not a typedef name and the other declarations shall all refer to
|
|
|
|
// the same variable or enumerator, or all refer to functions and function
|
|
|
|
// templates; in this case the class name or enumeration name is hidden.
|
|
|
|
// C++ [basic.scope.hiding]p2:
|
|
|
|
// A class name or enumeration name can be hidden by the name of a
|
|
|
|
// variable, data member, function, or enumerator declared in the same
|
|
|
|
// scope.
|
|
|
|
D = D->getUnderlyingDecl();
|
|
|
|
return isa<VarDecl>(D) || isa<EnumConstantDecl>(D) || isa<FunctionDecl>(D) ||
|
|
|
|
isa<FunctionTemplateDecl>(D) || isa<FieldDecl>(D);
|
|
|
|
}
|
|
|
|
|
2009-11-22 08:44:51 +08:00
|
|
|
/// Resolves the result kind of this lookup.
|
2009-11-18 15:57:50 +08:00
|
|
|
void LookupResult::resolveKind() {
|
2009-10-10 05:13:30 +08:00
|
|
|
unsigned N = Decls.size();
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2009-10-10 05:13:30 +08:00
|
|
|
// Fast case: no possible ambiguity.
|
2009-11-19 06:49:29 +08:00
|
|
|
if (N == 0) {
|
2015-07-30 07:38:25 +08:00
|
|
|
assert(ResultKind == NotFound ||
|
|
|
|
ResultKind == NotFoundInCurrentInstantiation);
|
2009-11-19 06:49:29 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-11-22 08:44:51 +08:00
|
|
|
// If there's a single decl, we need to examine it to decide what
|
|
|
|
// kind of lookup this is.
|
2009-11-18 10:36:19 +08:00
|
|
|
if (N == 1) {
|
2010-04-26 05:15:30 +08:00
|
|
|
NamedDecl *D = (*Decls.begin())->getUnderlyingDecl();
|
|
|
|
if (isa<FunctionTemplateDecl>(D))
|
2009-11-22 08:44:51 +08:00
|
|
|
ResultKind = FoundOverloaded;
|
2010-04-26 05:15:30 +08:00
|
|
|
else if (isa<UnresolvedUsingValueDecl>(D))
|
2009-11-18 10:36:19 +08:00
|
|
|
ResultKind = FoundUnresolvedValue;
|
|
|
|
return;
|
|
|
|
}
|
2009-06-26 11:37:05 +08:00
|
|
|
|
2009-10-10 13:48:19 +08:00
|
|
|
// Don't do any extra resolution if we've already resolved as ambiguous.
|
2009-11-17 10:14:36 +08:00
|
|
|
if (ResultKind == Ambiguous) return;
|
2009-10-10 13:48:19 +08:00
|
|
|
|
2015-07-22 07:54:07 +08:00
|
|
|
llvm::SmallDenseMap<NamedDecl*, unsigned, 16> Unique;
|
2015-07-30 07:38:25 +08:00
|
|
|
llvm::SmallDenseMap<QualType, unsigned, 16> UniqueTypes;
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2009-10-10 05:13:30 +08:00
|
|
|
bool Ambiguous = false;
|
2015-11-05 03:26:32 +08:00
|
|
|
bool HasTag = false, HasFunction = false;
|
2009-11-22 08:44:51 +08:00
|
|
|
bool HasFunctionTemplate = false, HasUnresolved = false;
|
2015-11-05 03:26:32 +08:00
|
|
|
NamedDecl *HasNonFunction = nullptr;
|
|
|
|
|
|
|
|
llvm::SmallVector<NamedDecl*, 4> EquivalentNonFunctions;
|
2009-01-15 08:26:24 +08:00
|
|
|
|
2009-10-10 05:13:30 +08:00
|
|
|
unsigned UniqueTagIndex = 0;
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2009-10-10 05:13:30 +08:00
|
|
|
unsigned I = 0;
|
|
|
|
while (I < N) {
|
2009-11-17 15:50:12 +08:00
|
|
|
NamedDecl *D = Decls[I]->getUnderlyingDecl();
|
|
|
|
D = cast<NamedDecl>(D->getCanonicalDecl());
|
2009-10-10 05:13:30 +08:00
|
|
|
|
2013-02-22 14:58:37 +08:00
|
|
|
// Ignore an invalid declaration unless it's the only one left.
|
2015-11-03 11:13:11 +08:00
|
|
|
if (D->isInvalidDecl() && !(I == 0 && N == 1)) {
|
2013-02-22 14:58:37 +08:00
|
|
|
Decls[I] = Decls[--N];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-07-30 07:38:25 +08:00
|
|
|
llvm::Optional<unsigned> ExistingI;
|
|
|
|
|
2010-08-11 22:45:53 +08:00
|
|
|
// Redeclarations of types via typedef can occur both within a scope
|
|
|
|
// and, through using declarations and directives, across scopes. There is
|
|
|
|
// no ambiguity if they all refer to the same type, so unique based on the
|
|
|
|
// canonical type.
|
|
|
|
if (TypeDecl *TD = dyn_cast<TypeDecl>(D)) {
|
2015-11-13 06:04:34 +08:00
|
|
|
QualType T = getSema().Context.getTypeDeclType(TD);
|
|
|
|
auto UniqueResult = UniqueTypes.insert(
|
|
|
|
std::make_pair(getSema().Context.getCanonicalType(T), I));
|
|
|
|
if (!UniqueResult.second) {
|
|
|
|
// The type is not unique.
|
|
|
|
ExistingI = UniqueResult.first->second;
|
2010-08-11 22:45:53 +08:00
|
|
|
}
|
|
|
|
}
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2015-07-30 07:38:25 +08:00
|
|
|
// For non-type declarations, check for a prior lookup result naming this
|
|
|
|
// canonical declaration.
|
|
|
|
if (!ExistingI) {
|
|
|
|
auto UniqueResult = Unique.insert(std::make_pair(D, I));
|
|
|
|
if (!UniqueResult.second) {
|
|
|
|
// We've seen this entity before.
|
|
|
|
ExistingI = UniqueResult.first->second;
|
2015-07-22 07:54:07 +08:00
|
|
|
}
|
2015-07-30 07:38:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ExistingI) {
|
|
|
|
// This is not a unique lookup result. Pick one of the results and
|
|
|
|
// discard the other.
|
2015-08-23 05:37:34 +08:00
|
|
|
if (isPreferredLookupResult(getSema(), getLookupKind(), Decls[I],
|
2015-07-30 07:38:25 +08:00
|
|
|
Decls[*ExistingI]))
|
|
|
|
Decls[*ExistingI] = Decls[I];
|
|
|
|
Decls[I] = Decls[--N];
|
2010-08-11 22:45:53 +08:00
|
|
|
continue;
|
2011-01-27 15:10:08 +08:00
|
|
|
}
|
|
|
|
|
2010-08-11 22:45:53 +08:00
|
|
|
// Otherwise, do some decl type analysis and then continue.
|
|
|
|
|
|
|
|
if (isa<UnresolvedUsingValueDecl>(D)) {
|
|
|
|
HasUnresolved = true;
|
|
|
|
} else if (isa<TagDecl>(D)) {
|
|
|
|
if (HasTag)
|
|
|
|
Ambiguous = true;
|
|
|
|
UniqueTagIndex = I;
|
|
|
|
HasTag = true;
|
|
|
|
} else if (isa<FunctionTemplateDecl>(D)) {
|
|
|
|
HasFunction = true;
|
|
|
|
HasFunctionTemplate = true;
|
|
|
|
} else if (isa<FunctionDecl>(D)) {
|
|
|
|
HasFunction = true;
|
2009-10-10 05:13:30 +08:00
|
|
|
} else {
|
2015-11-05 03:26:32 +08:00
|
|
|
if (HasNonFunction) {
|
|
|
|
// If we're about to create an ambiguity between two declarations that
|
|
|
|
// are equivalent, but one is an internal linkage declaration from one
|
|
|
|
// module and the other is an internal linkage declaration from another
|
|
|
|
// module, just skip it.
|
|
|
|
if (getSema().isEquivalentInternalLinkageDeclaration(HasNonFunction,
|
|
|
|
D)) {
|
|
|
|
EquivalentNonFunctions.push_back(D);
|
|
|
|
Decls[I] = Decls[--N];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-08-11 22:45:53 +08:00
|
|
|
Ambiguous = true;
|
2015-11-05 03:26:32 +08:00
|
|
|
}
|
|
|
|
HasNonFunction = D;
|
2009-01-15 08:26:24 +08:00
|
|
|
}
|
2010-08-11 22:45:53 +08:00
|
|
|
I++;
|
2009-01-15 08:26:24 +08:00
|
|
|
}
|
2009-04-24 10:57:34 +08:00
|
|
|
|
2009-10-10 05:13:30 +08:00
|
|
|
// C++ [basic.scope.hiding]p2:
|
|
|
|
// A class name or enumeration name can be hidden by the name of
|
|
|
|
// an object, function, or enumerator declared in the same
|
|
|
|
// scope. If a class or enumeration name and an object, function,
|
|
|
|
// or enumerator are declared in the same scope (in any order)
|
|
|
|
// with the same name, the class or enumeration name is hidden
|
|
|
|
// wherever the object, function, or enumerator name is visible.
|
|
|
|
// But it's still an error if there are distinct tag types found,
|
|
|
|
// even if they're not visible. (ref?)
|
2015-11-13 06:04:34 +08:00
|
|
|
if (N > 1 && HideTags && HasTag && !Ambiguous &&
|
2010-10-24 00:06:17 +08:00
|
|
|
(HasFunction || HasNonFunction || HasUnresolved)) {
|
2015-11-13 06:04:34 +08:00
|
|
|
NamedDecl *OtherDecl = Decls[UniqueTagIndex ? 0 : N - 1];
|
|
|
|
if (isa<TagDecl>(Decls[UniqueTagIndex]->getUnderlyingDecl()) &&
|
|
|
|
getContextForScopeMatching(Decls[UniqueTagIndex])->Equals(
|
|
|
|
getContextForScopeMatching(OtherDecl)) &&
|
|
|
|
canHideTag(OtherDecl))
|
2010-10-24 00:06:17 +08:00
|
|
|
Decls[UniqueTagIndex] = Decls[--N];
|
|
|
|
else
|
|
|
|
Ambiguous = true;
|
|
|
|
}
|
2009-10-10 05:13:30 +08:00
|
|
|
|
2015-11-05 03:26:32 +08:00
|
|
|
// FIXME: This diagnostic should really be delayed until we're done with
|
|
|
|
// the lookup result, in case the ambiguity is resolved by the caller.
|
|
|
|
if (!EquivalentNonFunctions.empty() && !Ambiguous)
|
|
|
|
getSema().diagnoseEquivalentInternalLinkageDeclarations(
|
|
|
|
getNameLoc(), HasNonFunction, EquivalentNonFunctions);
|
|
|
|
|
2009-10-10 05:13:30 +08:00
|
|
|
Decls.set_size(N);
|
|
|
|
|
2009-12-03 08:58:24 +08:00
|
|
|
if (HasNonFunction && (HasFunction || HasUnresolved))
|
2009-10-10 05:13:30 +08:00
|
|
|
Ambiguous = true;
|
2009-01-15 06:20:51 +08:00
|
|
|
|
2009-10-10 05:13:30 +08:00
|
|
|
if (Ambiguous)
|
2009-10-10 13:48:19 +08:00
|
|
|
setAmbiguous(LookupResult::AmbiguousReference);
|
2009-11-18 10:36:19 +08:00
|
|
|
else if (HasUnresolved)
|
|
|
|
ResultKind = LookupResult::FoundUnresolvedValue;
|
2009-11-22 08:44:51 +08:00
|
|
|
else if (N > 1 || HasFunctionTemplate)
|
2009-11-17 10:14:36 +08:00
|
|
|
ResultKind = LookupResult::FoundOverloaded;
|
2009-10-10 05:13:30 +08:00
|
|
|
else
|
2009-11-17 10:14:36 +08:00
|
|
|
ResultKind = LookupResult::Found;
|
2009-01-15 06:20:51 +08:00
|
|
|
}
|
|
|
|
|
2009-11-18 15:57:50 +08:00
|
|
|
void LookupResult::addDeclsFromBasePaths(const CXXBasePaths &P) {
|
2010-02-10 17:31:12 +08:00
|
|
|
CXXBasePaths::const_paths_iterator I, E;
|
2009-10-10 05:13:30 +08:00
|
|
|
for (I = P.begin(), E = P.end(); I != E; ++I)
|
2012-12-19 08:45:41 +08:00
|
|
|
for (DeclContext::lookup_iterator DI = I->Decls.begin(),
|
|
|
|
DE = I->Decls.end(); DI != DE; ++DI)
|
2009-10-10 05:13:30 +08:00
|
|
|
addDecl(*DI);
|
2009-01-15 08:26:24 +08:00
|
|
|
}
|
|
|
|
|
2009-11-18 15:57:50 +08:00
|
|
|
void LookupResult::setAmbiguousBaseSubobjects(CXXBasePaths &P) {
|
2009-10-10 05:13:30 +08:00
|
|
|
Paths = new CXXBasePaths;
|
|
|
|
Paths->swap(P);
|
|
|
|
addDeclsFromBasePaths(*Paths);
|
|
|
|
resolveKind();
|
2009-10-10 13:48:19 +08:00
|
|
|
setAmbiguous(AmbiguousBaseSubobjects);
|
2009-02-03 05:35:47 +08:00
|
|
|
}
|
|
|
|
|
2009-11-18 15:57:50 +08:00
|
|
|
void LookupResult::setAmbiguousBaseSubobjectTypes(CXXBasePaths &P) {
|
2009-10-10 05:13:30 +08:00
|
|
|
Paths = new CXXBasePaths;
|
|
|
|
Paths->swap(P);
|
|
|
|
addDeclsFromBasePaths(*Paths);
|
|
|
|
resolveKind();
|
2009-10-10 13:48:19 +08:00
|
|
|
setAmbiguous(AmbiguousBaseSubobjectTypes);
|
2009-02-03 05:35:47 +08:00
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
void LookupResult::print(raw_ostream &Out) {
|
2009-10-10 05:13:30 +08:00
|
|
|
Out << Decls.size() << " result(s)";
|
|
|
|
if (isAmbiguous()) Out << ", ambiguous";
|
|
|
|
if (Paths) Out << ", base paths present";
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2009-10-10 05:13:30 +08:00
|
|
|
for (iterator I = begin(), E = end(); I != E; ++I) {
|
|
|
|
Out << "\n";
|
|
|
|
(*I)->print(Out, 2);
|
2009-04-02 05:51:26 +08:00
|
|
|
}
|
2009-02-03 05:35:47 +08:00
|
|
|
}
|
|
|
|
|
2016-01-13 05:59:26 +08:00
|
|
|
LLVM_DUMP_METHOD void LookupResult::dump() {
|
|
|
|
llvm::errs() << "lookup results for " << getLookupName().getAsString()
|
|
|
|
<< ":\n";
|
|
|
|
for (NamedDecl *D : *this)
|
|
|
|
D->dump();
|
|
|
|
}
|
|
|
|
|
2010-02-12 13:48:04 +08:00
|
|
|
/// \brief Lookup a builtin function, when name lookup would otherwise
|
|
|
|
/// fail.
|
|
|
|
static bool LookupBuiltin(Sema &S, LookupResult &R) {
|
|
|
|
Sema::LookupNameKind NameKind = R.getLookupKind();
|
|
|
|
|
|
|
|
// If we didn't find a use of this identifier, and if the identifier
|
|
|
|
// corresponds to a compiler builtin, create the decl object for the builtin
|
|
|
|
// now, injecting it into translation unit scope, and return it.
|
|
|
|
if (NameKind == Sema::LookupOrdinaryName ||
|
|
|
|
NameKind == Sema::LookupRedeclarationWithLinkage) {
|
|
|
|
IdentifierInfo *II = R.getLookupName().getAsIdentifierInfo();
|
|
|
|
if (II) {
|
2013-06-21 05:44:55 +08:00
|
|
|
if (S.getLangOpts().CPlusPlus11 && S.getLangOpts().GNUMode &&
|
|
|
|
II == S.getFloat128Identifier()) {
|
|
|
|
// libstdc++4.7's type_traits expects type __float128 to exist, so
|
|
|
|
// insert a dummy type to make that header build in gnu++11 mode.
|
|
|
|
R.addDecl(S.getASTContext().getFloat128StubType());
|
|
|
|
return true;
|
|
|
|
}
|
2015-11-04 11:40:30 +08:00
|
|
|
if (S.getLangOpts().CPlusPlus && NameKind == Sema::LookupOrdinaryName &&
|
|
|
|
II == S.getASTContext().getMakeIntegerSeqName()) {
|
|
|
|
R.addDecl(S.getASTContext().getMakeIntegerSeqDecl());
|
|
|
|
return true;
|
|
|
|
}
|
2013-06-21 05:44:55 +08:00
|
|
|
|
2010-02-12 13:48:04 +08:00
|
|
|
// If this is a builtin on this (or all) targets, create the decl.
|
|
|
|
if (unsigned BuiltinID = II->getBuiltinID()) {
|
2016-02-02 19:29:43 +08:00
|
|
|
// In C++ and OpenCL (spec v1.2 s6.9.f), we don't have any predefined
|
|
|
|
// library functions like 'malloc'. Instead, we'll just error.
|
|
|
|
if ((S.getLangOpts().CPlusPlus || S.getLangOpts().OpenCL) &&
|
2010-02-12 13:48:04 +08:00
|
|
|
S.Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
|
|
|
|
return false;
|
2011-01-27 15:10:08 +08:00
|
|
|
|
|
|
|
if (NamedDecl *D = S.LazilyCreateBuiltin((IdentifierInfo *)II,
|
|
|
|
BuiltinID, S.TUScope,
|
2011-01-03 17:37:44 +08:00
|
|
|
R.isForRedeclaration(),
|
|
|
|
R.getNameLoc())) {
|
2010-02-12 13:48:04 +08:00
|
|
|
R.addDecl(D);
|
2011-01-03 17:37:44 +08:00
|
|
|
return true;
|
|
|
|
}
|
2010-02-12 13:48:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-07-03 04:37:36 +08:00
|
|
|
/// \brief Determine whether we can declare a special member function within
|
|
|
|
/// the class at this point.
|
2012-11-28 05:20:31 +08:00
|
|
|
static bool CanDeclareSpecialMemberFunction(const CXXRecordDecl *Class) {
|
2010-07-03 04:37:36 +08:00
|
|
|
// We need to have a definition for the class.
|
|
|
|
if (!Class->getDefinition() || Class->isDependentContext())
|
|
|
|
return false;
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2010-07-03 04:37:36 +08:00
|
|
|
// We can't be in the middle of defining the class.
|
2012-11-28 05:20:31 +08:00
|
|
|
return !Class->isBeingDefined();
|
2010-07-03 04:37:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sema::ForceDeclarationOfImplicitMembers(CXXRecordDecl *Class) {
|
2012-11-28 05:20:31 +08:00
|
|
|
if (!CanDeclareSpecialMemberFunction(Class))
|
2010-07-03 07:41:54 +08:00
|
|
|
return;
|
2010-07-03 08:47:00 +08:00
|
|
|
|
|
|
|
// If the default constructor has not yet been declared, do so now.
|
2011-05-12 06:34:38 +08:00
|
|
|
if (Class->needsImplicitDefaultConstructor())
|
2010-07-03 08:47:00 +08:00
|
|
|
DeclareImplicitDefaultConstructor(Class);
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2010-07-03 07:41:54 +08:00
|
|
|
// If the copy constructor has not yet been declared, do so now.
|
2012-12-01 10:35:44 +08:00
|
|
|
if (Class->needsImplicitCopyConstructor())
|
2010-07-03 07:41:54 +08:00
|
|
|
DeclareImplicitCopyConstructor(Class);
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2010-07-03 05:50:04 +08:00
|
|
|
// If the copy assignment operator has not yet been declared, do so now.
|
2012-12-01 10:35:44 +08:00
|
|
|
if (Class->needsImplicitCopyAssignment())
|
2010-07-03 05:50:04 +08:00
|
|
|
DeclareImplicitCopyAssignment(Class);
|
|
|
|
|
2013-01-02 19:42:31 +08:00
|
|
|
if (getLangOpts().CPlusPlus11) {
|
2011-08-31 03:58:05 +08:00
|
|
|
// If the move constructor has not yet been declared, do so now.
|
|
|
|
if (Class->needsImplicitMoveConstructor())
|
|
|
|
DeclareImplicitMoveConstructor(Class); // might not actually do it
|
|
|
|
|
|
|
|
// If the move assignment operator has not yet been declared, do so now.
|
|
|
|
if (Class->needsImplicitMoveAssignment())
|
|
|
|
DeclareImplicitMoveAssignment(Class); // might not actually do it
|
|
|
|
}
|
|
|
|
|
2010-07-03 04:37:36 +08:00
|
|
|
// If the destructor has not yet been declared, do so now.
|
2012-12-01 10:35:44 +08:00
|
|
|
if (Class->needsImplicitDestructor())
|
2011-01-27 15:10:08 +08:00
|
|
|
DeclareImplicitDestructor(Class);
|
2010-07-03 04:37:36 +08:00
|
|
|
}
|
|
|
|
|
2011-01-27 15:10:08 +08:00
|
|
|
/// \brief Determine whether this is the name of an implicitly-declared
|
2010-07-03 05:50:04 +08:00
|
|
|
/// special member function.
|
|
|
|
static bool isImplicitlyDeclaredMemberFunctionName(DeclarationName Name) {
|
|
|
|
switch (Name.getNameKind()) {
|
2010-07-03 07:41:54 +08:00
|
|
|
case DeclarationName::CXXConstructorName:
|
2010-07-03 05:50:04 +08:00
|
|
|
case DeclarationName::CXXDestructorName:
|
|
|
|
return true;
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2010-07-03 05:50:04 +08:00
|
|
|
case DeclarationName::CXXOperatorName:
|
|
|
|
return Name.getCXXOverloadedOperator() == OO_Equal;
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2010-07-03 05:50:04 +08:00
|
|
|
default:
|
2011-01-27 15:10:08 +08:00
|
|
|
break;
|
2010-07-03 05:50:04 +08:00
|
|
|
}
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2010-07-03 05:50:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief If there are any implicit member functions with the given name
|
|
|
|
/// that need to be declared in the given declaration context, do so.
|
2011-01-27 15:10:08 +08:00
|
|
|
static void DeclareImplicitMemberFunctionsWithName(Sema &S,
|
2010-07-03 05:50:04 +08:00
|
|
|
DeclarationName Name,
|
|
|
|
const DeclContext *DC) {
|
|
|
|
if (!DC)
|
|
|
|
return;
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2010-07-03 05:50:04 +08:00
|
|
|
switch (Name.getNameKind()) {
|
2010-07-03 07:41:54 +08:00
|
|
|
case DeclarationName::CXXConstructorName:
|
|
|
|
if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(DC))
|
2012-11-28 05:20:31 +08:00
|
|
|
if (Record->getDefinition() && CanDeclareSpecialMemberFunction(Record)) {
|
2011-08-31 03:58:05 +08:00
|
|
|
CXXRecordDecl *Class = const_cast<CXXRecordDecl *>(Record);
|
2011-05-12 06:34:38 +08:00
|
|
|
if (Record->needsImplicitDefaultConstructor())
|
2011-08-31 03:58:05 +08:00
|
|
|
S.DeclareImplicitDefaultConstructor(Class);
|
2012-12-01 10:35:44 +08:00
|
|
|
if (Record->needsImplicitCopyConstructor())
|
2011-08-31 03:58:05 +08:00
|
|
|
S.DeclareImplicitCopyConstructor(Class);
|
2013-01-02 19:42:31 +08:00
|
|
|
if (S.getLangOpts().CPlusPlus11 &&
|
2011-08-31 03:58:05 +08:00
|
|
|
Record->needsImplicitMoveConstructor())
|
|
|
|
S.DeclareImplicitMoveConstructor(Class);
|
2010-07-03 08:47:00 +08:00
|
|
|
}
|
2010-07-03 07:41:54 +08:00
|
|
|
break;
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2010-07-03 05:50:04 +08:00
|
|
|
case DeclarationName::CXXDestructorName:
|
|
|
|
if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(DC))
|
2012-12-01 10:35:44 +08:00
|
|
|
if (Record->getDefinition() && Record->needsImplicitDestructor() &&
|
2012-11-28 05:20:31 +08:00
|
|
|
CanDeclareSpecialMemberFunction(Record))
|
2010-07-03 05:50:04 +08:00
|
|
|
S.DeclareImplicitDestructor(const_cast<CXXRecordDecl *>(Record));
|
|
|
|
break;
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2010-07-03 05:50:04 +08:00
|
|
|
case DeclarationName::CXXOperatorName:
|
|
|
|
if (Name.getCXXOverloadedOperator() != OO_Equal)
|
|
|
|
break;
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2011-08-31 03:58:05 +08:00
|
|
|
if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(DC)) {
|
2012-11-28 05:20:31 +08:00
|
|
|
if (Record->getDefinition() && CanDeclareSpecialMemberFunction(Record)) {
|
2011-08-31 03:58:05 +08:00
|
|
|
CXXRecordDecl *Class = const_cast<CXXRecordDecl *>(Record);
|
2012-12-01 10:35:44 +08:00
|
|
|
if (Record->needsImplicitCopyAssignment())
|
2011-08-31 03:58:05 +08:00
|
|
|
S.DeclareImplicitCopyAssignment(Class);
|
2013-01-02 19:42:31 +08:00
|
|
|
if (S.getLangOpts().CPlusPlus11 &&
|
2011-08-31 03:58:05 +08:00
|
|
|
Record->needsImplicitMoveAssignment())
|
|
|
|
S.DeclareImplicitMoveAssignment(Class);
|
|
|
|
}
|
|
|
|
}
|
2010-07-03 05:50:04 +08:00
|
|
|
break;
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2010-07-03 05:50:04 +08:00
|
|
|
default:
|
2011-01-27 15:10:08 +08:00
|
|
|
break;
|
2010-07-03 05:50:04 +08:00
|
|
|
}
|
|
|
|
}
|
2010-07-03 04:37:36 +08:00
|
|
|
|
2009-10-10 05:13:30 +08:00
|
|
|
// Adds all qualifying matches for a name within a decl context to the
|
|
|
|
// given lookup result. Returns true if any matches were found.
|
2010-02-12 13:48:04 +08:00
|
|
|
static bool LookupDirect(Sema &S, LookupResult &R, const DeclContext *DC) {
|
2009-10-10 05:13:30 +08:00
|
|
|
bool Found = false;
|
2009-04-02 05:51:26 +08:00
|
|
|
|
2010-07-03 04:37:36 +08:00
|
|
|
// Lazily declare C++ special member functions.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (S.getLangOpts().CPlusPlus)
|
2010-07-03 05:50:04 +08:00
|
|
|
DeclareImplicitMemberFunctionsWithName(S, R.getLookupName(), DC);
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2010-07-03 04:37:36 +08:00
|
|
|
// Perform lookup into this declaration context.
|
2015-02-21 10:45:19 +08:00
|
|
|
DeclContext::lookup_result DR = DC->lookup(R.getLookupName());
|
|
|
|
for (DeclContext::lookup_iterator I = DR.begin(), E = DR.end(); I != E;
|
2012-12-19 08:45:41 +08:00
|
|
|
++I) {
|
2010-01-21 05:53:11 +08:00
|
|
|
NamedDecl *D = *I;
|
2011-12-15 00:03:29 +08:00
|
|
|
if ((D = R.getAcceptableDecl(D))) {
|
2010-01-21 05:53:11 +08:00
|
|
|
R.addDecl(D);
|
2010-01-12 02:40:55 +08:00
|
|
|
Found = true;
|
|
|
|
}
|
|
|
|
}
|
2009-04-02 05:51:26 +08:00
|
|
|
|
2010-02-12 13:48:04 +08:00
|
|
|
if (!Found && DC->isTranslationUnit() && LookupBuiltin(S, R))
|
|
|
|
return true;
|
|
|
|
|
2010-01-12 02:40:55 +08:00
|
|
|
if (R.getLookupName().getNameKind()
|
2010-01-31 19:44:02 +08:00
|
|
|
!= DeclarationName::CXXConversionFunctionName ||
|
|
|
|
R.getLookupName().getCXXNameType()->isDependentType() ||
|
|
|
|
!isa<CXXRecordDecl>(DC))
|
|
|
|
return Found;
|
|
|
|
|
|
|
|
// C++ [temp.mem]p6:
|
2011-01-27 15:10:08 +08:00
|
|
|
// A specialization of a conversion function template is not found by
|
2010-01-31 19:44:02 +08:00
|
|
|
// name lookup. Instead, any conversion function templates visible in the
|
|
|
|
// context of the use are considered. [...]
|
|
|
|
const CXXRecordDecl *Record = cast<CXXRecordDecl>(DC);
|
2011-10-07 14:10:15 +08:00
|
|
|
if (!Record->isCompleteDefinition())
|
2010-01-31 19:44:02 +08:00
|
|
|
return Found;
|
|
|
|
|
2012-11-28 11:56:09 +08:00
|
|
|
for (CXXRecordDecl::conversion_iterator U = Record->conversion_begin(),
|
|
|
|
UEnd = Record->conversion_end(); U != UEnd; ++U) {
|
2010-01-31 19:44:02 +08:00
|
|
|
FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(*U);
|
|
|
|
if (!ConvTemplate)
|
|
|
|
continue;
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2010-01-31 19:44:02 +08:00
|
|
|
// When we're performing lookup for the purposes of redeclaration, just
|
2011-01-27 15:10:08 +08:00
|
|
|
// add the conversion function template. When we deduce template
|
|
|
|
// arguments for specializations, we'll end up unifying the return
|
2010-01-31 19:44:02 +08:00
|
|
|
// type of the new declaration with the type of the function template.
|
|
|
|
if (R.isForRedeclaration()) {
|
|
|
|
R.addDecl(ConvTemplate);
|
|
|
|
Found = true;
|
|
|
|
continue;
|
|
|
|
}
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2010-01-12 02:40:55 +08:00
|
|
|
// C++ [temp.mem]p6:
|
2011-01-27 15:10:08 +08:00
|
|
|
// [...] For each such operator, if argument deduction succeeds
|
|
|
|
// (14.9.2.3), the resulting specialization is used as if found by
|
2010-01-31 19:44:02 +08:00
|
|
|
// name lookup.
|
|
|
|
//
|
|
|
|
// When referencing a conversion function for any purpose other than
|
|
|
|
// a redeclaration (such that we'll be building an expression with the
|
2011-01-27 15:10:08 +08:00
|
|
|
// result), perform template argument deduction and place the
|
2010-01-31 19:44:02 +08:00
|
|
|
// specialization into the result set. We do this to avoid forcing all
|
|
|
|
// callers to perform special deduction for conversion functions.
|
2012-09-19 10:26:47 +08:00
|
|
|
TemplateDeductionInfo Info(R.getNameLoc());
|
2014-05-26 14:22:03 +08:00
|
|
|
FunctionDecl *Specialization = nullptr;
|
2011-01-27 15:10:08 +08:00
|
|
|
|
|
|
|
const FunctionProtoType *ConvProto
|
2010-01-31 19:44:02 +08:00
|
|
|
= ConvTemplate->getTemplatedDecl()->getType()->getAs<FunctionProtoType>();
|
|
|
|
assert(ConvProto && "Nonsensical conversion function template type");
|
|
|
|
|
|
|
|
// Compute the type of the function that we would expect the conversion
|
|
|
|
// function to have, if it were to match the name given.
|
|
|
|
// FIXME: Calling convention!
|
2010-12-14 16:05:40 +08:00
|
|
|
FunctionProtoType::ExtProtoInfo EPI = ConvProto->getExtProtoInfo();
|
2013-08-28 07:08:25 +08:00
|
|
|
EPI.ExtInfo = EPI.ExtInfo.withCallingConv(CC_C);
|
2014-08-01 05:57:55 +08:00
|
|
|
EPI.ExceptionSpec = EST_None;
|
2010-01-31 19:44:02 +08:00
|
|
|
QualType ExpectedType
|
|
|
|
= R.getSema().Context.getFunctionType(R.getLookupName().getCXXNameType(),
|
2013-05-05 08:41:58 +08:00
|
|
|
None, EPI);
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2010-01-31 19:44:02 +08:00
|
|
|
// Perform template argument deduction against the type that we would
|
|
|
|
// expect the function to have.
|
2014-05-26 14:22:03 +08:00
|
|
|
if (R.getSema().DeduceTemplateArguments(ConvTemplate, nullptr, ExpectedType,
|
2010-01-31 19:44:02 +08:00
|
|
|
Specialization, Info)
|
|
|
|
== Sema::TDK_Success) {
|
|
|
|
R.addDecl(Specialization);
|
|
|
|
Found = true;
|
2010-01-12 02:40:55 +08:00
|
|
|
}
|
|
|
|
}
|
2010-01-31 19:44:02 +08:00
|
|
|
|
2009-10-10 05:13:30 +08:00
|
|
|
return Found;
|
2009-02-03 05:35:47 +08:00
|
|
|
}
|
|
|
|
|
2009-11-10 15:01:13 +08:00
|
|
|
// Performs C++ unqualified lookup into the given file context.
|
2009-10-10 05:13:30 +08:00
|
|
|
static bool
|
2011-01-27 15:10:08 +08:00
|
|
|
CppNamespaceLookup(Sema &S, LookupResult &R, ASTContext &Context,
|
2010-02-12 13:48:04 +08:00
|
|
|
DeclContext *NS, UnqualUsingDirectiveSet &UDirs) {
|
2009-02-06 03:25:20 +08:00
|
|
|
|
|
|
|
assert(NS && NS->isFileContext() && "CppNamespaceLookup() requires namespace!");
|
|
|
|
|
2009-11-10 15:01:13 +08:00
|
|
|
// Perform direct name lookup into the LookupCtx.
|
2010-02-12 13:48:04 +08:00
|
|
|
bool Found = LookupDirect(S, R, NS);
|
2009-02-06 03:25:20 +08:00
|
|
|
|
2009-11-10 15:01:13 +08:00
|
|
|
// Perform direct name lookup into the namespaces nominated by the
|
|
|
|
// using directives whose common ancestor is this namespace.
|
2015-02-07 01:25:10 +08:00
|
|
|
for (const UnqualUsingEntry &UUE : UDirs.getNamespacesFor(NS))
|
|
|
|
if (LookupDirect(S, R, UUE.getNominatedNamespace()))
|
2009-11-10 15:01:13 +08:00
|
|
|
Found = true;
|
2009-10-10 05:13:30 +08:00
|
|
|
|
|
|
|
R.resolveKind();
|
|
|
|
|
|
|
|
return Found;
|
2009-02-06 03:25:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool isNamespaceOrTranslationUnitScope(Scope *S) {
|
2013-10-09 01:08:03 +08:00
|
|
|
if (DeclContext *Ctx = S->getEntity())
|
2009-02-06 03:25:20 +08:00
|
|
|
return Ctx->isFileContext();
|
|
|
|
return false;
|
2009-02-04 03:21:40 +08:00
|
|
|
}
|
Eliminated LookupCriteria, whose creation was causing a bottleneck for
LookupName et al. Instead, use an enum and a bool to describe its
contents.
Optimized the C/Objective-C path through LookupName, eliminating any
unnecessarily C++isms. Simplify IdentifierResolver::iterator, removing
some code and arguments that are no longer used.
Eliminated LookupDeclInScope/LookupDeclInContext, moving all callers
over to LookupName, LookupQualifiedName, or LookupParsedName, as
appropriate.
All together, I'm seeing a 0.2% speedup on Cocoa.h with PTH and
-disable-free. Plus, we're down to three name-lookup routines.
llvm-svn: 63354
2009-01-30 09:04:22 +08:00
|
|
|
|
2010-03-15 22:33:29 +08:00
|
|
|
// Find the next outer declaration context from this scope. This
|
|
|
|
// routine actually returns the semantic outer context, which may
|
|
|
|
// differ from the lexical context (encoded directly in the Scope
|
|
|
|
// stack) when we are parsing a member of a class template. In this
|
|
|
|
// case, the second element of the pair will be true, to indicate that
|
|
|
|
// name lookup should continue searching in this semantic context when
|
|
|
|
// it leaves the current template parameter scope.
|
|
|
|
static std::pair<DeclContext *, bool> findOuterContext(Scope *S) {
|
2013-10-09 01:08:03 +08:00
|
|
|
DeclContext *DC = S->getEntity();
|
2014-05-26 14:22:03 +08:00
|
|
|
DeclContext *Lexical = nullptr;
|
2011-01-27 15:10:08 +08:00
|
|
|
for (Scope *OuterS = S->getParent(); OuterS;
|
2010-03-15 22:33:29 +08:00
|
|
|
OuterS = OuterS->getParent()) {
|
|
|
|
if (OuterS->getEntity()) {
|
2013-10-09 01:08:03 +08:00
|
|
|
Lexical = OuterS->getEntity();
|
2010-03-15 22:33:29 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// C++ [temp.local]p8:
|
|
|
|
// In the definition of a member of a class template that appears
|
|
|
|
// outside of the namespace containing the class template
|
|
|
|
// definition, the name of a template-parameter hides the name of
|
|
|
|
// a member of this namespace.
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
2011-01-27 15:10:08 +08:00
|
|
|
// namespace N {
|
|
|
|
// class C { };
|
2010-03-15 22:33:29 +08:00
|
|
|
//
|
|
|
|
// template<class T> class B {
|
|
|
|
// void f(T);
|
2011-01-27 15:10:08 +08:00
|
|
|
// };
|
2010-03-15 22:33:29 +08:00
|
|
|
// }
|
|
|
|
//
|
|
|
|
// template<class C> void N::B<C>::f(C) {
|
|
|
|
// C b; // C is the template parameter, not N::C
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// In this example, the lexical context we return is the
|
|
|
|
// TranslationUnit, while the semantic context is the namespace N.
|
2011-01-27 15:10:08 +08:00
|
|
|
if (!Lexical || !DC || !S->getParent() ||
|
2010-03-15 22:33:29 +08:00
|
|
|
!S->getParent()->isTemplateParamScope())
|
|
|
|
return std::make_pair(Lexical, false);
|
|
|
|
|
2011-01-27 15:10:08 +08:00
|
|
|
// Find the outermost template parameter scope.
|
2010-03-15 22:33:29 +08:00
|
|
|
// For the example, this is the scope for the template parameters of
|
|
|
|
// template<class C>.
|
|
|
|
Scope *OutermostTemplateScope = S->getParent();
|
|
|
|
while (OutermostTemplateScope->getParent() &&
|
|
|
|
OutermostTemplateScope->getParent()->isTemplateParamScope())
|
|
|
|
OutermostTemplateScope = OutermostTemplateScope->getParent();
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2010-03-15 22:33:29 +08:00
|
|
|
// Find the namespace context in which the original scope occurs. In
|
|
|
|
// the example, this is namespace N.
|
|
|
|
DeclContext *Semantic = DC;
|
|
|
|
while (!Semantic->isFileContext())
|
|
|
|
Semantic = Semantic->getParent();
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2010-03-15 22:33:29 +08:00
|
|
|
// Find the declaration context just outside of the template
|
|
|
|
// parameter scope. This is the context in which the template is
|
|
|
|
// being lexically declaration (a namespace context). In the
|
|
|
|
// example, this is the global scope.
|
|
|
|
if (Lexical->isFileContext() && !Lexical->Equals(Semantic) &&
|
|
|
|
Lexical->Encloses(Semantic))
|
|
|
|
return std::make_pair(Semantic, true);
|
|
|
|
|
|
|
|
return std::make_pair(Lexical, false);
|
2009-09-11 00:57:35 +08:00
|
|
|
}
|
|
|
|
|
2013-09-20 09:15:31 +08:00
|
|
|
namespace {
|
|
|
|
/// An RAII object to specify that we want to find block scope extern
|
|
|
|
/// declarations.
|
|
|
|
struct FindLocalExternScope {
|
|
|
|
FindLocalExternScope(LookupResult &R)
|
|
|
|
: R(R), OldFindLocalExtern(R.getIdentifierNamespace() &
|
|
|
|
Decl::IDNS_LocalExtern) {
|
|
|
|
R.setFindLocalExtern(R.getIdentifierNamespace() & Decl::IDNS_Ordinary);
|
|
|
|
}
|
|
|
|
void restore() {
|
|
|
|
R.setFindLocalExtern(OldFindLocalExtern);
|
|
|
|
}
|
|
|
|
~FindLocalExternScope() {
|
|
|
|
restore();
|
|
|
|
}
|
|
|
|
LookupResult &R;
|
|
|
|
bool OldFindLocalExtern;
|
|
|
|
};
|
2015-10-07 07:40:43 +08:00
|
|
|
} // end anonymous namespace
|
2013-09-20 09:15:31 +08:00
|
|
|
|
2009-11-17 10:14:36 +08:00
|
|
|
bool Sema::CppLookupName(LookupResult &R, Scope *S) {
|
2012-03-11 15:00:24 +08:00
|
|
|
assert(getLangOpts().CPlusPlus && "Can perform only C++ lookup");
|
2009-11-17 10:14:36 +08:00
|
|
|
|
|
|
|
DeclarationName Name = R.getLookupName();
|
2013-08-14 02:18:50 +08:00
|
|
|
Sema::LookupNameKind NameKind = R.getLookupKind();
|
2009-11-17 10:14:36 +08:00
|
|
|
|
2010-07-03 05:50:04 +08:00
|
|
|
// If this is the name of an implicitly-declared special member function,
|
|
|
|
// go through the scope stack to implicitly declare
|
|
|
|
if (isImplicitlyDeclaredMemberFunctionName(Name)) {
|
|
|
|
for (Scope *PreS = S; PreS; PreS = PreS->getParent())
|
2013-10-09 01:08:03 +08:00
|
|
|
if (DeclContext *DC = PreS->getEntity())
|
2010-07-03 05:50:04 +08:00
|
|
|
DeclareImplicitMemberFunctionsWithName(*this, Name, DC);
|
|
|
|
}
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2010-07-03 05:50:04 +08:00
|
|
|
// Implicitly declare member functions with the name we're looking for, if in
|
|
|
|
// fact we are in a scope where it matters.
|
|
|
|
|
2009-02-04 03:21:40 +08:00
|
|
|
Scope *Initial = S;
|
2009-09-09 23:08:12 +08:00
|
|
|
IdentifierResolver::iterator
|
2009-02-04 03:21:40 +08:00
|
|
|
I = IdResolver.begin(Name),
|
|
|
|
IEnd = IdResolver.end();
|
|
|
|
|
|
|
|
// First we lookup local scope.
|
2009-02-05 03:02:06 +08:00
|
|
|
// We don't consider using-directives, as per 7.3.4.p1 [namespace.udir]
|
2009-02-04 03:21:40 +08:00
|
|
|
// ...During unqualified name lookup (3.4.1), the names appear as if
|
|
|
|
// they were declared in the nearest enclosing namespace which contains
|
|
|
|
// both the using-directive and the nominated namespace.
|
2009-08-06 03:21:58 +08:00
|
|
|
// [Note: in this context, "contains" means "contains directly or
|
2009-09-09 23:08:12 +08:00
|
|
|
// indirectly".
|
2009-02-04 03:21:40 +08:00
|
|
|
//
|
|
|
|
// For example:
|
|
|
|
// namespace A { int i; }
|
|
|
|
// void foo() {
|
|
|
|
// int i;
|
|
|
|
// {
|
|
|
|
// using namespace A;
|
|
|
|
// ++i; // finds local 'i', A::i appears at global scope
|
|
|
|
// }
|
|
|
|
// }
|
2009-02-05 01:27:36 +08:00
|
|
|
//
|
2013-04-09 07:11:25 +08:00
|
|
|
UnqualUsingDirectiveSet UDirs;
|
|
|
|
bool VisitedUsingDirectives = false;
|
2013-08-14 02:18:50 +08:00
|
|
|
bool LeftStartingScope = false;
|
2014-05-26 14:22:03 +08:00
|
|
|
DeclContext *OutsideOfTemplateParamDC = nullptr;
|
2013-09-20 09:15:31 +08:00
|
|
|
|
|
|
|
// When performing a scope lookup, we want to find local extern decls.
|
|
|
|
FindLocalExternScope FindLocals(R);
|
|
|
|
|
2009-02-06 03:25:20 +08:00
|
|
|
for (; S && !isNamespaceOrTranslationUnitScope(S); S = S->getParent()) {
|
2013-10-09 01:08:03 +08:00
|
|
|
DeclContext *Ctx = S->getEntity();
|
2010-05-21 04:58:56 +08:00
|
|
|
|
2009-02-04 03:21:40 +08:00
|
|
|
// Check whether the IdResolver has anything in this scope.
|
2009-10-10 05:13:30 +08:00
|
|
|
bool Found = false;
|
2010-08-21 17:40:31 +08:00
|
|
|
for (; I != IEnd && S->isDeclScope(*I); ++I) {
|
2011-12-15 00:03:29 +08:00
|
|
|
if (NamedDecl *ND = R.getAcceptableDecl(*I)) {
|
2013-08-14 02:18:50 +08:00
|
|
|
if (NameKind == LookupRedeclarationWithLinkage) {
|
|
|
|
// Determine whether this (or a previous) declaration is
|
|
|
|
// out-of-scope.
|
|
|
|
if (!LeftStartingScope && !Initial->isDeclScope(*I))
|
|
|
|
LeftStartingScope = true;
|
|
|
|
|
|
|
|
// If we found something outside of our starting scope that
|
2013-10-17 05:12:00 +08:00
|
|
|
// does not have linkage, skip it. If it's a template parameter,
|
|
|
|
// we still find it, so we can diagnose the invalid redeclaration.
|
|
|
|
if (LeftStartingScope && !((*I)->hasLinkage()) &&
|
|
|
|
!(*I)->isTemplateParameter()) {
|
2013-08-14 02:18:50 +08:00
|
|
|
R.setShadowed();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-10 05:13:30 +08:00
|
|
|
Found = true;
|
2011-12-15 00:03:29 +08:00
|
|
|
R.addDecl(ND);
|
2009-02-04 03:21:40 +08:00
|
|
|
}
|
|
|
|
}
|
2009-10-10 05:13:30 +08:00
|
|
|
if (Found) {
|
|
|
|
R.resolveKind();
|
2010-05-21 04:58:56 +08:00
|
|
|
if (S->isClassScope())
|
|
|
|
if (CXXRecordDecl *Record = dyn_cast_or_null<CXXRecordDecl>(Ctx))
|
|
|
|
R.setNamingClass(Record);
|
2009-10-10 05:13:30 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-14 02:18:50 +08:00
|
|
|
if (NameKind == LookupLocalFriendName && !S->isClassScope()) {
|
2013-08-09 12:35:01 +08:00
|
|
|
// C++11 [class.friend]p11:
|
|
|
|
// If a friend declaration appears in a local class and the name
|
|
|
|
// specified is an unqualified name, a prior declaration is
|
|
|
|
// looked up without considering scopes that are outside the
|
|
|
|
// innermost enclosing non-class scope.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-03-15 22:33:29 +08:00
|
|
|
if (!Ctx && S->isTemplateParamScope() && OutsideOfTemplateParamDC &&
|
|
|
|
S->getParent() && !S->getParent()->isTemplateParamScope()) {
|
|
|
|
// We've just searched the last template parameter scope and
|
2012-07-23 16:59:39 +08:00
|
|
|
// found nothing, so look into the contexts between the
|
2010-03-15 22:33:29 +08:00
|
|
|
// lexical and semantic declaration contexts returned by
|
|
|
|
// findOuterContext(). This implements the name lookup behavior
|
|
|
|
// of C++ [temp.local]p8.
|
|
|
|
Ctx = OutsideOfTemplateParamDC;
|
2014-05-26 14:22:03 +08:00
|
|
|
OutsideOfTemplateParamDC = nullptr;
|
2010-03-15 22:33:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Ctx) {
|
|
|
|
DeclContext *OuterCtx;
|
|
|
|
bool SearchAfterTemplateScope;
|
2014-03-02 21:01:17 +08:00
|
|
|
std::tie(OuterCtx, SearchAfterTemplateScope) = findOuterContext(S);
|
2010-03-15 22:33:29 +08:00
|
|
|
if (SearchAfterTemplateScope)
|
|
|
|
OutsideOfTemplateParamDC = OuterCtx;
|
|
|
|
|
2010-03-15 23:26:48 +08:00
|
|
|
for (; Ctx && !Ctx->Equals(OuterCtx); Ctx = Ctx->getLookupParent()) {
|
2010-02-20 00:08:35 +08:00
|
|
|
// We do not directly look into transparent contexts, since
|
|
|
|
// those entities will be found in the nearest enclosing
|
|
|
|
// non-transparent context.
|
|
|
|
if (Ctx->isTransparentContext())
|
2009-09-11 00:57:35 +08:00
|
|
|
continue;
|
2010-02-20 00:08:35 +08:00
|
|
|
|
|
|
|
// We do not look directly into function or method contexts,
|
|
|
|
// since all of the local variables and parameters of the
|
|
|
|
// function/method are present within the Scope.
|
|
|
|
if (Ctx->isFunctionOrMethod()) {
|
|
|
|
// If we have an Objective-C instance method, look for ivars
|
|
|
|
// in the corresponding interface.
|
|
|
|
if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(Ctx)) {
|
|
|
|
if (Method->isInstanceMethod() && Name.getAsIdentifierInfo())
|
|
|
|
if (ObjCInterfaceDecl *Class = Method->getClassInterface()) {
|
|
|
|
ObjCInterfaceDecl *ClassDeclared;
|
|
|
|
if (ObjCIvarDecl *Ivar = Class->lookupInstanceVariable(
|
2011-01-27 15:10:08 +08:00
|
|
|
Name.getAsIdentifierInfo(),
|
2010-02-20 00:08:35 +08:00
|
|
|
ClassDeclared)) {
|
2011-12-15 00:03:29 +08:00
|
|
|
if (NamedDecl *ND = R.getAcceptableDecl(Ivar)) {
|
|
|
|
R.addDecl(ND);
|
2010-02-20 00:08:35 +08:00
|
|
|
R.resolveKind();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-03-27 20:51:49 +08:00
|
|
|
// If this is a file context, we need to perform unqualified name
|
|
|
|
// lookup considering using directives.
|
|
|
|
if (Ctx->isFileContext()) {
|
2013-04-09 07:11:25 +08:00
|
|
|
// If we haven't handled using directives yet, do so now.
|
|
|
|
if (!VisitedUsingDirectives) {
|
|
|
|
// Add using directives from this context up to the top level.
|
2013-04-09 09:49:26 +08:00
|
|
|
for (DeclContext *UCtx = Ctx; UCtx; UCtx = UCtx->getParent()) {
|
|
|
|
if (UCtx->isTransparentContext())
|
|
|
|
continue;
|
|
|
|
|
2013-04-09 07:11:25 +08:00
|
|
|
UDirs.visit(UCtx, UCtx);
|
2013-04-09 09:49:26 +08:00
|
|
|
}
|
2013-04-09 07:11:25 +08:00
|
|
|
|
|
|
|
// Find the innermost file scope, so we can add using directives
|
|
|
|
// from local scopes.
|
|
|
|
Scope *InnermostFileScope = S;
|
|
|
|
while (InnermostFileScope &&
|
|
|
|
!isNamespaceOrTranslationUnitScope(InnermostFileScope))
|
|
|
|
InnermostFileScope = InnermostFileScope->getParent();
|
|
|
|
UDirs.visitScopeChain(Initial, InnermostFileScope);
|
|
|
|
|
|
|
|
UDirs.done();
|
|
|
|
|
|
|
|
VisitedUsingDirectives = true;
|
|
|
|
}
|
2013-03-27 20:51:49 +08:00
|
|
|
|
|
|
|
if (CppNamespaceLookup(*this, R, Context, Ctx, UDirs)) {
|
|
|
|
R.resolveKind();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-09-11 00:57:35 +08:00
|
|
|
// Perform qualified name lookup into this context.
|
|
|
|
// FIXME: In some cases, we know that every name that could be found by
|
|
|
|
// this qualified name lookup will also be on the identifier chain. For
|
|
|
|
// example, inside a class without any base classes, we never need to
|
|
|
|
// perform qualified lookup because all of the members are on top of the
|
|
|
|
// identifier chain.
|
2010-01-15 09:44:47 +08:00
|
|
|
if (LookupQualifiedName(R, Ctx, /*InUnqualifiedLookup=*/true))
|
2009-10-10 05:13:30 +08:00
|
|
|
return true;
|
2009-03-27 12:21:56 +08:00
|
|
|
}
|
2009-02-06 03:25:20 +08:00
|
|
|
}
|
2009-02-04 03:21:40 +08:00
|
|
|
}
|
Eliminated LookupCriteria, whose creation was causing a bottleneck for
LookupName et al. Instead, use an enum and a bool to describe its
contents.
Optimized the C/Objective-C path through LookupName, eliminating any
unnecessarily C++isms. Simplify IdentifierResolver::iterator, removing
some code and arguments that are no longer used.
Eliminated LookupDeclInScope/LookupDeclInContext, moving all callers
over to LookupName, LookupQualifiedName, or LookupParsedName, as
appropriate.
All together, I'm seeing a 0.2% speedup on Cocoa.h with PTH and
-disable-free. Plus, we're down to three name-lookup routines.
llvm-svn: 63354
2009-01-30 09:04:22 +08:00
|
|
|
|
2009-11-10 15:01:13 +08:00
|
|
|
// Stop if we ran out of scopes.
|
|
|
|
// FIXME: This really, really shouldn't be happening.
|
|
|
|
if (!S) return false;
|
|
|
|
|
2010-10-30 00:12:50 +08:00
|
|
|
// If we are looking for members, no need to look into global/namespace scope.
|
2013-08-14 02:18:50 +08:00
|
|
|
if (NameKind == LookupMemberName)
|
2010-10-30 00:12:50 +08:00
|
|
|
return false;
|
|
|
|
|
2009-02-06 03:25:20 +08:00
|
|
|
// Collect UsingDirectiveDecls in all scopes, and recursively all
|
2009-02-04 03:21:40 +08:00
|
|
|
// nominated namespaces by those using-directives.
|
2009-11-10 15:01:13 +08:00
|
|
|
//
|
2009-05-16 15:39:55 +08:00
|
|
|
// FIXME: Cache this sorted list in Scope structure, and DeclContext, so we
|
|
|
|
// don't build it for each lookup!
|
2013-04-09 07:11:25 +08:00
|
|
|
if (!VisitedUsingDirectives) {
|
|
|
|
UDirs.visitScopeChain(Initial, S);
|
|
|
|
UDirs.done();
|
|
|
|
}
|
2013-09-20 09:15:31 +08:00
|
|
|
|
|
|
|
// If we're not performing redeclaration lookup, do not look for local
|
|
|
|
// extern declarations outside of a function scope.
|
|
|
|
if (!R.isForRedeclaration())
|
|
|
|
FindLocals.restore();
|
|
|
|
|
2009-02-06 03:25:20 +08:00
|
|
|
// Lookup namespace scope, and global scope.
|
2009-02-04 03:21:40 +08:00
|
|
|
// Unqualified name lookup in C++ requires looking into scopes
|
|
|
|
// that aren't strictly lexical, and therefore we walk through the
|
|
|
|
// context as well as walking through the scopes.
|
|
|
|
for (; S; S = S->getParent()) {
|
|
|
|
// Check whether the IdResolver has anything in this scope.
|
2009-10-10 05:13:30 +08:00
|
|
|
bool Found = false;
|
2010-08-21 17:40:31 +08:00
|
|
|
for (; I != IEnd && S->isDeclScope(*I); ++I) {
|
2011-12-15 00:03:29 +08:00
|
|
|
if (NamedDecl *ND = R.getAcceptableDecl(*I)) {
|
2009-02-04 03:21:40 +08:00
|
|
|
// We found something. Look for anything else in our scope
|
|
|
|
// with this same name and in an acceptable identifier
|
|
|
|
// namespace, so that we can construct an overload set if we
|
|
|
|
// need to.
|
2009-10-10 05:13:30 +08:00
|
|
|
Found = true;
|
2011-12-15 00:03:29 +08:00
|
|
|
R.addDecl(ND);
|
2009-02-04 03:21:40 +08:00
|
|
|
}
|
|
|
|
}
|
Eliminated LookupCriteria, whose creation was causing a bottleneck for
LookupName et al. Instead, use an enum and a bool to describe its
contents.
Optimized the C/Objective-C path through LookupName, eliminating any
unnecessarily C++isms. Simplify IdentifierResolver::iterator, removing
some code and arguments that are no longer used.
Eliminated LookupDeclInScope/LookupDeclInContext, moving all callers
over to LookupName, LookupQualifiedName, or LookupParsedName, as
appropriate.
All together, I'm seeing a 0.2% speedup on Cocoa.h with PTH and
-disable-free. Plus, we're down to three name-lookup routines.
llvm-svn: 63354
2009-01-30 09:04:22 +08:00
|
|
|
|
2010-05-14 12:53:42 +08:00
|
|
|
if (Found && S->isTemplateParamScope()) {
|
|
|
|
R.resolveKind();
|
|
|
|
return true;
|
|
|
|
}
|
2010-02-05 15:07:10 +08:00
|
|
|
|
2013-10-09 01:08:03 +08:00
|
|
|
DeclContext *Ctx = S->getEntity();
|
2010-05-14 12:53:42 +08:00
|
|
|
if (!Ctx && S->isTemplateParamScope() && OutsideOfTemplateParamDC &&
|
|
|
|
S->getParent() && !S->getParent()->isTemplateParamScope()) {
|
|
|
|
// We've just searched the last template parameter scope and
|
2012-07-23 16:59:39 +08:00
|
|
|
// found nothing, so look into the contexts between the
|
2010-05-14 12:53:42 +08:00
|
|
|
// lexical and semantic declaration contexts returned by
|
|
|
|
// findOuterContext(). This implements the name lookup behavior
|
|
|
|
// of C++ [temp.local]p8.
|
|
|
|
Ctx = OutsideOfTemplateParamDC;
|
2014-05-26 14:22:03 +08:00
|
|
|
OutsideOfTemplateParamDC = nullptr;
|
2010-02-05 15:07:10 +08:00
|
|
|
}
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2010-05-14 12:53:42 +08:00
|
|
|
if (Ctx) {
|
|
|
|
DeclContext *OuterCtx;
|
|
|
|
bool SearchAfterTemplateScope;
|
2014-03-02 21:01:17 +08:00
|
|
|
std::tie(OuterCtx, SearchAfterTemplateScope) = findOuterContext(S);
|
2010-05-14 12:53:42 +08:00
|
|
|
if (SearchAfterTemplateScope)
|
|
|
|
OutsideOfTemplateParamDC = OuterCtx;
|
2009-02-04 03:21:40 +08:00
|
|
|
|
2010-05-14 12:53:42 +08:00
|
|
|
for (; Ctx && !Ctx->Equals(OuterCtx); Ctx = Ctx->getLookupParent()) {
|
|
|
|
// We do not directly look into transparent contexts, since
|
|
|
|
// those entities will be found in the nearest enclosing
|
|
|
|
// non-transparent context.
|
|
|
|
if (Ctx->isTransparentContext())
|
|
|
|
continue;
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2010-05-14 12:53:42 +08:00
|
|
|
// If we have a context, and it's not a context stashed in the
|
|
|
|
// template parameter scope for an out-of-line definition, also
|
|
|
|
// look into that context.
|
|
|
|
if (!(Found && S && S->isTemplateParamScope())) {
|
|
|
|
assert(Ctx->isFileContext() &&
|
|
|
|
"We should have been looking only at file context here already.");
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2010-05-14 12:53:42 +08:00
|
|
|
// Look into context considering using-directives.
|
|
|
|
if (CppNamespaceLookup(*this, R, Context, Ctx, UDirs))
|
|
|
|
Found = true;
|
|
|
|
}
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2010-05-14 12:53:42 +08:00
|
|
|
if (Found) {
|
|
|
|
R.resolveKind();
|
|
|
|
return true;
|
|
|
|
}
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2010-05-14 12:53:42 +08:00
|
|
|
if (R.isForRedeclaration() && !Ctx->isTransparentContext())
|
|
|
|
return false;
|
|
|
|
}
|
2009-10-10 05:13:30 +08:00
|
|
|
}
|
2009-02-04 03:21:40 +08:00
|
|
|
|
2010-02-05 15:07:10 +08:00
|
|
|
if (R.isForRedeclaration() && Ctx && !Ctx->isTransparentContext())
|
2009-10-10 05:13:30 +08:00
|
|
|
return false;
|
Eliminated LookupCriteria, whose creation was causing a bottleneck for
LookupName et al. Instead, use an enum and a bool to describe its
contents.
Optimized the C/Objective-C path through LookupName, eliminating any
unnecessarily C++isms. Simplify IdentifierResolver::iterator, removing
some code and arguments that are no longer used.
Eliminated LookupDeclInScope/LookupDeclInContext, moving all callers
over to LookupName, LookupQualifiedName, or LookupParsedName, as
appropriate.
All together, I'm seeing a 0.2% speedup on Cocoa.h with PTH and
-disable-free. Plus, we're down to three name-lookup routines.
llvm-svn: 63354
2009-01-30 09:04:22 +08:00
|
|
|
}
|
2009-10-10 05:13:30 +08:00
|
|
|
|
|
|
|
return !R.empty();
|
Eliminated LookupCriteria, whose creation was causing a bottleneck for
LookupName et al. Instead, use an enum and a bool to describe its
contents.
Optimized the C/Objective-C path through LookupName, eliminating any
unnecessarily C++isms. Simplify IdentifierResolver::iterator, removing
some code and arguments that are no longer used.
Eliminated LookupDeclInScope/LookupDeclInContext, moving all callers
over to LookupName, LookupQualifiedName, or LookupParsedName, as
appropriate.
All together, I'm seeing a 0.2% speedup on Cocoa.h with PTH and
-disable-free. Plus, we're down to three name-lookup routines.
llvm-svn: 63354
2009-01-30 09:04:22 +08:00
|
|
|
}
|
|
|
|
|
When we perform dependent name lookup during template instantiation, it's not
sufficient to only consider names visible at the point of instantiation,
because that may not include names that were visible when the template was
defined. More generally, if the instantiation backtrace goes through a module
M, then every declaration visible within M should be available to the
instantiation. Any of those declarations might be part of the interface that M
intended to export to a template that it instantiates.
The fix here has two parts:
1) If we find a non-visible declaration during name lookup during template
instantiation, check whether the declaration was visible from the defining
module of all entities on the active template instantiation stack. The defining
module is not the owning module in all cases: we look at the module in which a
template was defined, not the module in which it was first instantiated.
2) Perform pending instantiations at the end of a module, not at the end of the
translation unit. This is general goodness, since it significantly cuts down
the amount of redundant work that is performed in every TU importing a module,
and also implicitly adds the module containing the point of instantiation to
the set of modules checked for declarations in a lookup within a template
instantiation.
There's a known issue here with template instantiations performed while
building a module, if additional imports are added later on. I'll fix that
in a subsequent commit.
llvm-svn: 187167
2013-07-26 07:08:39 +08:00
|
|
|
/// \brief Find the declaration that a class temploid member specialization was
|
|
|
|
/// instantiated from, or the member itself if it is an explicit specialization.
|
|
|
|
static Decl *getInstantiatedFrom(Decl *D, MemberSpecializationInfo *MSInfo) {
|
|
|
|
return MSInfo->isExplicitSpecialization() ? D : MSInfo->getInstantiatedFrom();
|
|
|
|
}
|
|
|
|
|
2015-05-16 04:05:43 +08:00
|
|
|
Module *Sema::getOwningModule(Decl *Entity) {
|
|
|
|
// If it's imported, grab its owning module.
|
|
|
|
Module *M = Entity->getImportedOwningModule();
|
|
|
|
if (M || !isa<NamedDecl>(Entity) || !cast<NamedDecl>(Entity)->isHidden())
|
|
|
|
return M;
|
|
|
|
assert(!Entity->isFromASTFile() &&
|
|
|
|
"hidden entity from AST file has no owning module");
|
|
|
|
|
2015-06-09 08:35:49 +08:00
|
|
|
if (!getLangOpts().ModulesLocalVisibility) {
|
|
|
|
// If we're not tracking visibility locally, the only way a declaration
|
|
|
|
// can be hidden and local is if it's hidden because it's parent is (for
|
|
|
|
// instance, maybe this is a lazily-declared special member of an imported
|
|
|
|
// class).
|
|
|
|
auto *Parent = cast<NamedDecl>(Entity->getDeclContext());
|
|
|
|
assert(Parent->isHidden() && "unexpectedly hidden decl");
|
|
|
|
return getOwningModule(Parent);
|
|
|
|
}
|
|
|
|
|
2015-05-16 04:05:43 +08:00
|
|
|
// It's local and hidden; grab or compute its owning module.
|
|
|
|
M = Entity->getLocalOwningModule();
|
|
|
|
if (M)
|
|
|
|
return M;
|
|
|
|
|
|
|
|
if (auto *Containing =
|
|
|
|
PP.getModuleContainingLocation(Entity->getLocation())) {
|
|
|
|
M = Containing;
|
|
|
|
} else if (Entity->isInvalidDecl() || Entity->getLocation().isInvalid()) {
|
|
|
|
// Don't bother tracking visibility for invalid declarations with broken
|
|
|
|
// locations.
|
|
|
|
cast<NamedDecl>(Entity)->setHidden(false);
|
|
|
|
} else {
|
|
|
|
// We need to assign a module to an entity that exists outside of any
|
|
|
|
// module, so that we can hide it from modules that we textually enter.
|
|
|
|
// Invent a fake module for all such entities.
|
|
|
|
if (!CachedFakeTopLevelModule) {
|
|
|
|
CachedFakeTopLevelModule =
|
|
|
|
PP.getHeaderSearchInfo().getModuleMap().findOrCreateModule(
|
|
|
|
"<top-level>", nullptr, false, false).first;
|
|
|
|
|
|
|
|
auto &SrcMgr = PP.getSourceManager();
|
|
|
|
SourceLocation StartLoc =
|
|
|
|
SrcMgr.getLocForStartOfFile(SrcMgr.getMainFileID());
|
|
|
|
auto &TopLevel =
|
|
|
|
VisibleModulesStack.empty() ? VisibleModules : VisibleModulesStack[0];
|
|
|
|
TopLevel.setVisible(CachedFakeTopLevelModule, StartLoc);
|
|
|
|
}
|
|
|
|
|
|
|
|
M = CachedFakeTopLevelModule;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (M)
|
|
|
|
Entity->setLocalOwningModule(M);
|
|
|
|
return M;
|
|
|
|
}
|
|
|
|
|
2015-05-07 11:54:19 +08:00
|
|
|
void Sema::makeMergedDefinitionVisible(NamedDecl *ND, SourceLocation Loc) {
|
2015-06-09 08:35:49 +08:00
|
|
|
if (auto *M = PP.getModuleContainingLocation(Loc))
|
|
|
|
Context.mergeDefinitionIntoModule(ND, M);
|
|
|
|
else
|
|
|
|
// We're not building a module; just make the definition visible.
|
|
|
|
ND->setHidden(false);
|
2015-06-18 04:39:41 +08:00
|
|
|
|
|
|
|
// If ND is a template declaration, make the template parameters
|
|
|
|
// visible too. They're not (necessarily) within a mergeable DeclContext.
|
|
|
|
if (auto *TD = dyn_cast<TemplateDecl>(ND))
|
|
|
|
for (auto *Param : *TD->getTemplateParameters())
|
|
|
|
makeMergedDefinitionVisible(Param, Loc);
|
2015-05-07 11:54:19 +08:00
|
|
|
}
|
|
|
|
|
When we perform dependent name lookup during template instantiation, it's not
sufficient to only consider names visible at the point of instantiation,
because that may not include names that were visible when the template was
defined. More generally, if the instantiation backtrace goes through a module
M, then every declaration visible within M should be available to the
instantiation. Any of those declarations might be part of the interface that M
intended to export to a template that it instantiates.
The fix here has two parts:
1) If we find a non-visible declaration during name lookup during template
instantiation, check whether the declaration was visible from the defining
module of all entities on the active template instantiation stack. The defining
module is not the owning module in all cases: we look at the module in which a
template was defined, not the module in which it was first instantiated.
2) Perform pending instantiations at the end of a module, not at the end of the
translation unit. This is general goodness, since it significantly cuts down
the amount of redundant work that is performed in every TU importing a module,
and also implicitly adds the module containing the point of instantiation to
the set of modules checked for declarations in a lookup within a template
instantiation.
There's a known issue here with template instantiations performed while
building a module, if additional imports are added later on. I'll fix that
in a subsequent commit.
llvm-svn: 187167
2013-07-26 07:08:39 +08:00
|
|
|
/// \brief Find the module in which the given declaration was defined.
|
2015-05-16 04:05:43 +08:00
|
|
|
static Module *getDefiningModule(Sema &S, Decl *Entity) {
|
When we perform dependent name lookup during template instantiation, it's not
sufficient to only consider names visible at the point of instantiation,
because that may not include names that were visible when the template was
defined. More generally, if the instantiation backtrace goes through a module
M, then every declaration visible within M should be available to the
instantiation. Any of those declarations might be part of the interface that M
intended to export to a template that it instantiates.
The fix here has two parts:
1) If we find a non-visible declaration during name lookup during template
instantiation, check whether the declaration was visible from the defining
module of all entities on the active template instantiation stack. The defining
module is not the owning module in all cases: we look at the module in which a
template was defined, not the module in which it was first instantiated.
2) Perform pending instantiations at the end of a module, not at the end of the
translation unit. This is general goodness, since it significantly cuts down
the amount of redundant work that is performed in every TU importing a module,
and also implicitly adds the module containing the point of instantiation to
the set of modules checked for declarations in a lookup within a template
instantiation.
There's a known issue here with template instantiations performed while
building a module, if additional imports are added later on. I'll fix that
in a subsequent commit.
llvm-svn: 187167
2013-07-26 07:08:39 +08:00
|
|
|
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Entity)) {
|
|
|
|
// If this function was instantiated from a template, the defining module is
|
|
|
|
// the module containing the pattern.
|
|
|
|
if (FunctionDecl *Pattern = FD->getTemplateInstantiationPattern())
|
|
|
|
Entity = Pattern;
|
|
|
|
} else if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Entity)) {
|
2014-10-15 04:28:40 +08:00
|
|
|
if (CXXRecordDecl *Pattern = RD->getTemplateInstantiationPattern())
|
|
|
|
Entity = Pattern;
|
When we perform dependent name lookup during template instantiation, it's not
sufficient to only consider names visible at the point of instantiation,
because that may not include names that were visible when the template was
defined. More generally, if the instantiation backtrace goes through a module
M, then every declaration visible within M should be available to the
instantiation. Any of those declarations might be part of the interface that M
intended to export to a template that it instantiates.
The fix here has two parts:
1) If we find a non-visible declaration during name lookup during template
instantiation, check whether the declaration was visible from the defining
module of all entities on the active template instantiation stack. The defining
module is not the owning module in all cases: we look at the module in which a
template was defined, not the module in which it was first instantiated.
2) Perform pending instantiations at the end of a module, not at the end of the
translation unit. This is general goodness, since it significantly cuts down
the amount of redundant work that is performed in every TU importing a module,
and also implicitly adds the module containing the point of instantiation to
the set of modules checked for declarations in a lookup within a template
instantiation.
There's a known issue here with template instantiations performed while
building a module, if additional imports are added later on. I'll fix that
in a subsequent commit.
llvm-svn: 187167
2013-07-26 07:08:39 +08:00
|
|
|
} else if (EnumDecl *ED = dyn_cast<EnumDecl>(Entity)) {
|
|
|
|
if (MemberSpecializationInfo *MSInfo = ED->getMemberSpecializationInfo())
|
|
|
|
Entity = getInstantiatedFrom(ED, MSInfo);
|
|
|
|
} else if (VarDecl *VD = dyn_cast<VarDecl>(Entity)) {
|
|
|
|
// FIXME: Map from variable template specializations back to the template.
|
|
|
|
if (MemberSpecializationInfo *MSInfo = VD->getMemberSpecializationInfo())
|
|
|
|
Entity = getInstantiatedFrom(VD, MSInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Walk up to the containing context. That might also have been instantiated
|
|
|
|
// from a template.
|
|
|
|
DeclContext *Context = Entity->getDeclContext();
|
|
|
|
if (Context->isFileContext())
|
2015-05-16 04:05:43 +08:00
|
|
|
return S.getOwningModule(Entity);
|
|
|
|
return getDefiningModule(S, cast<Decl>(Context));
|
When we perform dependent name lookup during template instantiation, it's not
sufficient to only consider names visible at the point of instantiation,
because that may not include names that were visible when the template was
defined. More generally, if the instantiation backtrace goes through a module
M, then every declaration visible within M should be available to the
instantiation. Any of those declarations might be part of the interface that M
intended to export to a template that it instantiates.
The fix here has two parts:
1) If we find a non-visible declaration during name lookup during template
instantiation, check whether the declaration was visible from the defining
module of all entities on the active template instantiation stack. The defining
module is not the owning module in all cases: we look at the module in which a
template was defined, not the module in which it was first instantiated.
2) Perform pending instantiations at the end of a module, not at the end of the
translation unit. This is general goodness, since it significantly cuts down
the amount of redundant work that is performed in every TU importing a module,
and also implicitly adds the module containing the point of instantiation to
the set of modules checked for declarations in a lookup within a template
instantiation.
There's a known issue here with template instantiations performed while
building a module, if additional imports are added later on. I'll fix that
in a subsequent commit.
llvm-svn: 187167
2013-07-26 07:08:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::DenseSet<Module*> &Sema::getLookupModules() {
|
|
|
|
unsigned N = ActiveTemplateInstantiations.size();
|
|
|
|
for (unsigned I = ActiveTemplateInstantiationLookupModules.size();
|
|
|
|
I != N; ++I) {
|
2015-05-16 04:05:43 +08:00
|
|
|
Module *M =
|
|
|
|
getDefiningModule(*this, ActiveTemplateInstantiations[I].Entity);
|
When we perform dependent name lookup during template instantiation, it's not
sufficient to only consider names visible at the point of instantiation,
because that may not include names that were visible when the template was
defined. More generally, if the instantiation backtrace goes through a module
M, then every declaration visible within M should be available to the
instantiation. Any of those declarations might be part of the interface that M
intended to export to a template that it instantiates.
The fix here has two parts:
1) If we find a non-visible declaration during name lookup during template
instantiation, check whether the declaration was visible from the defining
module of all entities on the active template instantiation stack. The defining
module is not the owning module in all cases: we look at the module in which a
template was defined, not the module in which it was first instantiated.
2) Perform pending instantiations at the end of a module, not at the end of the
translation unit. This is general goodness, since it significantly cuts down
the amount of redundant work that is performed in every TU importing a module,
and also implicitly adds the module containing the point of instantiation to
the set of modules checked for declarations in a lookup within a template
instantiation.
There's a known issue here with template instantiations performed while
building a module, if additional imports are added later on. I'll fix that
in a subsequent commit.
llvm-svn: 187167
2013-07-26 07:08:39 +08:00
|
|
|
if (M && !LookupModulesCache.insert(M).second)
|
2014-05-26 14:22:03 +08:00
|
|
|
M = nullptr;
|
When we perform dependent name lookup during template instantiation, it's not
sufficient to only consider names visible at the point of instantiation,
because that may not include names that were visible when the template was
defined. More generally, if the instantiation backtrace goes through a module
M, then every declaration visible within M should be available to the
instantiation. Any of those declarations might be part of the interface that M
intended to export to a template that it instantiates.
The fix here has two parts:
1) If we find a non-visible declaration during name lookup during template
instantiation, check whether the declaration was visible from the defining
module of all entities on the active template instantiation stack. The defining
module is not the owning module in all cases: we look at the module in which a
template was defined, not the module in which it was first instantiated.
2) Perform pending instantiations at the end of a module, not at the end of the
translation unit. This is general goodness, since it significantly cuts down
the amount of redundant work that is performed in every TU importing a module,
and also implicitly adds the module containing the point of instantiation to
the set of modules checked for declarations in a lookup within a template
instantiation.
There's a known issue here with template instantiations performed while
building a module, if additional imports are added later on. I'll fix that
in a subsequent commit.
llvm-svn: 187167
2013-07-26 07:08:39 +08:00
|
|
|
ActiveTemplateInstantiationLookupModules.push_back(M);
|
|
|
|
}
|
|
|
|
return LookupModulesCache;
|
|
|
|
}
|
|
|
|
|
2015-05-16 04:05:43 +08:00
|
|
|
bool Sema::hasVisibleMergedDefinition(NamedDecl *Def) {
|
|
|
|
for (Module *Merged : Context.getModulesWithMergedDefinition(Def))
|
|
|
|
if (isModuleVisible(Merged))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-11 04:30:23 +08:00
|
|
|
template<typename ParmDecl>
|
2015-06-18 04:16:32 +08:00
|
|
|
static bool
|
|
|
|
hasVisibleDefaultArgument(Sema &S, const ParmDecl *D,
|
|
|
|
llvm::SmallVectorImpl<Module *> *Modules) {
|
2015-06-11 04:30:23 +08:00
|
|
|
if (!D->hasDefaultArgument())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
while (D) {
|
|
|
|
auto &DefaultArg = D->getDefaultArgStorage();
|
|
|
|
if (!DefaultArg.isInherited() && S.isVisible(D))
|
|
|
|
return true;
|
|
|
|
|
2015-06-18 04:39:41 +08:00
|
|
|
if (!DefaultArg.isInherited() && Modules) {
|
|
|
|
auto *NonConstD = const_cast<ParmDecl*>(D);
|
|
|
|
Modules->push_back(S.getOwningModule(NonConstD));
|
|
|
|
const auto &Merged = S.Context.getModulesWithMergedDefinition(NonConstD);
|
|
|
|
Modules->insert(Modules->end(), Merged.begin(), Merged.end());
|
|
|
|
}
|
2015-06-18 04:16:32 +08:00
|
|
|
|
2015-06-11 04:30:23 +08:00
|
|
|
// If there was a previous default argument, maybe its parameter is visible.
|
|
|
|
D = DefaultArg.getInheritedFrom();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-18 04:16:32 +08:00
|
|
|
bool Sema::hasVisibleDefaultArgument(const NamedDecl *D,
|
|
|
|
llvm::SmallVectorImpl<Module *> *Modules) {
|
2015-06-11 04:30:23 +08:00
|
|
|
if (auto *P = dyn_cast<TemplateTypeParmDecl>(D))
|
2015-06-18 04:16:32 +08:00
|
|
|
return ::hasVisibleDefaultArgument(*this, P, Modules);
|
2015-06-11 04:30:23 +08:00
|
|
|
if (auto *P = dyn_cast<NonTypeTemplateParmDecl>(D))
|
2015-06-18 04:16:32 +08:00
|
|
|
return ::hasVisibleDefaultArgument(*this, P, Modules);
|
|
|
|
return ::hasVisibleDefaultArgument(*this, cast<TemplateTemplateParmDecl>(D),
|
|
|
|
Modules);
|
2015-06-11 04:30:23 +08:00
|
|
|
}
|
|
|
|
|
When we perform dependent name lookup during template instantiation, it's not
sufficient to only consider names visible at the point of instantiation,
because that may not include names that were visible when the template was
defined. More generally, if the instantiation backtrace goes through a module
M, then every declaration visible within M should be available to the
instantiation. Any of those declarations might be part of the interface that M
intended to export to a template that it instantiates.
The fix here has two parts:
1) If we find a non-visible declaration during name lookup during template
instantiation, check whether the declaration was visible from the defining
module of all entities on the active template instantiation stack. The defining
module is not the owning module in all cases: we look at the module in which a
template was defined, not the module in which it was first instantiated.
2) Perform pending instantiations at the end of a module, not at the end of the
translation unit. This is general goodness, since it significantly cuts down
the amount of redundant work that is performed in every TU importing a module,
and also implicitly adds the module containing the point of instantiation to
the set of modules checked for declarations in a lookup within a template
instantiation.
There's a known issue here with template instantiations performed while
building a module, if additional imports are added later on. I'll fix that
in a subsequent commit.
llvm-svn: 187167
2013-07-26 07:08:39 +08:00
|
|
|
/// \brief Determine whether a declaration is visible to name lookup.
|
|
|
|
///
|
|
|
|
/// This routine determines whether the declaration D is visible in the current
|
|
|
|
/// lookup context, taking into account the current template instantiation
|
|
|
|
/// stack. During template instantiation, a declaration is visible if it is
|
|
|
|
/// visible from a module containing any entity on the template instantiation
|
|
|
|
/// path (by instantiating a template, you allow it to see the declarations that
|
|
|
|
/// your module can see, including those later on in your module).
|
|
|
|
bool LookupResult::isVisibleSlow(Sema &SemaRef, NamedDecl *D) {
|
2015-03-27 08:41:57 +08:00
|
|
|
assert(D->isHidden() && "should not call this: not in slow case");
|
2015-08-24 11:38:11 +08:00
|
|
|
Module *DeclModule = nullptr;
|
|
|
|
|
|
|
|
if (SemaRef.getLangOpts().ModulesLocalVisibility) {
|
|
|
|
DeclModule = SemaRef.getOwningModule(D);
|
|
|
|
if (!DeclModule) {
|
|
|
|
// getOwningModule() may have decided the declaration should not be hidden.
|
|
|
|
assert(!D->isHidden() && "hidden decl not from a module");
|
2015-05-16 04:05:43 +08:00
|
|
|
return true;
|
2015-08-24 11:38:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the owning module is visible, and the decl is not module private,
|
|
|
|
// then the decl is visible too. (Module private is ignored within the same
|
|
|
|
// top-level module.)
|
|
|
|
if ((!D->isFromASTFile() || !D->isModulePrivate()) &&
|
|
|
|
(SemaRef.isModuleVisible(DeclModule) ||
|
|
|
|
SemaRef.hasVisibleMergedDefinition(D)))
|
2015-05-16 04:05:43 +08:00
|
|
|
return true;
|
|
|
|
}
|
When we perform dependent name lookup during template instantiation, it's not
sufficient to only consider names visible at the point of instantiation,
because that may not include names that were visible when the template was
defined. More generally, if the instantiation backtrace goes through a module
M, then every declaration visible within M should be available to the
instantiation. Any of those declarations might be part of the interface that M
intended to export to a template that it instantiates.
The fix here has two parts:
1) If we find a non-visible declaration during name lookup during template
instantiation, check whether the declaration was visible from the defining
module of all entities on the active template instantiation stack. The defining
module is not the owning module in all cases: we look at the module in which a
template was defined, not the module in which it was first instantiated.
2) Perform pending instantiations at the end of a module, not at the end of the
translation unit. This is general goodness, since it significantly cuts down
the amount of redundant work that is performed in every TU importing a module,
and also implicitly adds the module containing the point of instantiation to
the set of modules checked for declarations in a lookup within a template
instantiation.
There's a known issue here with template instantiations performed while
building a module, if additional imports are added later on. I'll fix that
in a subsequent commit.
llvm-svn: 187167
2013-07-26 07:08:39 +08:00
|
|
|
|
2015-03-27 08:41:57 +08:00
|
|
|
// If this declaration is not at namespace scope nor module-private,
|
|
|
|
// then it is visible if its lexical parent has a visible definition.
|
|
|
|
DeclContext *DC = D->getLexicalDeclContext();
|
|
|
|
if (!D->isModulePrivate() &&
|
|
|
|
DC && !DC->isFileContext() && !isa<LinkageSpecDecl>(DC)) {
|
2015-05-21 01:50:35 +08:00
|
|
|
// For a parameter, check whether our current template declaration's
|
|
|
|
// lexical context is visible, not whether there's some other visible
|
|
|
|
// definition of it, because parameters aren't "within" the definition.
|
|
|
|
if ((D->isTemplateParameter() || isa<ParmVarDecl>(D))
|
|
|
|
? isVisible(SemaRef, cast<NamedDecl>(DC))
|
|
|
|
: SemaRef.hasVisibleDefinition(cast<NamedDecl>(DC))) {
|
2015-05-16 04:05:43 +08:00
|
|
|
if (SemaRef.ActiveTemplateInstantiations.empty() &&
|
|
|
|
// FIXME: Do something better in this case.
|
|
|
|
!SemaRef.getLangOpts().ModulesLocalVisibility) {
|
2015-03-27 08:41:57 +08:00
|
|
|
// Cache the fact that this declaration is implicitly visible because
|
|
|
|
// its parent has a visible definition.
|
|
|
|
D->setHidden(false);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
When we perform dependent name lookup during template instantiation, it's not
sufficient to only consider names visible at the point of instantiation,
because that may not include names that were visible when the template was
defined. More generally, if the instantiation backtrace goes through a module
M, then every declaration visible within M should be available to the
instantiation. Any of those declarations might be part of the interface that M
intended to export to a template that it instantiates.
The fix here has two parts:
1) If we find a non-visible declaration during name lookup during template
instantiation, check whether the declaration was visible from the defining
module of all entities on the active template instantiation stack. The defining
module is not the owning module in all cases: we look at the module in which a
template was defined, not the module in which it was first instantiated.
2) Perform pending instantiations at the end of a module, not at the end of the
translation unit. This is general goodness, since it significantly cuts down
the amount of redundant work that is performed in every TU importing a module,
and also implicitly adds the module containing the point of instantiation to
the set of modules checked for declarations in a lookup within a template
instantiation.
There's a known issue here with template instantiations performed while
building a module, if additional imports are added later on. I'll fix that
in a subsequent commit.
llvm-svn: 187167
2013-07-26 07:08:39 +08:00
|
|
|
// Find the extra places where we need to look.
|
|
|
|
llvm::DenseSet<Module*> &LookupModules = SemaRef.getLookupModules();
|
|
|
|
if (LookupModules.empty())
|
|
|
|
return false;
|
|
|
|
|
2015-08-24 11:38:11 +08:00
|
|
|
if (!DeclModule) {
|
|
|
|
DeclModule = SemaRef.getOwningModule(D);
|
|
|
|
assert(DeclModule && "hidden decl not from a module");
|
|
|
|
}
|
|
|
|
|
When we perform dependent name lookup during template instantiation, it's not
sufficient to only consider names visible at the point of instantiation,
because that may not include names that were visible when the template was
defined. More generally, if the instantiation backtrace goes through a module
M, then every declaration visible within M should be available to the
instantiation. Any of those declarations might be part of the interface that M
intended to export to a template that it instantiates.
The fix here has two parts:
1) If we find a non-visible declaration during name lookup during template
instantiation, check whether the declaration was visible from the defining
module of all entities on the active template instantiation stack. The defining
module is not the owning module in all cases: we look at the module in which a
template was defined, not the module in which it was first instantiated.
2) Perform pending instantiations at the end of a module, not at the end of the
translation unit. This is general goodness, since it significantly cuts down
the amount of redundant work that is performed in every TU importing a module,
and also implicitly adds the module containing the point of instantiation to
the set of modules checked for declarations in a lookup within a template
instantiation.
There's a known issue here with template instantiations performed while
building a module, if additional imports are added later on. I'll fix that
in a subsequent commit.
llvm-svn: 187167
2013-07-26 07:08:39 +08:00
|
|
|
// If our lookup set contains the decl's module, it's visible.
|
|
|
|
if (LookupModules.count(DeclModule))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// If the declaration isn't exported, it's not visible in any other module.
|
|
|
|
if (D->isModulePrivate())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check whether DeclModule is transitively exported to an import of
|
|
|
|
// the lookup set.
|
2015-11-24 03:28:42 +08:00
|
|
|
return std::any_of(LookupModules.begin(), LookupModules.end(),
|
2015-11-25 04:18:24 +08:00
|
|
|
[&](Module *M) { return M->isModuleVisible(DeclModule); });
|
When we perform dependent name lookup during template instantiation, it's not
sufficient to only consider names visible at the point of instantiation,
because that may not include names that were visible when the template was
defined. More generally, if the instantiation backtrace goes through a module
M, then every declaration visible within M should be available to the
instantiation. Any of those declarations might be part of the interface that M
intended to export to a template that it instantiates.
The fix here has two parts:
1) If we find a non-visible declaration during name lookup during template
instantiation, check whether the declaration was visible from the defining
module of all entities on the active template instantiation stack. The defining
module is not the owning module in all cases: we look at the module in which a
template was defined, not the module in which it was first instantiated.
2) Perform pending instantiations at the end of a module, not at the end of the
translation unit. This is general goodness, since it significantly cuts down
the amount of redundant work that is performed in every TU importing a module,
and also implicitly adds the module containing the point of instantiation to
the set of modules checked for declarations in a lookup within a template
instantiation.
There's a known issue here with template instantiations performed while
building a module, if additional imports are added later on. I'll fix that
in a subsequent commit.
llvm-svn: 187167
2013-07-26 07:08:39 +08:00
|
|
|
}
|
|
|
|
|
2015-05-16 04:05:43 +08:00
|
|
|
bool Sema::isVisibleSlow(const NamedDecl *D) {
|
|
|
|
return LookupResult::isVisible(*this, const_cast<NamedDecl*>(D));
|
|
|
|
}
|
|
|
|
|
2015-11-17 11:02:41 +08:00
|
|
|
bool Sema::shouldLinkPossiblyHiddenDecl(LookupResult &R, const NamedDecl *New) {
|
|
|
|
for (auto *D : R) {
|
|
|
|
if (isVisible(D))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return New->isExternallyVisible();
|
|
|
|
}
|
|
|
|
|
2011-12-15 00:03:29 +08:00
|
|
|
/// \brief Retrieve the visible declaration corresponding to D, if any.
|
|
|
|
///
|
|
|
|
/// This routine determines whether the declaration D is visible in the current
|
|
|
|
/// module, with the current imports. If not, it checks whether any
|
|
|
|
/// redeclaration of D is visible, and if so, returns that declaration.
|
When we perform dependent name lookup during template instantiation, it's not
sufficient to only consider names visible at the point of instantiation,
because that may not include names that were visible when the template was
defined. More generally, if the instantiation backtrace goes through a module
M, then every declaration visible within M should be available to the
instantiation. Any of those declarations might be part of the interface that M
intended to export to a template that it instantiates.
The fix here has two parts:
1) If we find a non-visible declaration during name lookup during template
instantiation, check whether the declaration was visible from the defining
module of all entities on the active template instantiation stack. The defining
module is not the owning module in all cases: we look at the module in which a
template was defined, not the module in which it was first instantiated.
2) Perform pending instantiations at the end of a module, not at the end of the
translation unit. This is general goodness, since it significantly cuts down
the amount of redundant work that is performed in every TU importing a module,
and also implicitly adds the module containing the point of instantiation to
the set of modules checked for declarations in a lookup within a template
instantiation.
There's a known issue here with template instantiations performed while
building a module, if additional imports are added later on. I'll fix that
in a subsequent commit.
llvm-svn: 187167
2013-07-26 07:08:39 +08:00
|
|
|
///
|
2011-12-15 00:03:29 +08:00
|
|
|
/// \returns D, or a visible previous declaration of D, whichever is more recent
|
|
|
|
/// and visible. If no declaration of D is visible, returns null.
|
2013-08-21 04:35:18 +08:00
|
|
|
static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) {
|
|
|
|
assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case");
|
When we perform dependent name lookup during template instantiation, it's not
sufficient to only consider names visible at the point of instantiation,
because that may not include names that were visible when the template was
defined. More generally, if the instantiation backtrace goes through a module
M, then every declaration visible within M should be available to the
instantiation. Any of those declarations might be part of the interface that M
intended to export to a template that it instantiates.
The fix here has two parts:
1) If we find a non-visible declaration during name lookup during template
instantiation, check whether the declaration was visible from the defining
module of all entities on the active template instantiation stack. The defining
module is not the owning module in all cases: we look at the module in which a
template was defined, not the module in which it was first instantiated.
2) Perform pending instantiations at the end of a module, not at the end of the
translation unit. This is general goodness, since it significantly cuts down
the amount of redundant work that is performed in every TU importing a module,
and also implicitly adds the module containing the point of instantiation to
the set of modules checked for declarations in a lookup within a template
instantiation.
There's a known issue here with template instantiations performed while
building a module, if additional imports are added later on. I'll fix that
in a subsequent commit.
llvm-svn: 187167
2013-07-26 07:08:39 +08:00
|
|
|
|
2014-03-07 07:45:36 +08:00
|
|
|
for (auto RD : D->redecls()) {
|
2016-02-18 05:52:44 +08:00
|
|
|
// Don't bother with extra checks if we already know this one isn't visible.
|
|
|
|
if (RD == D)
|
|
|
|
continue;
|
|
|
|
|
2014-03-07 07:45:36 +08:00
|
|
|
if (auto ND = dyn_cast<NamedDecl>(RD)) {
|
2015-02-10 11:28:10 +08:00
|
|
|
// FIXME: This is wrong in the case where the previous declaration is not
|
|
|
|
// visible in the same scope as D. This needs to be done much more
|
|
|
|
// carefully.
|
2013-08-21 04:35:18 +08:00
|
|
|
if (LookupResult::isVisible(SemaRef, ND))
|
2012-01-07 06:05:37 +08:00
|
|
|
return ND;
|
|
|
|
}
|
2011-12-15 00:03:29 +08:00
|
|
|
}
|
When we perform dependent name lookup during template instantiation, it's not
sufficient to only consider names visible at the point of instantiation,
because that may not include names that were visible when the template was
defined. More generally, if the instantiation backtrace goes through a module
M, then every declaration visible within M should be available to the
instantiation. Any of those declarations might be part of the interface that M
intended to export to a template that it instantiates.
The fix here has two parts:
1) If we find a non-visible declaration during name lookup during template
instantiation, check whether the declaration was visible from the defining
module of all entities on the active template instantiation stack. The defining
module is not the owning module in all cases: we look at the module in which a
template was defined, not the module in which it was first instantiated.
2) Perform pending instantiations at the end of a module, not at the end of the
translation unit. This is general goodness, since it significantly cuts down
the amount of redundant work that is performed in every TU importing a module,
and also implicitly adds the module containing the point of instantiation to
the set of modules checked for declarations in a lookup within a template
instantiation.
There's a known issue here with template instantiations performed while
building a module, if additional imports are added later on. I'll fix that
in a subsequent commit.
llvm-svn: 187167
2013-07-26 07:08:39 +08:00
|
|
|
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2011-12-15 00:03:29 +08:00
|
|
|
}
|
|
|
|
|
2013-08-21 04:35:18 +08:00
|
|
|
NamedDecl *LookupResult::getAcceptableDeclSlow(NamedDecl *D) const {
|
2016-02-18 05:52:44 +08:00
|
|
|
if (auto *ND = dyn_cast<NamespaceDecl>(D)) {
|
|
|
|
// Namespaces are a bit of a special case: we expect there to be a lot of
|
|
|
|
// redeclarations of some namespaces, all declarations of a namespace are
|
|
|
|
// essentially interchangeable, all declarations are found by name lookup
|
|
|
|
// if any is, and namespaces are never looked up during template
|
|
|
|
// instantiation. So we benefit from caching the check in this case, and
|
|
|
|
// it is correct to do so.
|
|
|
|
auto *Key = ND->getCanonicalDecl();
|
|
|
|
if (auto *Acceptable = getSema().VisibleNamespaceCache.lookup(Key))
|
|
|
|
return Acceptable;
|
|
|
|
auto *Acceptable =
|
|
|
|
isVisible(getSema(), Key) ? Key : findAcceptableDecl(getSema(), Key);
|
|
|
|
if (Acceptable)
|
|
|
|
getSema().VisibleNamespaceCache.insert(std::make_pair(Key, Acceptable));
|
|
|
|
return Acceptable;
|
|
|
|
}
|
|
|
|
|
2014-11-12 07:00:42 +08:00
|
|
|
return findAcceptableDecl(getSema(), D);
|
2013-08-21 04:35:18 +08:00
|
|
|
}
|
|
|
|
|
2009-01-15 06:20:51 +08:00
|
|
|
/// @brief Perform unqualified name lookup starting from a given
|
|
|
|
/// scope.
|
|
|
|
///
|
|
|
|
/// Unqualified name lookup (C++ [basic.lookup.unqual], C99 6.2.1) is
|
|
|
|
/// used to find names within the current scope. For example, 'x' in
|
|
|
|
/// @code
|
|
|
|
/// int x;
|
|
|
|
/// int f() {
|
|
|
|
/// return x; // unqualified name look finds 'x' in the global scope
|
|
|
|
/// }
|
|
|
|
/// @endcode
|
|
|
|
///
|
|
|
|
/// Different lookup criteria can find different names. For example, a
|
|
|
|
/// particular scope can have both a struct and a function of the same
|
|
|
|
/// name, and each can be found by certain lookup criteria. For more
|
|
|
|
/// information about lookup criteria, see the documentation for the
|
|
|
|
/// class LookupCriteria.
|
|
|
|
///
|
|
|
|
/// @param S The scope from which unqualified name lookup will
|
|
|
|
/// begin. If the lookup criteria permits, name lookup may also search
|
|
|
|
/// in the parent scopes.
|
|
|
|
///
|
2012-06-22 18:32:46 +08:00
|
|
|
/// @param [in,out] R Specifies the lookup to perform (e.g., the name to
|
|
|
|
/// look up and the lookup kind), and is updated with the results of lookup
|
|
|
|
/// including zero or more declarations and possibly additional information
|
|
|
|
/// used to diagnose ambiguities.
|
2009-01-15 06:20:51 +08:00
|
|
|
///
|
2012-06-22 18:32:46 +08:00
|
|
|
/// @returns \c true if lookup succeeded and false otherwise.
|
2009-11-17 10:14:36 +08:00
|
|
|
bool Sema::LookupName(LookupResult &R, Scope *S, bool AllowBuiltinCreation) {
|
|
|
|
DeclarationName Name = R.getLookupName();
|
2009-10-10 05:13:30 +08:00
|
|
|
if (!Name) return false;
|
2009-01-15 06:20:51 +08:00
|
|
|
|
2009-11-17 10:14:36 +08:00
|
|
|
LookupNameKind NameKind = R.getLookupKind();
|
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
if (!getLangOpts().CPlusPlus) {
|
2009-01-15 06:20:51 +08:00
|
|
|
// Unqualified name lookup in C/Objective-C is purely lexical, so
|
|
|
|
// search in the declarations attached to the name.
|
2009-12-18 18:40:03 +08:00
|
|
|
if (NameKind == Sema::LookupRedeclarationWithLinkage) {
|
2009-02-25 04:03:32 +08:00
|
|
|
// Find the nearest non-transparent declaration scope.
|
|
|
|
while (!(S->getFlags() & Scope::DeclScope) ||
|
2013-10-09 01:08:03 +08:00
|
|
|
(S->getEntity() && S->getEntity()->isTransparentContext()))
|
2009-02-25 04:03:32 +08:00
|
|
|
S = S->getParent();
|
Eliminated LookupCriteria, whose creation was causing a bottleneck for
LookupName et al. Instead, use an enum and a bool to describe its
contents.
Optimized the C/Objective-C path through LookupName, eliminating any
unnecessarily C++isms. Simplify IdentifierResolver::iterator, removing
some code and arguments that are no longer used.
Eliminated LookupDeclInScope/LookupDeclInContext, moving all callers
over to LookupName, LookupQualifiedName, or LookupParsedName, as
appropriate.
All together, I'm seeing a 0.2% speedup on Cocoa.h with PTH and
-disable-free. Plus, we're down to three name-lookup routines.
llvm-svn: 63354
2009-01-30 09:04:22 +08:00
|
|
|
}
|
2009-01-15 06:20:51 +08:00
|
|
|
|
2013-09-20 09:15:31 +08:00
|
|
|
// When performing a scope lookup, we want to find local extern decls.
|
|
|
|
FindLocalExternScope FindLocals(R);
|
|
|
|
|
2009-01-15 06:20:51 +08:00
|
|
|
// Scan up the scope chain looking for a decl that matches this
|
|
|
|
// identifier that is in the appropriate namespace. This search
|
|
|
|
// should not take long, as shadowing of names is uncommon, and
|
|
|
|
// deep shadowing is extremely uncommon.
|
2009-02-25 04:03:32 +08:00
|
|
|
bool LeftStartingScope = false;
|
|
|
|
|
Eliminated LookupCriteria, whose creation was causing a bottleneck for
LookupName et al. Instead, use an enum and a bool to describe its
contents.
Optimized the C/Objective-C path through LookupName, eliminating any
unnecessarily C++isms. Simplify IdentifierResolver::iterator, removing
some code and arguments that are no longer used.
Eliminated LookupDeclInScope/LookupDeclInContext, moving all callers
over to LookupName, LookupQualifiedName, or LookupParsedName, as
appropriate.
All together, I'm seeing a 0.2% speedup on Cocoa.h with PTH and
-disable-free. Plus, we're down to three name-lookup routines.
llvm-svn: 63354
2009-01-30 09:04:22 +08:00
|
|
|
for (IdentifierResolver::iterator I = IdResolver.begin(Name),
|
2009-09-09 23:08:12 +08:00
|
|
|
IEnd = IdResolver.end();
|
Eliminated LookupCriteria, whose creation was causing a bottleneck for
LookupName et al. Instead, use an enum and a bool to describe its
contents.
Optimized the C/Objective-C path through LookupName, eliminating any
unnecessarily C++isms. Simplify IdentifierResolver::iterator, removing
some code and arguments that are no longer used.
Eliminated LookupDeclInScope/LookupDeclInContext, moving all callers
over to LookupName, LookupQualifiedName, or LookupParsedName, as
appropriate.
All together, I'm seeing a 0.2% speedup on Cocoa.h with PTH and
-disable-free. Plus, we're down to three name-lookup routines.
llvm-svn: 63354
2009-01-30 09:04:22 +08:00
|
|
|
I != IEnd; ++I)
|
When we perform dependent name lookup during template instantiation, it's not
sufficient to only consider names visible at the point of instantiation,
because that may not include names that were visible when the template was
defined. More generally, if the instantiation backtrace goes through a module
M, then every declaration visible within M should be available to the
instantiation. Any of those declarations might be part of the interface that M
intended to export to a template that it instantiates.
The fix here has two parts:
1) If we find a non-visible declaration during name lookup during template
instantiation, check whether the declaration was visible from the defining
module of all entities on the active template instantiation stack. The defining
module is not the owning module in all cases: we look at the module in which a
template was defined, not the module in which it was first instantiated.
2) Perform pending instantiations at the end of a module, not at the end of the
translation unit. This is general goodness, since it significantly cuts down
the amount of redundant work that is performed in every TU importing a module,
and also implicitly adds the module containing the point of instantiation to
the set of modules checked for declarations in a lookup within a template
instantiation.
There's a known issue here with template instantiations performed while
building a module, if additional imports are added later on. I'll fix that
in a subsequent commit.
llvm-svn: 187167
2013-07-26 07:08:39 +08:00
|
|
|
if (NamedDecl *D = R.getAcceptableDecl(*I)) {
|
2009-02-25 04:03:32 +08:00
|
|
|
if (NameKind == LookupRedeclarationWithLinkage) {
|
|
|
|
// Determine whether this (or a previous) declaration is
|
|
|
|
// out-of-scope.
|
2010-08-21 17:40:31 +08:00
|
|
|
if (!LeftStartingScope && !S->isDeclScope(*I))
|
2009-02-25 04:03:32 +08:00
|
|
|
LeftStartingScope = true;
|
|
|
|
|
|
|
|
// If we found something outside of our starting scope that
|
|
|
|
// does not have linkage, skip it.
|
2013-08-14 02:18:50 +08:00
|
|
|
if (LeftStartingScope && !((*I)->hasLinkage())) {
|
|
|
|
R.setShadowed();
|
2009-02-25 04:03:32 +08:00
|
|
|
continue;
|
2013-08-14 02:18:50 +08:00
|
|
|
}
|
2009-02-25 04:03:32 +08:00
|
|
|
}
|
2011-07-13 01:16:56 +08:00
|
|
|
else if (NameKind == LookupObjCImplicitSelfParam &&
|
|
|
|
!isa<ImplicitParamDecl>(*I))
|
|
|
|
continue;
|
When we perform dependent name lookup during template instantiation, it's not
sufficient to only consider names visible at the point of instantiation,
because that may not include names that were visible when the template was
defined. More generally, if the instantiation backtrace goes through a module
M, then every declaration visible within M should be available to the
instantiation. Any of those declarations might be part of the interface that M
intended to export to a template that it instantiates.
The fix here has two parts:
1) If we find a non-visible declaration during name lookup during template
instantiation, check whether the declaration was visible from the defining
module of all entities on the active template instantiation stack. The defining
module is not the owning module in all cases: we look at the module in which a
template was defined, not the module in which it was first instantiated.
2) Perform pending instantiations at the end of a module, not at the end of the
translation unit. This is general goodness, since it significantly cuts down
the amount of redundant work that is performed in every TU importing a module,
and also implicitly adds the module containing the point of instantiation to
the set of modules checked for declarations in a lookup within a template
instantiation.
There's a known issue here with template instantiations performed while
building a module, if additional imports are added later on. I'll fix that
in a subsequent commit.
llvm-svn: 187167
2013-07-26 07:08:39 +08:00
|
|
|
|
2011-12-15 00:03:29 +08:00
|
|
|
R.addDecl(D);
|
2009-10-10 05:13:30 +08:00
|
|
|
|
2012-01-04 07:26:26 +08:00
|
|
|
// Check whether there are any other declarations with the same name
|
|
|
|
// and in the same scope.
|
2012-01-05 00:44:10 +08:00
|
|
|
if (I != IEnd) {
|
2012-01-14 07:06:53 +08:00
|
|
|
// Find the scope in which this declaration was declared (if it
|
|
|
|
// actually exists in a Scope).
|
|
|
|
while (S && !S->isDeclScope(D))
|
|
|
|
S = S->getParent();
|
|
|
|
|
|
|
|
// If the scope containing the declaration is the translation unit,
|
|
|
|
// then we'll need to perform our checks based on the matching
|
|
|
|
// DeclContexts rather than matching scopes.
|
|
|
|
if (S && isNamespaceOrTranslationUnitScope(S))
|
2014-05-26 14:22:03 +08:00
|
|
|
S = nullptr;
|
2012-01-14 07:06:53 +08:00
|
|
|
|
|
|
|
// Compute the DeclContext, if we need it.
|
2014-05-26 14:22:03 +08:00
|
|
|
DeclContext *DC = nullptr;
|
2012-01-14 07:06:53 +08:00
|
|
|
if (!S)
|
|
|
|
DC = (*I)->getDeclContext()->getRedeclContext();
|
|
|
|
|
2012-01-05 00:44:10 +08:00
|
|
|
IdentifierResolver::iterator LastI = I;
|
|
|
|
for (++LastI; LastI != IEnd; ++LastI) {
|
2012-01-14 07:06:53 +08:00
|
|
|
if (S) {
|
|
|
|
// Match based on scope.
|
|
|
|
if (!S->isDeclScope(*LastI))
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
// Match based on DeclContext.
|
|
|
|
DeclContext *LastDC
|
|
|
|
= (*LastI)->getDeclContext()->getRedeclContext();
|
|
|
|
if (!LastDC->Equals(DC))
|
|
|
|
break;
|
|
|
|
}
|
When we perform dependent name lookup during template instantiation, it's not
sufficient to only consider names visible at the point of instantiation,
because that may not include names that were visible when the template was
defined. More generally, if the instantiation backtrace goes through a module
M, then every declaration visible within M should be available to the
instantiation. Any of those declarations might be part of the interface that M
intended to export to a template that it instantiates.
The fix here has two parts:
1) If we find a non-visible declaration during name lookup during template
instantiation, check whether the declaration was visible from the defining
module of all entities on the active template instantiation stack. The defining
module is not the owning module in all cases: we look at the module in which a
template was defined, not the module in which it was first instantiated.
2) Perform pending instantiations at the end of a module, not at the end of the
translation unit. This is general goodness, since it significantly cuts down
the amount of redundant work that is performed in every TU importing a module,
and also implicitly adds the module containing the point of instantiation to
the set of modules checked for declarations in a lookup within a template
instantiation.
There's a known issue here with template instantiations performed while
building a module, if additional imports are added later on. I'll fix that
in a subsequent commit.
llvm-svn: 187167
2013-07-26 07:08:39 +08:00
|
|
|
|
|
|
|
// If the declaration is in the right namespace and visible, add it.
|
|
|
|
if (NamedDecl *LastD = R.getAcceptableDecl(*LastI))
|
|
|
|
R.addDecl(LastD);
|
2012-01-05 00:44:10 +08:00
|
|
|
}
|
2012-01-04 07:26:26 +08:00
|
|
|
|
2012-01-05 00:44:10 +08:00
|
|
|
R.resolveKind();
|
Initial implementation of function overloading in C.
This commit adds a new attribute, "overloadable", that enables C++
function overloading in C. The attribute can only be added to function
declarations, e.g.,
int *f(int) __attribute__((overloadable));
If the "overloadable" attribute exists on a function with a given
name, *all* functions with that name (and in that scope) must have the
"overloadable" attribute. Sets of overloaded functions with the
"overloadable" attribute then follow the normal C++ rules for
overloaded functions, e.g., overloads must have different
parameter-type-lists from each other.
When calling an overloaded function in C, we follow the same
overloading rules as C++, with three extensions to the set of standard
conversions:
- A value of a given struct or union type T can be converted to the
type T. This is just the identity conversion. (In C++, this would
go through a copy constructor).
- A value of pointer type T* can be converted to a value of type U*
if T and U are compatible types. This conversion has Conversion
rank (it's considered a pointer conversion in C).
- A value of type T can be converted to a value of type U if T and U
are compatible (and are not both pointer types). This conversion
has Conversion rank (it's considered to be a new kind of
conversion unique to C, a "compatible" conversion).
Known defects (and, therefore, next steps):
1) The standard-conversion handling does not understand conversions
involving _Complex or vector extensions, so it is likely to get
these wrong. We need to add these conversions.
2) All overloadable functions with the same name will have the same
linkage name, which means we'll get a collision in the linker (if
not sooner). We'll need to mangle the names of these functions.
llvm-svn: 64336
2009-02-12 07:02:49 +08:00
|
|
|
}
|
2013-09-20 09:15:31 +08:00
|
|
|
|
2009-10-10 05:13:30 +08:00
|
|
|
return true;
|
Initial implementation of function overloading in C.
This commit adds a new attribute, "overloadable", that enables C++
function overloading in C. The attribute can only be added to function
declarations, e.g.,
int *f(int) __attribute__((overloadable));
If the "overloadable" attribute exists on a function with a given
name, *all* functions with that name (and in that scope) must have the
"overloadable" attribute. Sets of overloaded functions with the
"overloadable" attribute then follow the normal C++ rules for
overloaded functions, e.g., overloads must have different
parameter-type-lists from each other.
When calling an overloaded function in C, we follow the same
overloading rules as C++, with three extensions to the set of standard
conversions:
- A value of a given struct or union type T can be converted to the
type T. This is just the identity conversion. (In C++, this would
go through a copy constructor).
- A value of pointer type T* can be converted to a value of type U*
if T and U are compatible types. This conversion has Conversion
rank (it's considered a pointer conversion in C).
- A value of type T can be converted to a value of type U if T and U
are compatible (and are not both pointer types). This conversion
has Conversion rank (it's considered to be a new kind of
conversion unique to C, a "compatible" conversion).
Known defects (and, therefore, next steps):
1) The standard-conversion handling does not understand conversions
involving _Complex or vector extensions, so it is likely to get
these wrong. We need to add these conversions.
2) All overloadable functions with the same name will have the same
linkage name, which means we'll get a collision in the linker (if
not sooner). We'll need to mangle the names of these functions.
llvm-svn: 64336
2009-02-12 07:02:49 +08:00
|
|
|
}
|
2009-01-15 06:20:51 +08:00
|
|
|
} else {
|
2009-02-04 03:21:40 +08:00
|
|
|
// Perform C++ unqualified name lookup.
|
2009-11-17 10:14:36 +08:00
|
|
|
if (CppLookupName(R, S))
|
2009-10-10 05:13:30 +08:00
|
|
|
return true;
|
2009-01-15 06:20:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we didn't find a use of this identifier, and if the identifier
|
|
|
|
// corresponds to a compiler builtin, create the decl object for the builtin
|
|
|
|
// now, injecting it into translation unit scope, and return it.
|
2011-04-13 21:19:46 +08:00
|
|
|
if (AllowBuiltinCreation && LookupBuiltin(*this, R))
|
|
|
|
return true;
|
Implicitly declare certain C library functions (malloc, strcpy, memmove,
etc.) when we perform name lookup on them. This ensures that we
produce the correct signature for these functions, which has two
practical impacts:
1) When we're supporting the "implicit function declaration" feature
of C99, these functions will be implicitly declared with the right
signature rather than as a function returning "int" with no
prototype. See PR3541 for the reason why this is important (hint:
GCC always predeclares these functions).
2) If users attempt to redeclare one of these library functions with
an incompatible signature, we produce a hard error.
This patch does a little bit of work to give reasonable error
messages. For example, when we hit case #1 we complain that we're
implicitly declaring this function with a specific signature, and then
we give a note that asks the user to include the appropriate header
(e.g., "please include <stdlib.h> or explicitly declare 'malloc'"). In
case #2, we show the type of the implicit builtin that was incorrectly
declared, so the user can see the problem. We could do better here:
for example, when displaying this latter error message we say
something like:
'strcpy' was implicitly declared here with type 'char *(char *, char
const *)'
but we should really print out a fake code line showing the
declaration, like this:
'strcpy' was implicitly declared here as:
char *strcpy(char *, char const *)
This would also be good for printing built-in candidates with C++
operator overloading.
The set of C library functions supported by this patch includes all
functions from the C99 specification's <stdlib.h> and <string.h> that
(a) are predefined by GCC and (b) have signatures that could cause
codegen issues if they are treated as functions with no prototype
returning and int. Future work could extend this set of functions to
other C library functions that we know about.
llvm-svn: 64504
2009-02-14 07:20:09 +08:00
|
|
|
|
2011-02-25 00:47:47 +08:00
|
|
|
// If we didn't find a use of this identifier, the ExternalSource
|
|
|
|
// may be able to handle the situation.
|
|
|
|
// Note: some lookup failures are expected!
|
|
|
|
// See e.g. R.isForRedeclaration().
|
|
|
|
return (ExternalSource && ExternalSource->LookupUnqualified(R, S));
|
2009-01-15 06:20:51 +08:00
|
|
|
}
|
|
|
|
|
2009-10-10 13:48:19 +08:00
|
|
|
/// @brief Perform qualified name lookup in the namespaces nominated by
|
|
|
|
/// using directives by the given context.
|
|
|
|
///
|
|
|
|
/// C++98 [namespace.qual]p2:
|
2012-06-20 05:05:49 +08:00
|
|
|
/// Given X::m (where X is a user-declared namespace), or given \::m
|
2009-10-10 13:48:19 +08:00
|
|
|
/// (where X is the global namespace), let S be the set of all
|
|
|
|
/// declarations of m in X and in the transitive closure of all
|
|
|
|
/// namespaces nominated by using-directives in X and its used
|
|
|
|
/// namespaces, except that using-directives are ignored in any
|
|
|
|
/// namespace, including X, directly containing one or more
|
|
|
|
/// declarations of m. No namespace is searched more than once in
|
|
|
|
/// the lookup of a name. If S is the empty set, the program is
|
|
|
|
/// ill-formed. Otherwise, if S has exactly one member, or if the
|
|
|
|
/// context of the reference is a using-declaration
|
|
|
|
/// (namespace.udecl), S is the required set of declarations of
|
|
|
|
/// m. Otherwise if the use of m is not one that allows a unique
|
|
|
|
/// declaration to be chosen from S, the program is ill-formed.
|
2012-06-20 05:05:49 +08:00
|
|
|
///
|
2009-10-10 13:48:19 +08:00
|
|
|
/// C++98 [namespace.qual]p5:
|
|
|
|
/// During the lookup of a qualified namespace member name, if the
|
|
|
|
/// lookup finds more than one declaration of the member, and if one
|
|
|
|
/// declaration introduces a class name or enumeration name and the
|
|
|
|
/// other declarations either introduce the same object, the same
|
|
|
|
/// enumerator or a set of functions, the non-type name hides the
|
|
|
|
/// class or enumeration name if and only if the declarations are
|
|
|
|
/// from the same namespace; otherwise (the declarations are from
|
|
|
|
/// different namespaces), the program is ill-formed.
|
2010-02-12 13:48:04 +08:00
|
|
|
static bool LookupQualifiedNameInUsingDirectives(Sema &S, LookupResult &R,
|
2009-11-17 10:14:36 +08:00
|
|
|
DeclContext *StartDC) {
|
2009-10-10 13:48:19 +08:00
|
|
|
assert(StartDC->isFileContext() && "start context is not a file context");
|
|
|
|
|
2014-03-18 01:14:12 +08:00
|
|
|
DeclContext::udir_range UsingDirectives = StartDC->using_directives();
|
|
|
|
if (UsingDirectives.begin() == UsingDirectives.end()) return false;
|
2009-10-10 13:48:19 +08:00
|
|
|
|
|
|
|
// We have at least added all these contexts to the queue.
|
2012-02-24 00:06:01 +08:00
|
|
|
llvm::SmallPtrSet<DeclContext*, 8> Visited;
|
2009-10-10 13:48:19 +08:00
|
|
|
Visited.insert(StartDC);
|
|
|
|
|
|
|
|
// We have not yet looked into these namespaces, much less added
|
|
|
|
// their "using-children" to the queue.
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<NamespaceDecl*, 8> Queue;
|
2009-10-10 13:48:19 +08:00
|
|
|
|
|
|
|
// We have already looked into the initial namespace; seed the queue
|
|
|
|
// with its using-children.
|
2014-03-18 01:14:12 +08:00
|
|
|
for (auto *I : UsingDirectives) {
|
|
|
|
NamespaceDecl *ND = I->getNominatedNamespace()->getOriginalNamespace();
|
2014-11-19 15:49:47 +08:00
|
|
|
if (Visited.insert(ND).second)
|
2009-10-10 13:48:19 +08:00
|
|
|
Queue.push_back(ND);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The easiest way to implement the restriction in [namespace.qual]p5
|
|
|
|
// is to check whether any of the individual results found a tag
|
|
|
|
// and, if so, to declare an ambiguity if the final result is not
|
|
|
|
// a tag.
|
|
|
|
bool FoundTag = false;
|
|
|
|
bool FoundNonTag = false;
|
|
|
|
|
2009-11-18 15:57:50 +08:00
|
|
|
LookupResult LocalR(LookupResult::Temporary, R);
|
2009-10-10 13:48:19 +08:00
|
|
|
|
|
|
|
bool Found = false;
|
|
|
|
while (!Queue.empty()) {
|
2013-08-24 00:11:15 +08:00
|
|
|
NamespaceDecl *ND = Queue.pop_back_val();
|
2009-10-10 13:48:19 +08:00
|
|
|
|
|
|
|
// We go through some convolutions here to avoid copying results
|
|
|
|
// between LookupResults.
|
|
|
|
bool UseLocal = !R.empty();
|
2009-11-18 15:57:50 +08:00
|
|
|
LookupResult &DirectR = UseLocal ? LocalR : R;
|
2010-02-12 13:48:04 +08:00
|
|
|
bool FoundDirect = LookupDirect(S, DirectR, ND);
|
2009-10-10 13:48:19 +08:00
|
|
|
|
|
|
|
if (FoundDirect) {
|
|
|
|
// First do any local hiding.
|
|
|
|
DirectR.resolveKind();
|
|
|
|
|
|
|
|
// If the local result is a tag, remember that.
|
|
|
|
if (DirectR.isSingleTagDecl())
|
|
|
|
FoundTag = true;
|
|
|
|
else
|
|
|
|
FoundNonTag = true;
|
|
|
|
|
|
|
|
// Append the local results to the total results if necessary.
|
|
|
|
if (UseLocal) {
|
|
|
|
R.addAllDecls(LocalR);
|
|
|
|
LocalR.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we find names in this namespace, ignore its using directives.
|
|
|
|
if (FoundDirect) {
|
|
|
|
Found = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-03-18 01:14:12 +08:00
|
|
|
for (auto I : ND->using_directives()) {
|
2014-03-07 21:44:44 +08:00
|
|
|
NamespaceDecl *Nom = I->getNominatedNamespace();
|
2014-11-19 15:49:47 +08:00
|
|
|
if (Visited.insert(Nom).second)
|
2009-10-10 13:48:19 +08:00
|
|
|
Queue.push_back(Nom);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Found) {
|
|
|
|
if (FoundTag && FoundNonTag)
|
|
|
|
R.setAmbiguousQualifiedTagHiding();
|
|
|
|
else
|
|
|
|
R.resolveKind();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Found;
|
|
|
|
}
|
|
|
|
|
2010-08-15 14:18:01 +08:00
|
|
|
/// \brief Callback that looks for any member of a class with the given name.
|
2011-01-27 15:10:08 +08:00
|
|
|
static bool LookupAnyMember(const CXXBaseSpecifier *Specifier,
|
2015-07-25 23:07:25 +08:00
|
|
|
CXXBasePath &Path, DeclarationName Name) {
|
2010-08-15 14:18:01 +08:00
|
|
|
RecordDecl *BaseRecord = Specifier->getType()->getAs<RecordType>()->getDecl();
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2015-07-25 23:07:25 +08:00
|
|
|
Path.Decls = BaseRecord->lookup(Name);
|
2012-12-19 08:45:41 +08:00
|
|
|
return !Path.Decls.empty();
|
2010-08-15 14:18:01 +08:00
|
|
|
}
|
|
|
|
|
2011-01-27 15:10:08 +08:00
|
|
|
/// \brief Determine whether the given set of member declarations contains only
|
2010-10-23 06:08:47 +08:00
|
|
|
/// static members, nested types, and enumerators.
|
|
|
|
template<typename InputIterator>
|
|
|
|
static bool HasOnlyStaticMembers(InputIterator First, InputIterator Last) {
|
|
|
|
Decl *D = (*First)->getUnderlyingDecl();
|
|
|
|
if (isa<VarDecl>(D) || isa<TypeDecl>(D) || isa<EnumConstantDecl>(D))
|
|
|
|
return true;
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2010-10-23 06:08:47 +08:00
|
|
|
if (isa<CXXMethodDecl>(D)) {
|
|
|
|
// Determine whether all of the methods are static.
|
|
|
|
bool AllMethodsAreStatic = true;
|
|
|
|
for(; First != Last; ++First) {
|
|
|
|
D = (*First)->getUnderlyingDecl();
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2010-10-23 06:08:47 +08:00
|
|
|
if (!isa<CXXMethodDecl>(D)) {
|
|
|
|
assert(isa<TagDecl>(D) && "Non-function must be a tag decl");
|
|
|
|
break;
|
|
|
|
}
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2010-10-23 06:08:47 +08:00
|
|
|
if (!cast<CXXMethodDecl>(D)->isStatic()) {
|
|
|
|
AllMethodsAreStatic = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2010-10-23 06:08:47 +08:00
|
|
|
if (AllMethodsAreStatic)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-01-15 09:44:47 +08:00
|
|
|
/// \brief Perform qualified name lookup into a given context.
|
2009-01-15 06:20:51 +08:00
|
|
|
///
|
|
|
|
/// Qualified name lookup (C++ [basic.lookup.qual]) is used to find
|
|
|
|
/// names when the context of those names is explicit specified, e.g.,
|
2010-01-15 09:44:47 +08:00
|
|
|
/// "std::vector" or "x->member", or as part of unqualified name lookup.
|
2009-01-15 06:20:51 +08:00
|
|
|
///
|
|
|
|
/// Different lookup criteria can find different names. For example, a
|
|
|
|
/// particular scope can have both a struct and a function of the same
|
|
|
|
/// name, and each can be found by certain lookup criteria. For more
|
|
|
|
/// information about lookup criteria, see the documentation for the
|
|
|
|
/// class LookupCriteria.
|
|
|
|
///
|
2010-01-15 09:44:47 +08:00
|
|
|
/// \param R captures both the lookup criteria and any lookup results found.
|
|
|
|
///
|
|
|
|
/// \param LookupCtx The context in which qualified name lookup will
|
2009-01-15 06:20:51 +08:00
|
|
|
/// search. If the lookup criteria permits, name lookup may also search
|
|
|
|
/// in the parent contexts or (for C++ classes) base classes.
|
|
|
|
///
|
2011-01-27 15:10:08 +08:00
|
|
|
/// \param InUnqualifiedLookup true if this is qualified name lookup that
|
2010-01-15 09:44:47 +08:00
|
|
|
/// occurs as part of unqualified name lookup.
|
2009-01-15 06:20:51 +08:00
|
|
|
///
|
2010-01-15 09:44:47 +08:00
|
|
|
/// \returns true if lookup succeeded, false if it failed.
|
|
|
|
bool Sema::LookupQualifiedName(LookupResult &R, DeclContext *LookupCtx,
|
|
|
|
bool InUnqualifiedLookup) {
|
2009-01-15 06:20:51 +08:00
|
|
|
assert(LookupCtx && "Sema::LookupQualifiedName requires a lookup context");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-17 10:14:36 +08:00
|
|
|
if (!R.getLookupName())
|
2009-10-10 05:13:30 +08:00
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-09-03 06:59:36 +08:00
|
|
|
// Make sure that the declaration context is complete.
|
|
|
|
assert((!isa<TagDecl>(LookupCtx) ||
|
|
|
|
LookupCtx->isDependentContext() ||
|
2011-10-07 14:10:15 +08:00
|
|
|
cast<TagDecl>(LookupCtx)->isCompleteDefinition() ||
|
2012-03-23 11:33:32 +08:00
|
|
|
cast<TagDecl>(LookupCtx)->isBeingDefined()) &&
|
2009-09-03 06:59:36 +08:00
|
|
|
"Declaration context must already be complete!");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2015-11-18 20:48:05 +08:00
|
|
|
struct QualifiedLookupInScope {
|
|
|
|
bool oldVal;
|
|
|
|
DeclContext *Context;
|
|
|
|
// Set flag in DeclContext informing debugger that we're looking for qualified name
|
|
|
|
QualifiedLookupInScope(DeclContext *ctx) : Context(ctx) {
|
|
|
|
oldVal = ctx->setUseQualifiedLookup();
|
|
|
|
}
|
|
|
|
~QualifiedLookupInScope() {
|
|
|
|
Context->setUseQualifiedLookup(oldVal);
|
|
|
|
}
|
|
|
|
} QL(LookupCtx);
|
|
|
|
|
2010-02-12 13:48:04 +08:00
|
|
|
if (LookupDirect(*this, R, LookupCtx)) {
|
2009-10-10 05:13:30 +08:00
|
|
|
R.resolveKind();
|
2010-01-23 08:46:32 +08:00
|
|
|
if (isa<CXXRecordDecl>(LookupCtx))
|
|
|
|
R.setNamingClass(cast<CXXRecordDecl>(LookupCtx));
|
2009-10-10 05:13:30 +08:00
|
|
|
return true;
|
|
|
|
}
|
2009-01-15 06:20:51 +08:00
|
|
|
|
2009-10-10 13:48:19 +08:00
|
|
|
// Don't descend into implied contexts for redeclarations.
|
|
|
|
// C++98 [namespace.qual]p6:
|
|
|
|
// In a declaration for a namespace member in which the
|
|
|
|
// declarator-id is a qualified-id, given that the qualified-id
|
|
|
|
// for the namespace member has the form
|
|
|
|
// nested-name-specifier unqualified-id
|
|
|
|
// the unqualified-id shall name a member of the namespace
|
|
|
|
// designated by the nested-name-specifier.
|
|
|
|
// See also [class.mfct]p5 and [class.static.data]p2.
|
2009-11-17 10:14:36 +08:00
|
|
|
if (R.isForRedeclaration())
|
2009-10-10 13:48:19 +08:00
|
|
|
return false;
|
|
|
|
|
2009-11-17 10:14:36 +08:00
|
|
|
// If this is a namespace, look it up in the implied namespaces.
|
2009-10-10 13:48:19 +08:00
|
|
|
if (LookupCtx->isFileContext())
|
2010-02-12 13:48:04 +08:00
|
|
|
return LookupQualifiedNameInUsingDirectives(*this, R, LookupCtx);
|
2009-10-10 13:48:19 +08:00
|
|
|
|
2009-09-03 06:59:36 +08:00
|
|
|
// If this isn't a C++ class, we aren't allowed to look into base
|
2009-09-12 06:57:37 +08:00
|
|
|
// classes, we're done.
|
2010-01-15 09:44:47 +08:00
|
|
|
CXXRecordDecl *LookupRec = dyn_cast<CXXRecordDecl>(LookupCtx);
|
2010-07-01 08:21:21 +08:00
|
|
|
if (!LookupRec || !LookupRec->getDefinition())
|
2009-10-10 05:13:30 +08:00
|
|
|
return false;
|
2009-01-15 08:26:24 +08:00
|
|
|
|
2010-01-15 09:44:47 +08:00
|
|
|
// If we're performing qualified name lookup into a dependent class,
|
|
|
|
// then we are actually looking into a current instantiation. If we have any
|
2011-01-27 15:10:08 +08:00
|
|
|
// dependent base classes, then we either have to delay lookup until
|
2010-01-15 09:44:47 +08:00
|
|
|
// template instantiation time (at which point all bases will be available)
|
|
|
|
// or we have to fail.
|
|
|
|
if (!InUnqualifiedLookup && LookupRec->isDependentContext() &&
|
|
|
|
LookupRec->hasAnyDependentBases()) {
|
|
|
|
R.setNotFoundInCurrentInstantiation();
|
|
|
|
return false;
|
|
|
|
}
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2009-01-15 08:26:24 +08:00
|
|
|
// Perform lookup into our base classes.
|
2009-10-07 01:59:45 +08:00
|
|
|
CXXBasePaths Paths;
|
|
|
|
Paths.setOrigin(LookupRec);
|
2009-01-15 08:26:24 +08:00
|
|
|
|
|
|
|
// Look for this member in our base classes
|
2015-07-25 23:07:25 +08:00
|
|
|
bool (*BaseCallback)(const CXXBaseSpecifier *Specifier, CXXBasePath &Path,
|
|
|
|
DeclarationName Name) = nullptr;
|
2009-11-17 10:14:36 +08:00
|
|
|
switch (R.getLookupKind()) {
|
2011-07-13 01:16:56 +08:00
|
|
|
case LookupObjCImplicitSelfParam:
|
2009-10-07 01:59:45 +08:00
|
|
|
case LookupOrdinaryName:
|
|
|
|
case LookupMemberName:
|
|
|
|
case LookupRedeclarationWithLinkage:
|
2013-08-09 12:35:01 +08:00
|
|
|
case LookupLocalFriendName:
|
2009-10-07 01:59:45 +08:00
|
|
|
BaseCallback = &CXXRecordDecl::FindOrdinaryMember;
|
|
|
|
break;
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2009-10-07 01:59:45 +08:00
|
|
|
case LookupTagName:
|
|
|
|
BaseCallback = &CXXRecordDecl::FindTagMember;
|
|
|
|
break;
|
2009-12-10 17:41:52 +08:00
|
|
|
|
2010-08-15 14:18:01 +08:00
|
|
|
case LookupAnyName:
|
|
|
|
BaseCallback = &LookupAnyMember;
|
|
|
|
break;
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2009-12-10 17:41:52 +08:00
|
|
|
case LookupUsingDeclName:
|
|
|
|
// This lookup is for redeclarations only.
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2009-10-07 01:59:45 +08:00
|
|
|
case LookupOperatorName:
|
|
|
|
case LookupNamespaceName:
|
|
|
|
case LookupObjCProtocolName:
|
2011-02-18 09:27:55 +08:00
|
|
|
case LookupLabel:
|
2009-10-07 01:59:45 +08:00
|
|
|
// These lookups will never find a member in a C++ class (or base class).
|
2009-10-10 05:13:30 +08:00
|
|
|
return false;
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2009-10-07 01:59:45 +08:00
|
|
|
case LookupNestedNameSpecifierName:
|
|
|
|
BaseCallback = &CXXRecordDecl::FindNestedNameSpecifierMember;
|
|
|
|
break;
|
|
|
|
}
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2015-07-25 23:07:25 +08:00
|
|
|
DeclarationName Name = R.getLookupName();
|
|
|
|
if (!LookupRec->lookupInBases(
|
|
|
|
[=](const CXXBaseSpecifier *Specifier, CXXBasePath &Path) {
|
|
|
|
return BaseCallback(Specifier, Path, Name);
|
|
|
|
},
|
|
|
|
Paths))
|
2009-10-10 05:13:30 +08:00
|
|
|
return false;
|
2009-01-15 08:26:24 +08:00
|
|
|
|
2010-01-23 08:46:32 +08:00
|
|
|
R.setNamingClass(LookupRec);
|
|
|
|
|
2009-01-15 08:26:24 +08:00
|
|
|
// C++ [class.member.lookup]p2:
|
|
|
|
// [...] If the resulting set of declarations are not all from
|
|
|
|
// sub-objects of the same type, or the set has a nonstatic member
|
|
|
|
// and includes members from distinct sub-objects, there is an
|
|
|
|
// ambiguity and the program is ill-formed. Otherwise that set is
|
|
|
|
// the result of the lookup.
|
|
|
|
QualType SubobjectType;
|
2009-01-16 02:32:35 +08:00
|
|
|
int SubobjectNumber = 0;
|
2010-03-19 07:49:19 +08:00
|
|
|
AccessSpecifier SubobjectAccess = AS_none;
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2009-10-07 01:59:45 +08:00
|
|
|
for (CXXBasePaths::paths_iterator Path = Paths.begin(), PathEnd = Paths.end();
|
2009-01-15 08:26:24 +08:00
|
|
|
Path != PathEnd; ++Path) {
|
2009-10-07 01:59:45 +08:00
|
|
|
const CXXBasePathElement &PathElement = Path->back();
|
2009-01-15 08:26:24 +08:00
|
|
|
|
2010-01-21 05:53:11 +08:00
|
|
|
// Pick the best (i.e. most permissive i.e. numerically lowest) access
|
|
|
|
// across all paths.
|
|
|
|
SubobjectAccess = std::min(SubobjectAccess, Path->Access);
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2009-01-15 08:26:24 +08:00
|
|
|
// Determine whether we're looking at a distinct sub-object or not.
|
|
|
|
if (SubobjectType.isNull()) {
|
2009-10-10 05:13:30 +08:00
|
|
|
// This is the first subobject we've looked at. Record its type.
|
2009-01-15 08:26:24 +08:00
|
|
|
SubobjectType = Context.getCanonicalType(PathElement.Base->getType());
|
|
|
|
SubobjectNumber = PathElement.SubobjectNumber;
|
2010-10-23 06:08:47 +08:00
|
|
|
continue;
|
2011-01-27 15:10:08 +08:00
|
|
|
}
|
|
|
|
|
2010-10-23 06:08:47 +08:00
|
|
|
if (SubobjectType
|
2009-01-15 08:26:24 +08:00
|
|
|
!= Context.getCanonicalType(PathElement.Base->getType())) {
|
|
|
|
// We found members of the given name in two subobjects of
|
2010-10-23 06:08:47 +08:00
|
|
|
// different types. If the declaration sets aren't the same, this
|
2014-07-09 13:42:35 +08:00
|
|
|
// lookup is ambiguous.
|
2012-12-19 08:45:41 +08:00
|
|
|
if (HasOnlyStaticMembers(Path->Decls.begin(), Path->Decls.end())) {
|
2010-10-23 06:08:47 +08:00
|
|
|
CXXBasePaths::paths_iterator FirstPath = Paths.begin();
|
2012-12-19 08:45:41 +08:00
|
|
|
DeclContext::lookup_iterator FirstD = FirstPath->Decls.begin();
|
|
|
|
DeclContext::lookup_iterator CurrentD = Path->Decls.begin();
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2012-12-19 08:45:41 +08:00
|
|
|
while (FirstD != FirstPath->Decls.end() &&
|
|
|
|
CurrentD != Path->Decls.end()) {
|
2010-10-23 06:08:47 +08:00
|
|
|
if ((*FirstD)->getUnderlyingDecl()->getCanonicalDecl() !=
|
|
|
|
(*CurrentD)->getUnderlyingDecl()->getCanonicalDecl())
|
|
|
|
break;
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2010-10-23 06:08:47 +08:00
|
|
|
++FirstD;
|
|
|
|
++CurrentD;
|
|
|
|
}
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2012-12-19 08:45:41 +08:00
|
|
|
if (FirstD == FirstPath->Decls.end() &&
|
|
|
|
CurrentD == Path->Decls.end())
|
2010-10-23 06:08:47 +08:00
|
|
|
continue;
|
|
|
|
}
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2009-10-10 05:13:30 +08:00
|
|
|
R.setAmbiguousBaseSubobjectTypes(Paths);
|
|
|
|
return true;
|
2011-01-27 15:10:08 +08:00
|
|
|
}
|
|
|
|
|
2010-10-23 06:08:47 +08:00
|
|
|
if (SubobjectNumber != PathElement.SubobjectNumber) {
|
2009-01-15 08:26:24 +08:00
|
|
|
// We have a different subobject of the same type.
|
|
|
|
|
|
|
|
// C++ [class.member.lookup]p5:
|
|
|
|
// A static member, a nested type or an enumerator defined in
|
|
|
|
// a base class T can unambiguously be found even if an object
|
2009-09-09 23:08:12 +08:00
|
|
|
// has more than one base class subobject of type T.
|
2012-12-19 08:45:41 +08:00
|
|
|
if (HasOnlyStaticMembers(Path->Decls.begin(), Path->Decls.end()))
|
2009-01-15 08:26:24 +08:00
|
|
|
continue;
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2009-01-15 08:26:24 +08:00
|
|
|
// We have found a nonstatic member name in multiple, distinct
|
|
|
|
// subobjects. Name lookup is ambiguous.
|
2009-10-10 05:13:30 +08:00
|
|
|
R.setAmbiguousBaseSubobjects(Paths);
|
|
|
|
return true;
|
2009-01-15 08:26:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup in a base class succeeded; return these results.
|
|
|
|
|
2014-07-16 06:03:49 +08:00
|
|
|
for (auto *D : Paths.front().Decls) {
|
2010-01-23 08:46:32 +08:00
|
|
|
AccessSpecifier AS = CXXRecordDecl::MergeAccess(SubobjectAccess,
|
|
|
|
D->getAccess());
|
|
|
|
R.addDecl(D, AS);
|
|
|
|
}
|
2009-10-10 05:13:30 +08:00
|
|
|
R.resolveKind();
|
|
|
|
return true;
|
2009-01-15 06:20:51 +08:00
|
|
|
}
|
|
|
|
|
2014-12-02 07:15:01 +08:00
|
|
|
/// \brief Performs qualified name lookup or special type of lookup for
|
|
|
|
/// "__super::" scope specifier.
|
|
|
|
///
|
|
|
|
/// This routine is a convenience overload meant to be called from contexts
|
|
|
|
/// that need to perform a qualified name lookup with an optional C++ scope
|
|
|
|
/// specifier that might require special kind of lookup.
|
|
|
|
///
|
|
|
|
/// \param R captures both the lookup criteria and any lookup results found.
|
|
|
|
///
|
|
|
|
/// \param LookupCtx The context in which qualified name lookup will
|
|
|
|
/// search.
|
|
|
|
///
|
|
|
|
/// \param SS An optional C++ scope-specifier.
|
|
|
|
///
|
|
|
|
/// \returns true if lookup succeeded, false if it failed.
|
|
|
|
bool Sema::LookupQualifiedName(LookupResult &R, DeclContext *LookupCtx,
|
|
|
|
CXXScopeSpec &SS) {
|
|
|
|
auto *NNS = SS.getScopeRep();
|
|
|
|
if (NNS && NNS->getKind() == NestedNameSpecifier::Super)
|
|
|
|
return LookupInSuper(R, NNS->getAsRecordDecl());
|
|
|
|
else
|
|
|
|
|
|
|
|
return LookupQualifiedName(R, LookupCtx);
|
|
|
|
}
|
|
|
|
|
2009-01-15 06:20:51 +08:00
|
|
|
/// @brief Performs name lookup for a name that was parsed in the
|
|
|
|
/// source code, and may contain a C++ scope specifier.
|
|
|
|
///
|
|
|
|
/// This routine is a convenience routine meant to be called from
|
|
|
|
/// contexts that receive a name and an optional C++ scope specifier
|
|
|
|
/// (e.g., "N::M::x"). It will then perform either qualified or
|
|
|
|
/// unqualified name lookup (with LookupQualifiedName or LookupName,
|
2014-10-04 18:17:57 +08:00
|
|
|
/// respectively) on the given name and return those results. It will
|
|
|
|
/// perform a special type of lookup for "__super::" scope specifier.
|
2009-01-15 06:20:51 +08:00
|
|
|
///
|
|
|
|
/// @param S The scope from which unqualified name lookup will
|
|
|
|
/// begin.
|
2009-09-09 23:08:12 +08:00
|
|
|
///
|
Improve support for out-of-line definitions of nested templates and
their members, including member class template, member function
templates, and member classes and functions of member templates.
To actually parse the nested-name-specifiers that qualify the name of
an out-of-line definition of a member template, e.g.,
template<typename X> template<typename Y>
X Outer<X>::Inner1<Y>::foo(Y) {
return X();
}
we need to look for the template names (e.g., "Inner1") as a member of
the current instantiation (Outer<X>), even before we have entered the
scope of the current instantiation. Since we can't do this in general
(i.e., we should not be looking into all dependent
nested-name-specifiers as if they were the current instantiation), we
rely on the parser to tell us when it is parsing a declaration
specifier sequence, and, therefore, when we should consider the
current scope specifier to be a current instantiation.
Printing of complicated, dependent nested-name-specifiers may be
somewhat broken by this commit; I'll add tests for this issue and fix
the problem (if it still exists) in a subsequent commit.
llvm-svn: 80044
2009-08-26 06:51:20 +08:00
|
|
|
/// @param SS An optional C++ scope-specifier, e.g., "::N::M".
|
2009-01-15 06:20:51 +08:00
|
|
|
///
|
Improve support for out-of-line definitions of nested templates and
their members, including member class template, member function
templates, and member classes and functions of member templates.
To actually parse the nested-name-specifiers that qualify the name of
an out-of-line definition of a member template, e.g.,
template<typename X> template<typename Y>
X Outer<X>::Inner1<Y>::foo(Y) {
return X();
}
we need to look for the template names (e.g., "Inner1") as a member of
the current instantiation (Outer<X>), even before we have entered the
scope of the current instantiation. Since we can't do this in general
(i.e., we should not be looking into all dependent
nested-name-specifiers as if they were the current instantiation), we
rely on the parser to tell us when it is parsing a declaration
specifier sequence, and, therefore, when we should consider the
current scope specifier to be a current instantiation.
Printing of complicated, dependent nested-name-specifiers may be
somewhat broken by this commit; I'll add tests for this issue and fix
the problem (if it still exists) in a subsequent commit.
llvm-svn: 80044
2009-08-26 06:51:20 +08:00
|
|
|
/// @param EnteringContext Indicates whether we are going to enter the
|
|
|
|
/// context of the scope-specifier SS (if present).
|
|
|
|
///
|
2009-10-10 05:13:30 +08:00
|
|
|
/// @returns True if any decls were found (but possibly ambiguous)
|
2010-04-09 00:38:48 +08:00
|
|
|
bool Sema::LookupParsedName(LookupResult &R, Scope *S, CXXScopeSpec *SS,
|
2009-11-17 10:14:36 +08:00
|
|
|
bool AllowBuiltinCreation, bool EnteringContext) {
|
Improve support for out-of-line definitions of nested templates and
their members, including member class template, member function
templates, and member classes and functions of member templates.
To actually parse the nested-name-specifiers that qualify the name of
an out-of-line definition of a member template, e.g.,
template<typename X> template<typename Y>
X Outer<X>::Inner1<Y>::foo(Y) {
return X();
}
we need to look for the template names (e.g., "Inner1") as a member of
the current instantiation (Outer<X>), even before we have entered the
scope of the current instantiation. Since we can't do this in general
(i.e., we should not be looking into all dependent
nested-name-specifiers as if they were the current instantiation), we
rely on the parser to tell us when it is parsing a declaration
specifier sequence, and, therefore, when we should consider the
current scope specifier to be a current instantiation.
Printing of complicated, dependent nested-name-specifiers may be
somewhat broken by this commit; I'll add tests for this issue and fix
the problem (if it still exists) in a subsequent commit.
llvm-svn: 80044
2009-08-26 06:51:20 +08:00
|
|
|
if (SS && SS->isInvalid()) {
|
|
|
|
// When the scope specifier is invalid, don't even look for
|
2009-05-12 03:58:34 +08:00
|
|
|
// anything.
|
2009-10-10 05:13:30 +08:00
|
|
|
return false;
|
Improve support for out-of-line definitions of nested templates and
their members, including member class template, member function
templates, and member classes and functions of member templates.
To actually parse the nested-name-specifiers that qualify the name of
an out-of-line definition of a member template, e.g.,
template<typename X> template<typename Y>
X Outer<X>::Inner1<Y>::foo(Y) {
return X();
}
we need to look for the template names (e.g., "Inner1") as a member of
the current instantiation (Outer<X>), even before we have entered the
scope of the current instantiation. Since we can't do this in general
(i.e., we should not be looking into all dependent
nested-name-specifiers as if they were the current instantiation), we
rely on the parser to tell us when it is parsing a declaration
specifier sequence, and, therefore, when we should consider the
current scope specifier to be a current instantiation.
Printing of complicated, dependent nested-name-specifiers may be
somewhat broken by this commit; I'll add tests for this issue and fix
the problem (if it still exists) in a subsequent commit.
llvm-svn: 80044
2009-08-26 06:51:20 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
Improve support for out-of-line definitions of nested templates and
their members, including member class template, member function
templates, and member classes and functions of member templates.
To actually parse the nested-name-specifiers that qualify the name of
an out-of-line definition of a member template, e.g.,
template<typename X> template<typename Y>
X Outer<X>::Inner1<Y>::foo(Y) {
return X();
}
we need to look for the template names (e.g., "Inner1") as a member of
the current instantiation (Outer<X>), even before we have entered the
scope of the current instantiation. Since we can't do this in general
(i.e., we should not be looking into all dependent
nested-name-specifiers as if they were the current instantiation), we
rely on the parser to tell us when it is parsing a declaration
specifier sequence, and, therefore, when we should consider the
current scope specifier to be a current instantiation.
Printing of complicated, dependent nested-name-specifiers may be
somewhat broken by this commit; I'll add tests for this issue and fix
the problem (if it still exists) in a subsequent commit.
llvm-svn: 80044
2009-08-26 06:51:20 +08:00
|
|
|
if (SS && SS->isSet()) {
|
2014-10-04 18:17:57 +08:00
|
|
|
NestedNameSpecifier *NNS = SS->getScopeRep();
|
|
|
|
if (NNS->getKind() == NestedNameSpecifier::Super)
|
|
|
|
return LookupInSuper(R, NNS->getAsRecordDecl());
|
|
|
|
|
Improve support for out-of-line definitions of nested templates and
their members, including member class template, member function
templates, and member classes and functions of member templates.
To actually parse the nested-name-specifiers that qualify the name of
an out-of-line definition of a member template, e.g.,
template<typename X> template<typename Y>
X Outer<X>::Inner1<Y>::foo(Y) {
return X();
}
we need to look for the template names (e.g., "Inner1") as a member of
the current instantiation (Outer<X>), even before we have entered the
scope of the current instantiation. Since we can't do this in general
(i.e., we should not be looking into all dependent
nested-name-specifiers as if they were the current instantiation), we
rely on the parser to tell us when it is parsing a declaration
specifier sequence, and, therefore, when we should consider the
current scope specifier to be a current instantiation.
Printing of complicated, dependent nested-name-specifiers may be
somewhat broken by this commit; I'll add tests for this issue and fix
the problem (if it still exists) in a subsequent commit.
llvm-svn: 80044
2009-08-26 06:51:20 +08:00
|
|
|
if (DeclContext *DC = computeDeclContext(*SS, EnteringContext)) {
|
2009-09-09 23:08:12 +08:00
|
|
|
// We have resolved the scope specifier to a particular declaration
|
Improve support for out-of-line definitions of nested templates and
their members, including member class template, member function
templates, and member classes and functions of member templates.
To actually parse the nested-name-specifiers that qualify the name of
an out-of-line definition of a member template, e.g.,
template<typename X> template<typename Y>
X Outer<X>::Inner1<Y>::foo(Y) {
return X();
}
we need to look for the template names (e.g., "Inner1") as a member of
the current instantiation (Outer<X>), even before we have entered the
scope of the current instantiation. Since we can't do this in general
(i.e., we should not be looking into all dependent
nested-name-specifiers as if they were the current instantiation), we
rely on the parser to tell us when it is parsing a declaration
specifier sequence, and, therefore, when we should consider the
current scope specifier to be a current instantiation.
Printing of complicated, dependent nested-name-specifiers may be
somewhat broken by this commit; I'll add tests for this issue and fix
the problem (if it still exists) in a subsequent commit.
llvm-svn: 80044
2009-08-26 06:51:20 +08:00
|
|
|
// contex, and will perform name lookup in that context.
|
2010-05-01 08:40:08 +08:00
|
|
|
if (!DC->isDependentContext() && RequireCompleteDeclContext(*SS, DC))
|
2009-10-10 05:13:30 +08:00
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-17 10:14:36 +08:00
|
|
|
R.setContextRange(SS->getRange());
|
|
|
|
return LookupQualifiedName(R, DC);
|
Introduce a representation for types that we referred to via a
qualified name, e.g.,
foo::x
so that we retain the nested-name-specifier as written in the source
code and can reproduce that qualified name when printing the types
back (e.g., in diagnostics). This is PR3493, which won't be complete
until finished the other tasks mentioned near the end of this commit.
The parser's representation of nested-name-specifiers, CXXScopeSpec,
is now a bit fatter, because it needs to contain the scopes that
precede each '::' and keep track of whether the global scoping
operator '::' was at the beginning. For example, we need to keep track
of the leading '::', 'foo', and 'bar' in
::foo::bar::x
The Action's CXXScopeTy * is no longer a DeclContext *. It's now the
opaque version of the new NestedNameSpecifier, which contains a single
component of a nested-name-specifier (either a DeclContext * or a Type
*, bitmangled).
The new sugar type QualifiedNameType composes a sequence of
NestedNameSpecifiers with a representation of the type we're actually
referring to. At present, we only build QualifiedNameType nodes within
Sema::getTypeName. This will be extended to other type-constructing
actions (e.g., ActOnClassTemplateId).
Also on the way: QualifiedDeclRefExprs will also store a sequence of
NestedNameSpecifiers, so that we can print out the property
nested-name-specifier. I expect to also use this for handling
dependent names like Fibonacci<I - 1>::value.
llvm-svn: 67265
2009-03-19 08:18:19 +08:00
|
|
|
}
|
2009-05-12 03:58:34 +08:00
|
|
|
|
Improve support for out-of-line definitions of nested templates and
their members, including member class template, member function
templates, and member classes and functions of member templates.
To actually parse the nested-name-specifiers that qualify the name of
an out-of-line definition of a member template, e.g.,
template<typename X> template<typename Y>
X Outer<X>::Inner1<Y>::foo(Y) {
return X();
}
we need to look for the template names (e.g., "Inner1") as a member of
the current instantiation (Outer<X>), even before we have entered the
scope of the current instantiation. Since we can't do this in general
(i.e., we should not be looking into all dependent
nested-name-specifiers as if they were the current instantiation), we
rely on the parser to tell us when it is parsing a declaration
specifier sequence, and, therefore, when we should consider the
current scope specifier to be a current instantiation.
Printing of complicated, dependent nested-name-specifiers may be
somewhat broken by this commit; I'll add tests for this issue and fix
the problem (if it still exists) in a subsequent commit.
llvm-svn: 80044
2009-08-26 06:51:20 +08:00
|
|
|
// We could not resolve the scope specified to a specific declaration
|
2009-09-09 23:08:12 +08:00
|
|
|
// context, which means that SS refers to an unknown specialization.
|
Improve support for out-of-line definitions of nested templates and
their members, including member class template, member function
templates, and member classes and functions of member templates.
To actually parse the nested-name-specifiers that qualify the name of
an out-of-line definition of a member template, e.g.,
template<typename X> template<typename Y>
X Outer<X>::Inner1<Y>::foo(Y) {
return X();
}
we need to look for the template names (e.g., "Inner1") as a member of
the current instantiation (Outer<X>), even before we have entered the
scope of the current instantiation. Since we can't do this in general
(i.e., we should not be looking into all dependent
nested-name-specifiers as if they were the current instantiation), we
rely on the parser to tell us when it is parsing a declaration
specifier sequence, and, therefore, when we should consider the
current scope specifier to be a current instantiation.
Printing of complicated, dependent nested-name-specifiers may be
somewhat broken by this commit; I'll add tests for this issue and fix
the problem (if it still exists) in a subsequent commit.
llvm-svn: 80044
2009-08-26 06:51:20 +08:00
|
|
|
// Name lookup can't find anything in this case.
|
2011-10-25 06:24:50 +08:00
|
|
|
R.setNotFoundInCurrentInstantiation();
|
|
|
|
R.setContextRange(SS->getRange());
|
2009-10-10 05:13:30 +08:00
|
|
|
return false;
|
Eliminated LookupCriteria, whose creation was causing a bottleneck for
LookupName et al. Instead, use an enum and a bool to describe its
contents.
Optimized the C/Objective-C path through LookupName, eliminating any
unnecessarily C++isms. Simplify IdentifierResolver::iterator, removing
some code and arguments that are no longer used.
Eliminated LookupDeclInScope/LookupDeclInContext, moving all callers
over to LookupName, LookupQualifiedName, or LookupParsedName, as
appropriate.
All together, I'm seeing a 0.2% speedup on Cocoa.h with PTH and
-disable-free. Plus, we're down to three name-lookup routines.
llvm-svn: 63354
2009-01-30 09:04:22 +08:00
|
|
|
}
|
2009-01-15 06:20:51 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
// Perform unqualified name lookup starting in the given scope.
|
2009-11-17 10:14:36 +08:00
|
|
|
return LookupName(R, S, AllowBuiltinCreation);
|
2009-01-15 06:20:51 +08:00
|
|
|
}
|
|
|
|
|
2014-09-26 08:28:20 +08:00
|
|
|
/// \brief Perform qualified name lookup into all base classes of the given
|
|
|
|
/// class.
|
|
|
|
///
|
|
|
|
/// \param R captures both the lookup criteria and any lookup results found.
|
|
|
|
///
|
|
|
|
/// \param Class The context in which qualified name lookup will
|
|
|
|
/// search. Name lookup will search in all base classes merging the results.
|
2014-10-04 18:17:57 +08:00
|
|
|
///
|
|
|
|
/// @returns True if any decls were found (but possibly ambiguous)
|
|
|
|
bool Sema::LookupInSuper(LookupResult &R, CXXRecordDecl *Class) {
|
2015-09-10 07:04:17 +08:00
|
|
|
// The access-control rules we use here are essentially the rules for
|
|
|
|
// doing a lookup in Class that just magically skipped the direct
|
|
|
|
// members of Class itself. That is, the naming class is Class, and the
|
|
|
|
// access includes the access of the base.
|
2014-09-26 08:28:20 +08:00
|
|
|
for (const auto &BaseSpec : Class->bases()) {
|
|
|
|
CXXRecordDecl *RD = cast<CXXRecordDecl>(
|
|
|
|
BaseSpec.getType()->castAs<RecordType>()->getDecl());
|
|
|
|
LookupResult Result(*this, R.getLookupNameInfo(), R.getLookupKind());
|
|
|
|
Result.setBaseObjectType(Context.getRecordType(Class));
|
|
|
|
LookupQualifiedName(Result, RD);
|
2015-09-10 07:04:17 +08:00
|
|
|
|
|
|
|
// Copy the lookup results into the target, merging the base's access into
|
|
|
|
// the path access.
|
|
|
|
for (auto I = Result.begin(), E = Result.end(); I != E; ++I) {
|
|
|
|
R.addDecl(I.getDecl(),
|
|
|
|
CXXRecordDecl::MergeAccess(BaseSpec.getAccessSpecifier(),
|
|
|
|
I.getAccess()));
|
|
|
|
}
|
|
|
|
|
|
|
|
Result.suppressDiagnostics();
|
2014-09-26 08:28:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
R.resolveKind();
|
2015-09-10 07:04:17 +08:00
|
|
|
R.setNamingClass(Class);
|
2014-10-04 18:17:57 +08:00
|
|
|
|
|
|
|
return !R.empty();
|
2014-09-26 08:28:20 +08:00
|
|
|
}
|
2009-02-04 03:21:40 +08:00
|
|
|
|
2012-06-22 18:16:05 +08:00
|
|
|
/// \brief Produce a diagnostic describing the ambiguity that resulted
|
2009-01-15 08:26:24 +08:00
|
|
|
/// from name lookup.
|
|
|
|
///
|
2012-06-22 18:16:05 +08:00
|
|
|
/// \param Result The result of the ambiguous lookup to be diagnosed.
|
2013-08-29 15:23:24 +08:00
|
|
|
void Sema::DiagnoseAmbiguousLookup(LookupResult &Result) {
|
2009-01-15 08:26:24 +08:00
|
|
|
assert(Result.isAmbiguous() && "Lookup result must be ambiguous");
|
|
|
|
|
2009-11-17 10:14:36 +08:00
|
|
|
DeclarationName Name = Result.getLookupName();
|
|
|
|
SourceLocation NameLoc = Result.getNameLoc();
|
|
|
|
SourceRange LookupRange = Result.getContextRange();
|
|
|
|
|
2009-10-10 13:48:19 +08:00
|
|
|
switch (Result.getAmbiguityKind()) {
|
|
|
|
case LookupResult::AmbiguousBaseSubobjects: {
|
|
|
|
CXXBasePaths *Paths = Result.getBasePaths();
|
|
|
|
QualType SubobjectType = Paths->front().back().Base->getType();
|
|
|
|
Diag(NameLoc, diag::err_ambiguous_member_multiple_subobjects)
|
|
|
|
<< Name << SubobjectType << getAmbiguousPathsDisplayString(*Paths)
|
|
|
|
<< LookupRange;
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2012-12-19 08:45:41 +08:00
|
|
|
DeclContext::lookup_iterator Found = Paths->front().Decls.begin();
|
2009-10-10 13:48:19 +08:00
|
|
|
while (isa<CXXMethodDecl>(*Found) &&
|
|
|
|
cast<CXXMethodDecl>(*Found)->isStatic())
|
|
|
|
++Found;
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2009-10-10 13:48:19 +08:00
|
|
|
Diag((*Found)->getLocation(), diag::note_ambiguous_member_found);
|
2013-08-29 15:23:24 +08:00
|
|
|
break;
|
2009-10-10 13:48:19 +08:00
|
|
|
}
|
2009-02-04 03:21:40 +08:00
|
|
|
|
2009-10-10 13:48:19 +08:00
|
|
|
case LookupResult::AmbiguousBaseSubobjectTypes: {
|
2009-02-04 03:21:40 +08:00
|
|
|
Diag(NameLoc, diag::err_ambiguous_member_multiple_subobject_types)
|
|
|
|
<< Name << LookupRange;
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2009-10-10 13:48:19 +08:00
|
|
|
CXXBasePaths *Paths = Result.getBasePaths();
|
2009-02-04 03:21:40 +08:00
|
|
|
std::set<Decl *> DeclsPrinted;
|
2009-10-10 13:48:19 +08:00
|
|
|
for (CXXBasePaths::paths_iterator Path = Paths->begin(),
|
|
|
|
PathEnd = Paths->end();
|
2009-02-04 03:21:40 +08:00
|
|
|
Path != PathEnd; ++Path) {
|
2012-12-19 08:45:41 +08:00
|
|
|
Decl *D = Path->Decls.front();
|
2009-02-04 03:21:40 +08:00
|
|
|
if (DeclsPrinted.insert(D).second)
|
|
|
|
Diag(D->getLocation(), diag::note_ambiguous_member_found);
|
|
|
|
}
|
2013-08-29 15:23:24 +08:00
|
|
|
break;
|
2009-01-16 08:38:09 +08:00
|
|
|
}
|
|
|
|
|
2009-10-10 13:48:19 +08:00
|
|
|
case LookupResult::AmbiguousTagHiding: {
|
|
|
|
Diag(NameLoc, diag::err_ambiguous_tag_hiding) << Name << LookupRange;
|
2009-10-10 05:13:30 +08:00
|
|
|
|
2015-10-19 04:32:12 +08:00
|
|
|
llvm::SmallPtrSet<NamedDecl*, 8> TagDecls;
|
2009-10-10 13:48:19 +08:00
|
|
|
|
2014-07-16 06:03:49 +08:00
|
|
|
for (auto *D : Result)
|
|
|
|
if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
|
2009-10-10 13:48:19 +08:00
|
|
|
TagDecls.insert(TD);
|
|
|
|
Diag(TD->getLocation(), diag::note_hidden_tag);
|
|
|
|
}
|
|
|
|
|
2014-07-16 06:03:49 +08:00
|
|
|
for (auto *D : Result)
|
|
|
|
if (!isa<TagDecl>(D))
|
|
|
|
Diag(D->getLocation(), diag::note_hiding_object);
|
2009-10-10 13:48:19 +08:00
|
|
|
|
|
|
|
// For recovery purposes, go ahead and implement the hiding.
|
2010-01-20 08:46:10 +08:00
|
|
|
LookupResult::Filter F = Result.makeFilter();
|
|
|
|
while (F.hasNext()) {
|
|
|
|
if (TagDecls.count(F.next()))
|
|
|
|
F.erase();
|
|
|
|
}
|
|
|
|
F.done();
|
2013-08-29 15:23:24 +08:00
|
|
|
break;
|
2009-10-10 13:48:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case LookupResult::AmbiguousReference: {
|
|
|
|
Diag(NameLoc, diag::err_ambiguous_reference) << Name << LookupRange;
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2014-07-16 06:03:49 +08:00
|
|
|
for (auto *D : Result)
|
|
|
|
Diag(D->getLocation(), diag::note_ambiguous_candidate) << D;
|
2013-08-29 15:23:24 +08:00
|
|
|
break;
|
2009-10-10 13:48:19 +08:00
|
|
|
}
|
2013-08-29 15:23:24 +08:00
|
|
|
}
|
2009-01-15 08:26:24 +08:00
|
|
|
}
|
2009-02-04 08:32:51 +08:00
|
|
|
|
2010-05-29 02:45:08 +08:00
|
|
|
namespace {
|
|
|
|
struct AssociatedLookup {
|
2012-08-25 04:38:34 +08:00
|
|
|
AssociatedLookup(Sema &S, SourceLocation InstantiationLoc,
|
2010-05-29 02:45:08 +08:00
|
|
|
Sema::AssociatedNamespaceSet &Namespaces,
|
|
|
|
Sema::AssociatedClassSet &Classes)
|
2012-08-25 04:38:34 +08:00
|
|
|
: S(S), Namespaces(Namespaces), Classes(Classes),
|
|
|
|
InstantiationLoc(InstantiationLoc) {
|
2010-05-29 02:45:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Sema &S;
|
|
|
|
Sema::AssociatedNamespaceSet &Namespaces;
|
|
|
|
Sema::AssociatedClassSet &Classes;
|
2012-08-25 04:38:34 +08:00
|
|
|
SourceLocation InstantiationLoc;
|
2010-05-29 02:45:08 +08:00
|
|
|
};
|
2015-10-07 07:40:43 +08:00
|
|
|
} // end anonymous namespace
|
2010-05-29 02:45:08 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
static void
|
2010-05-29 02:45:08 +08:00
|
|
|
addAssociatedClassesAndNamespaces(AssociatedLookup &Result, QualType T);
|
2009-08-08 06:18:02 +08:00
|
|
|
|
2010-04-30 15:08:38 +08:00
|
|
|
static void CollectEnclosingNamespace(Sema::AssociatedNamespaceSet &Namespaces,
|
|
|
|
DeclContext *Ctx) {
|
|
|
|
// Add the associated namespace for this class.
|
|
|
|
|
|
|
|
// We don't use DeclContext::getEnclosingNamespaceContext() as this may
|
|
|
|
// be a locally scoped record.
|
|
|
|
|
2010-09-01 04:53:31 +08:00
|
|
|
// We skip out of inline namespaces. The innermost non-inline namespace
|
|
|
|
// contains all names of all its nested inline namespaces anyway, so we can
|
|
|
|
// replace the entire inline namespace tree with its root.
|
|
|
|
while (Ctx->isRecord() || Ctx->isTransparentContext() ||
|
|
|
|
Ctx->isInlineNamespace())
|
2010-04-30 15:08:38 +08:00
|
|
|
Ctx = Ctx->getParent();
|
|
|
|
|
2009-08-08 06:18:02 +08:00
|
|
|
if (Ctx->isFileContext())
|
2010-04-30 15:08:38 +08:00
|
|
|
Namespaces.insert(Ctx->getPrimaryContext());
|
2009-08-08 06:18:02 +08:00
|
|
|
}
|
2009-07-08 15:51:57 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
// \brief Add the associated classes and namespaces for argument-dependent
|
2009-07-08 15:51:57 +08:00
|
|
|
// lookup that involves a template argument (C++ [basic.lookup.koenig]p2).
|
2009-09-09 23:08:12 +08:00
|
|
|
static void
|
2010-05-29 02:45:08 +08:00
|
|
|
addAssociatedClassesAndNamespaces(AssociatedLookup &Result,
|
|
|
|
const TemplateArgument &Arg) {
|
2009-07-08 15:51:57 +08:00
|
|
|
// C++ [basic.lookup.koenig]p2, last bullet:
|
2009-09-09 23:08:12 +08:00
|
|
|
// -- [...] ;
|
2009-07-08 15:51:57 +08:00
|
|
|
switch (Arg.getKind()) {
|
|
|
|
case TemplateArgument::Null:
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-08 15:51:57 +08:00
|
|
|
case TemplateArgument::Type:
|
|
|
|
// [...] the namespaces and classes associated with the types of the
|
|
|
|
// template arguments provided for template type parameters (excluding
|
|
|
|
// template template parameters)
|
2010-05-29 02:45:08 +08:00
|
|
|
addAssociatedClassesAndNamespaces(Result, Arg.getAsType());
|
2009-07-08 15:51:57 +08:00
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-01-27 15:10:08 +08:00
|
|
|
case TemplateArgument::Template:
|
2011-01-06 02:58:31 +08:00
|
|
|
case TemplateArgument::TemplateExpansion: {
|
2009-09-09 23:08:12 +08:00
|
|
|
// [...] the namespaces in which any template template arguments are
|
|
|
|
// defined; and the classes in which any member templates used as
|
2009-07-08 15:51:57 +08:00
|
|
|
// template template arguments are defined.
|
2011-01-06 02:58:31 +08:00
|
|
|
TemplateName Template = Arg.getAsTemplateOrTemplatePattern();
|
2009-09-09 23:08:12 +08:00
|
|
|
if (ClassTemplateDecl *ClassTemplate
|
2009-11-11 09:00:40 +08:00
|
|
|
= dyn_cast<ClassTemplateDecl>(Template.getAsTemplateDecl())) {
|
2009-07-08 15:51:57 +08:00
|
|
|
DeclContext *Ctx = ClassTemplate->getDeclContext();
|
|
|
|
if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx))
|
2010-05-29 02:45:08 +08:00
|
|
|
Result.Classes.insert(EnclosingClass);
|
2009-07-08 15:51:57 +08:00
|
|
|
// Add the associated namespace for this class.
|
2010-05-29 02:45:08 +08:00
|
|
|
CollectEnclosingNamespace(Result.Namespaces, Ctx);
|
2009-07-08 15:51:57 +08:00
|
|
|
}
|
|
|
|
break;
|
2009-11-11 09:00:40 +08:00
|
|
|
}
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2009-11-11 09:00:40 +08:00
|
|
|
case TemplateArgument::Declaration:
|
2009-07-08 15:51:57 +08:00
|
|
|
case TemplateArgument::Integral:
|
|
|
|
case TemplateArgument::Expression:
|
2012-09-26 10:36:12 +08:00
|
|
|
case TemplateArgument::NullPtr:
|
2009-09-09 23:08:12 +08:00
|
|
|
// [Note: non-type template arguments do not contribute to the set of
|
2009-07-08 15:51:57 +08:00
|
|
|
// associated namespaces. ]
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-08 15:51:57 +08:00
|
|
|
case TemplateArgument::Pack:
|
2014-07-16 05:32:31 +08:00
|
|
|
for (const auto &P : Arg.pack_elements())
|
|
|
|
addAssociatedClassesAndNamespaces(Result, P);
|
2009-07-08 15:51:57 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-04 08:32:51 +08:00
|
|
|
// \brief Add the associated classes and namespaces for
|
2009-09-09 23:08:12 +08:00
|
|
|
// argument-dependent lookup with an argument of class type
|
|
|
|
// (C++ [basic.lookup.koenig]p2).
|
|
|
|
static void
|
2010-05-29 02:45:08 +08:00
|
|
|
addAssociatedClassesAndNamespaces(AssociatedLookup &Result,
|
|
|
|
CXXRecordDecl *Class) {
|
|
|
|
|
|
|
|
// Just silently ignore anything whose name is __va_list_tag.
|
|
|
|
if (Class->getDeclName() == Result.S.VAListTagName)
|
|
|
|
return;
|
|
|
|
|
2009-02-04 08:32:51 +08:00
|
|
|
// C++ [basic.lookup.koenig]p2:
|
|
|
|
// [...]
|
|
|
|
// -- If T is a class type (including unions), its associated
|
|
|
|
// classes are: the class itself; the class of which it is a
|
|
|
|
// member, if any; and its direct and indirect base
|
|
|
|
// classes. Its associated namespaces are the namespaces in
|
2009-09-09 23:08:12 +08:00
|
|
|
// which its associated classes are defined.
|
2009-02-04 08:32:51 +08:00
|
|
|
|
|
|
|
// Add the class of which it is a member, if any.
|
|
|
|
DeclContext *Ctx = Class->getDeclContext();
|
|
|
|
if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx))
|
2010-05-29 02:45:08 +08:00
|
|
|
Result.Classes.insert(EnclosingClass);
|
2009-02-04 08:32:51 +08:00
|
|
|
// Add the associated namespace for this class.
|
2010-05-29 02:45:08 +08:00
|
|
|
CollectEnclosingNamespace(Result.Namespaces, Ctx);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-04 08:32:51 +08:00
|
|
|
// Add the class itself. If we've already seen this class, we don't
|
|
|
|
// need to visit base classes.
|
2014-03-15 06:07:27 +08:00
|
|
|
//
|
|
|
|
// FIXME: That's not correct, we may have added this class only because it
|
|
|
|
// was the enclosing class of another class, and in that case we won't have
|
|
|
|
// added its base classes yet.
|
2014-11-19 15:49:47 +08:00
|
|
|
if (!Result.Classes.insert(Class).second)
|
2009-02-04 08:32:51 +08:00
|
|
|
return;
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
// -- If T is a template-id, its associated namespaces and classes are
|
|
|
|
// the namespace in which the template is defined; for member
|
2011-01-27 15:09:49 +08:00
|
|
|
// templates, the member template's class; the namespaces and classes
|
2009-09-09 23:08:12 +08:00
|
|
|
// associated with the types of the template arguments provided for
|
2009-07-08 15:51:57 +08:00
|
|
|
// template type parameters (excluding template template parameters); the
|
2009-09-09 23:08:12 +08:00
|
|
|
// namespaces in which any template template arguments are defined; and
|
|
|
|
// the classes in which any member templates used as template template
|
|
|
|
// arguments are defined. [Note: non-type template arguments do not
|
2009-07-08 15:51:57 +08:00
|
|
|
// contribute to the set of associated namespaces. ]
|
2009-09-09 23:08:12 +08:00
|
|
|
if (ClassTemplateSpecializationDecl *Spec
|
2009-07-08 15:51:57 +08:00
|
|
|
= dyn_cast<ClassTemplateSpecializationDecl>(Class)) {
|
|
|
|
DeclContext *Ctx = Spec->getSpecializedTemplate()->getDeclContext();
|
|
|
|
if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx))
|
2010-05-29 02:45:08 +08:00
|
|
|
Result.Classes.insert(EnclosingClass);
|
2009-07-08 15:51:57 +08:00
|
|
|
// Add the associated namespace for this class.
|
2010-05-29 02:45:08 +08:00
|
|
|
CollectEnclosingNamespace(Result.Namespaces, Ctx);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-08 15:51:57 +08:00
|
|
|
const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
|
|
|
|
for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
|
2010-05-29 02:45:08 +08:00
|
|
|
addAssociatedClassesAndNamespaces(Result, TemplateArgs[I]);
|
2009-07-08 15:51:57 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-02-05 06:26:26 +08:00
|
|
|
// Only recurse into base classes for complete types.
|
2015-12-19 06:40:25 +08:00
|
|
|
if (!Result.S.isCompleteType(Result.InstantiationLoc,
|
|
|
|
Result.S.Context.getRecordType(Class)))
|
2014-03-15 06:07:27 +08:00
|
|
|
return;
|
2010-02-05 06:26:26 +08:00
|
|
|
|
2009-02-04 08:32:51 +08:00
|
|
|
// Add direct and indirect base classes along with their associated
|
|
|
|
// namespaces.
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<CXXRecordDecl *, 32> Bases;
|
2009-02-04 08:32:51 +08:00
|
|
|
Bases.push_back(Class);
|
|
|
|
while (!Bases.empty()) {
|
|
|
|
// Pop this class off the stack.
|
2013-08-24 00:11:15 +08:00
|
|
|
Class = Bases.pop_back_val();
|
2009-02-04 08:32:51 +08:00
|
|
|
|
|
|
|
// Visit the base classes.
|
2014-03-13 23:41:46 +08:00
|
|
|
for (const auto &Base : Class->bases()) {
|
|
|
|
const RecordType *BaseType = Base.getType()->getAs<RecordType>();
|
2009-10-25 17:35:33 +08:00
|
|
|
// In dependent contexts, we do ADL twice, and the first time around,
|
|
|
|
// the base type might be a dependent TemplateSpecializationType, or a
|
|
|
|
// TemplateTypeParmType. If that happens, simply ignore it.
|
|
|
|
// FIXME: If we want to support export, we probably need to add the
|
|
|
|
// namespace of the template in a TemplateSpecializationType, or even
|
|
|
|
// the classes and namespaces of known non-dependent arguments.
|
|
|
|
if (!BaseType)
|
|
|
|
continue;
|
2009-02-04 08:32:51 +08:00
|
|
|
CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(BaseType->getDecl());
|
2014-11-19 15:49:47 +08:00
|
|
|
if (Result.Classes.insert(BaseDecl).second) {
|
2009-02-04 08:32:51 +08:00
|
|
|
// Find the associated namespace for this base class.
|
|
|
|
DeclContext *BaseCtx = BaseDecl->getDeclContext();
|
2010-05-29 02:45:08 +08:00
|
|
|
CollectEnclosingNamespace(Result.Namespaces, BaseCtx);
|
2009-02-04 08:32:51 +08:00
|
|
|
|
|
|
|
// Make sure we visit the bases of this base class.
|
|
|
|
if (BaseDecl->bases_begin() != BaseDecl->bases_end())
|
|
|
|
Bases.push_back(BaseDecl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// \brief Add the associated classes and namespaces for
|
|
|
|
// argument-dependent lookup with an argument of type T
|
2009-09-09 23:08:12 +08:00
|
|
|
// (C++ [basic.lookup.koenig]p2).
|
|
|
|
static void
|
2010-05-29 02:45:08 +08:00
|
|
|
addAssociatedClassesAndNamespaces(AssociatedLookup &Result, QualType Ty) {
|
2009-02-04 08:32:51 +08:00
|
|
|
// C++ [basic.lookup.koenig]p2:
|
|
|
|
//
|
|
|
|
// For each argument type T in the function call, there is a set
|
|
|
|
// of zero or more associated namespaces and a set of zero or more
|
|
|
|
// associated classes to be considered. The sets of namespaces and
|
|
|
|
// classes is determined entirely by the types of the function
|
|
|
|
// arguments (and the namespace of any template template
|
|
|
|
// argument). Typedef names and using-declarations used to specify
|
|
|
|
// the types do not contribute to this set. The sets of namespaces
|
|
|
|
// and classes are determined in the following way:
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<const Type *, 16> Queue;
|
2010-05-28 14:08:54 +08:00
|
|
|
const Type *T = Ty->getCanonicalTypeInternal().getTypePtr();
|
|
|
|
|
2009-02-04 08:32:51 +08:00
|
|
|
while (true) {
|
2010-05-28 14:08:54 +08:00
|
|
|
switch (T->getTypeClass()) {
|
|
|
|
|
|
|
|
#define TYPE(Class, Base)
|
|
|
|
#define DEPENDENT_TYPE(Class, Base) case Type::Class:
|
|
|
|
#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
|
|
|
|
#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class:
|
|
|
|
#define ABSTRACT_TYPE(Class, Base)
|
|
|
|
#include "clang/AST/TypeNodes.def"
|
|
|
|
// T is canonical. We can also ignore dependent types because
|
|
|
|
// we don't need to do ADL at the definition point, but if we
|
|
|
|
// wanted to implement template export (or if we find some other
|
|
|
|
// use for associated classes and namespaces...) this would be
|
|
|
|
// wrong.
|
2009-02-04 08:32:51 +08:00
|
|
|
break;
|
|
|
|
|
2010-05-28 14:08:54 +08:00
|
|
|
// -- If T is a pointer to U or an array of U, its associated
|
|
|
|
// namespaces and classes are those associated with U.
|
|
|
|
case Type::Pointer:
|
|
|
|
T = cast<PointerType>(T)->getPointeeType().getTypePtr();
|
|
|
|
continue;
|
|
|
|
case Type::ConstantArray:
|
|
|
|
case Type::IncompleteArray:
|
|
|
|
case Type::VariableArray:
|
|
|
|
T = cast<ArrayType>(T)->getElementType().getTypePtr();
|
|
|
|
continue;
|
2009-02-04 08:32:51 +08:00
|
|
|
|
2010-05-28 14:08:54 +08:00
|
|
|
// -- If T is a fundamental type, its associated sets of
|
|
|
|
// namespaces and classes are both empty.
|
|
|
|
case Type::Builtin:
|
|
|
|
break;
|
|
|
|
|
|
|
|
// -- If T is a class type (including unions), its associated
|
|
|
|
// classes are: the class itself; the class of which it is a
|
|
|
|
// member, if any; and its direct and indirect base
|
|
|
|
// classes. Its associated namespaces are the namespaces in
|
|
|
|
// which its associated classes are defined.
|
|
|
|
case Type::Record: {
|
2015-12-19 06:19:11 +08:00
|
|
|
CXXRecordDecl *Class =
|
|
|
|
cast<CXXRecordDecl>(cast<RecordType>(T)->getDecl());
|
2010-05-29 02:45:08 +08:00
|
|
|
addAssociatedClassesAndNamespaces(Result, Class);
|
2010-05-28 14:08:54 +08:00
|
|
|
break;
|
2009-02-28 09:32:25 +08:00
|
|
|
}
|
2010-05-20 10:26:51 +08:00
|
|
|
|
2010-05-28 14:08:54 +08:00
|
|
|
// -- If T is an enumeration type, its associated namespace is
|
|
|
|
// the namespace in which it is defined. If it is class
|
2011-01-27 15:09:49 +08:00
|
|
|
// member, its associated class is the member's class; else
|
2010-05-28 14:08:54 +08:00
|
|
|
// it has no associated class.
|
|
|
|
case Type::Enum: {
|
|
|
|
EnumDecl *Enum = cast<EnumType>(T)->getDecl();
|
2009-02-04 08:32:51 +08:00
|
|
|
|
2010-05-28 14:08:54 +08:00
|
|
|
DeclContext *Ctx = Enum->getDeclContext();
|
|
|
|
if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx))
|
2010-05-29 02:45:08 +08:00
|
|
|
Result.Classes.insert(EnclosingClass);
|
2009-02-04 08:32:51 +08:00
|
|
|
|
2010-05-28 14:08:54 +08:00
|
|
|
// Add the associated namespace for this class.
|
2010-05-29 02:45:08 +08:00
|
|
|
CollectEnclosingNamespace(Result.Namespaces, Ctx);
|
2009-02-04 08:32:51 +08:00
|
|
|
|
2010-05-28 14:08:54 +08:00
|
|
|
break;
|
|
|
|
}
|
2009-02-04 08:32:51 +08:00
|
|
|
|
2010-05-28 14:08:54 +08:00
|
|
|
// -- If T is a function type, its associated namespaces and
|
|
|
|
// classes are those associated with the function parameter
|
|
|
|
// types and those associated with the return type.
|
|
|
|
case Type::FunctionProto: {
|
|
|
|
const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
|
2014-03-17 23:23:01 +08:00
|
|
|
for (const auto &Arg : Proto->param_types())
|
|
|
|
Queue.push_back(Arg.getTypePtr());
|
2010-05-28 14:08:54 +08:00
|
|
|
// fallthrough
|
|
|
|
}
|
|
|
|
case Type::FunctionNoProto: {
|
|
|
|
const FunctionType *FnType = cast<FunctionType>(T);
|
2014-01-26 00:55:45 +08:00
|
|
|
T = FnType->getReturnType().getTypePtr();
|
2010-05-28 14:08:54 +08:00
|
|
|
continue;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-05-28 14:08:54 +08:00
|
|
|
// -- If T is a pointer to a member function of a class X, its
|
|
|
|
// associated namespaces and classes are those associated
|
|
|
|
// with the function parameter types and return type,
|
|
|
|
// together with those associated with X.
|
|
|
|
//
|
|
|
|
// -- If T is a pointer to a data member of class X, its
|
|
|
|
// associated namespaces and classes are those associated
|
|
|
|
// with the member type together with those associated with
|
|
|
|
// X.
|
|
|
|
case Type::MemberPointer: {
|
|
|
|
const MemberPointerType *MemberPtr = cast<MemberPointerType>(T);
|
|
|
|
|
|
|
|
// Queue up the class type into which this points.
|
|
|
|
Queue.push_back(MemberPtr->getClass());
|
|
|
|
|
|
|
|
// And directly continue with the pointee type.
|
|
|
|
T = MemberPtr->getPointeeType().getTypePtr();
|
|
|
|
continue;
|
|
|
|
}
|
2009-02-04 08:32:51 +08:00
|
|
|
|
2010-05-28 14:08:54 +08:00
|
|
|
// As an extension, treat this like a normal pointer.
|
|
|
|
case Type::BlockPointer:
|
|
|
|
T = cast<BlockPointerType>(T)->getPointeeType().getTypePtr();
|
|
|
|
continue;
|
2009-02-04 08:32:51 +08:00
|
|
|
|
2010-05-28 14:08:54 +08:00
|
|
|
// References aren't covered by the standard, but that's such an
|
|
|
|
// obvious defect that we cover them anyway.
|
|
|
|
case Type::LValueReference:
|
|
|
|
case Type::RValueReference:
|
|
|
|
T = cast<ReferenceType>(T)->getPointeeType().getTypePtr();
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// These are fundamental types.
|
|
|
|
case Type::Vector:
|
|
|
|
case Type::ExtVector:
|
|
|
|
case Type::Complex:
|
|
|
|
break;
|
2009-02-04 08:32:51 +08:00
|
|
|
|
2013-04-30 21:56:41 +08:00
|
|
|
// Non-deduced auto types only get here for error cases.
|
|
|
|
case Type::Auto:
|
|
|
|
break;
|
|
|
|
|
2011-04-12 09:02:45 +08:00
|
|
|
// If T is an Objective-C object or interface type, or a pointer to an
|
|
|
|
// object or interface type, the associated namespace is the global
|
|
|
|
// namespace.
|
2010-05-28 14:08:54 +08:00
|
|
|
case Type::ObjCObject:
|
|
|
|
case Type::ObjCInterface:
|
|
|
|
case Type::ObjCObjectPointer:
|
2011-04-12 09:02:45 +08:00
|
|
|
Result.Namespaces.insert(Result.S.Context.getTranslationUnitDecl());
|
2010-05-28 14:08:54 +08:00
|
|
|
break;
|
2011-10-07 07:00:33 +08:00
|
|
|
|
|
|
|
// Atomic types are just wrappers; use the associations of the
|
|
|
|
// contained type.
|
|
|
|
case Type::Atomic:
|
|
|
|
T = cast<AtomicType>(T)->getValueType().getTypePtr();
|
|
|
|
continue;
|
2016-01-09 20:53:17 +08:00
|
|
|
case Type::Pipe:
|
|
|
|
T = cast<PipeType>(T)->getElementType().getTypePtr();
|
|
|
|
continue;
|
2010-05-28 14:08:54 +08:00
|
|
|
}
|
|
|
|
|
2013-08-24 00:11:15 +08:00
|
|
|
if (Queue.empty())
|
|
|
|
break;
|
|
|
|
T = Queue.pop_back_val();
|
2010-05-28 14:08:54 +08:00
|
|
|
}
|
2009-02-04 08:32:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Find the associated classes and namespaces for
|
|
|
|
/// argument-dependent lookup for a call with the given set of
|
|
|
|
/// arguments.
|
|
|
|
///
|
|
|
|
/// This routine computes the sets of associated classes and associated
|
2009-09-09 23:08:12 +08:00
|
|
|
/// namespaces searched by argument-dependent lookup
|
2009-02-04 08:32:51 +08:00
|
|
|
/// (C++ [basic.lookup.argdep]) for a given set of arguments.
|
2013-08-10 02:02:13 +08:00
|
|
|
void Sema::FindAssociatedClassesAndNamespaces(
|
|
|
|
SourceLocation InstantiationLoc, ArrayRef<Expr *> Args,
|
|
|
|
AssociatedNamespaceSet &AssociatedNamespaces,
|
|
|
|
AssociatedClassSet &AssociatedClasses) {
|
2009-02-04 08:32:51 +08:00
|
|
|
AssociatedNamespaces.clear();
|
|
|
|
AssociatedClasses.clear();
|
|
|
|
|
2012-08-25 04:38:34 +08:00
|
|
|
AssociatedLookup Result(*this, InstantiationLoc,
|
|
|
|
AssociatedNamespaces, AssociatedClasses);
|
2010-05-29 02:45:08 +08:00
|
|
|
|
2009-02-04 08:32:51 +08:00
|
|
|
// C++ [basic.lookup.koenig]p2:
|
|
|
|
// For each argument type T in the function call, there is a set
|
|
|
|
// of zero or more associated namespaces and a set of zero or more
|
|
|
|
// associated classes to be considered. The sets of namespaces and
|
|
|
|
// classes is determined entirely by the types of the function
|
|
|
|
// arguments (and the namespace of any template template
|
2009-09-09 23:08:12 +08:00
|
|
|
// argument).
|
2012-02-25 19:00:22 +08:00
|
|
|
for (unsigned ArgIdx = 0; ArgIdx != Args.size(); ++ArgIdx) {
|
2009-02-04 08:32:51 +08:00
|
|
|
Expr *Arg = Args[ArgIdx];
|
|
|
|
|
|
|
|
if (Arg->getType() != Context.OverloadTy) {
|
2010-05-29 02:45:08 +08:00
|
|
|
addAssociatedClassesAndNamespaces(Result, Arg->getType());
|
2009-02-04 08:32:51 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// [...] In addition, if the argument is the name or address of a
|
|
|
|
// set of overloaded functions and/or function templates, its
|
|
|
|
// associated classes and namespaces are the union of those
|
|
|
|
// associated with each of the members of the set: the namespace
|
|
|
|
// in which the function or function template is defined and the
|
|
|
|
// classes and namespaces associated with its (non-dependent)
|
|
|
|
// parameter types and return type.
|
2009-07-08 18:57:20 +08:00
|
|
|
Arg = Arg->IgnoreParens();
|
2009-11-21 16:51:07 +08:00
|
|
|
if (UnaryOperator *unaryOp = dyn_cast<UnaryOperator>(Arg))
|
2010-08-25 19:45:40 +08:00
|
|
|
if (unaryOp->getOpcode() == UO_AddrOf)
|
2009-11-21 16:51:07 +08:00
|
|
|
Arg = unaryOp->getSubExpr();
|
|
|
|
|
2010-05-29 02:45:08 +08:00
|
|
|
UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(Arg);
|
|
|
|
if (!ULE) continue;
|
2009-02-04 08:32:51 +08:00
|
|
|
|
2014-07-16 06:03:49 +08:00
|
|
|
for (const auto *D : ULE->decls()) {
|
2009-12-29 14:17:27 +08:00
|
|
|
// Look through any using declarations to find the underlying function.
|
2014-07-16 06:03:49 +08:00
|
|
|
const FunctionDecl *FDecl = D->getUnderlyingDecl()->getAsFunction();
|
2009-02-04 08:32:51 +08:00
|
|
|
|
|
|
|
// Add the classes and namespaces associated with the parameter
|
|
|
|
// types and return type of this function.
|
2010-05-29 02:45:08 +08:00
|
|
|
addAssociatedClassesAndNamespaces(Result, FDecl->getType());
|
2009-02-04 08:32:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-03-13 08:33:25 +08:00
|
|
|
|
2009-11-18 15:57:50 +08:00
|
|
|
NamedDecl *Sema::LookupSingleName(Scope *S, DeclarationName Name,
|
2010-04-16 06:33:43 +08:00
|
|
|
SourceLocation Loc,
|
2009-11-18 15:57:50 +08:00
|
|
|
LookupNameKind NameKind,
|
|
|
|
RedeclarationKind Redecl) {
|
2010-04-16 06:33:43 +08:00
|
|
|
LookupResult R(*this, Name, Loc, NameKind, Redecl);
|
2009-11-18 15:57:50 +08:00
|
|
|
LookupName(R, S);
|
2009-12-02 16:25:40 +08:00
|
|
|
return R.getAsSingle<NamedDecl>();
|
2009-11-18 15:57:50 +08:00
|
|
|
}
|
|
|
|
|
2009-04-24 07:18:26 +08:00
|
|
|
/// \brief Find the protocol with the given name, if any.
|
2011-01-27 15:10:08 +08:00
|
|
|
ObjCProtocolDecl *Sema::LookupProtocol(IdentifierInfo *II,
|
2012-01-02 04:30:41 +08:00
|
|
|
SourceLocation IdLoc,
|
|
|
|
RedeclarationKind Redecl) {
|
2010-04-16 06:33:43 +08:00
|
|
|
Decl *D = LookupSingleName(TUScope, II, IdLoc,
|
2012-01-02 04:30:41 +08:00
|
|
|
LookupObjCProtocolName, Redecl);
|
2009-04-24 07:18:26 +08:00
|
|
|
return cast_or_null<ObjCProtocolDecl>(D);
|
|
|
|
}
|
|
|
|
|
2009-03-13 08:33:25 +08:00
|
|
|
void Sema::LookupOverloadedOperatorName(OverloadedOperatorKind Op, Scope *S,
|
2009-09-09 23:08:12 +08:00
|
|
|
QualType T1, QualType T2,
|
2010-01-26 11:27:55 +08:00
|
|
|
UnresolvedSetImpl &Functions) {
|
2009-03-13 08:33:25 +08:00
|
|
|
// C++ [over.match.oper]p3:
|
|
|
|
// -- The set of non-member candidates is the result of the
|
|
|
|
// unqualified lookup of operator@ in the context of the
|
|
|
|
// expression according to the usual rules for name lookup in
|
|
|
|
// unqualified function calls (3.4.2) except that all member
|
2014-04-17 09:52:14 +08:00
|
|
|
// functions are ignored.
|
2009-03-13 08:33:25 +08:00
|
|
|
DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op);
|
2009-11-17 10:14:36 +08:00
|
|
|
LookupResult Operators(*this, OpName, SourceLocation(), LookupOperatorName);
|
|
|
|
LookupName(Operators, S);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-13 08:33:25 +08:00
|
|
|
assert(!Operators.isAmbiguous() && "Operator lookup cannot be ambiguous");
|
2014-04-17 09:52:14 +08:00
|
|
|
Functions.append(Operators.begin(), Operators.end());
|
2009-03-13 08:33:25 +08:00
|
|
|
}
|
|
|
|
|
2011-06-24 10:11:39 +08:00
|
|
|
Sema::SpecialMemberOverloadResult *Sema::LookupSpecialMember(CXXRecordDecl *RD,
|
2011-06-04 12:32:43 +08:00
|
|
|
CXXSpecialMember SM,
|
|
|
|
bool ConstArg,
|
|
|
|
bool VolatileArg,
|
|
|
|
bool RValueThis,
|
|
|
|
bool ConstThis,
|
|
|
|
bool VolatileThis) {
|
2012-11-28 05:20:31 +08:00
|
|
|
assert(CanDeclareSpecialMemberFunction(RD) &&
|
2011-06-04 12:32:43 +08:00
|
|
|
"doing special member lookup into record that isn't fully complete");
|
2012-11-28 05:20:31 +08:00
|
|
|
RD = RD->getDefinition();
|
2011-06-04 12:32:43 +08:00
|
|
|
if (RValueThis || ConstThis || VolatileThis)
|
|
|
|
assert((SM == CXXCopyAssignment || SM == CXXMoveAssignment) &&
|
|
|
|
"constructors and destructors always have unqualified lvalue this");
|
|
|
|
if (ConstArg || VolatileArg)
|
|
|
|
assert((SM != CXXDefaultConstructor && SM != CXXDestructor) &&
|
|
|
|
"parameter-less special members can't have qualified arguments");
|
|
|
|
|
|
|
|
llvm::FoldingSetNodeID ID;
|
2011-06-24 10:11:39 +08:00
|
|
|
ID.AddPointer(RD);
|
2011-06-04 12:32:43 +08:00
|
|
|
ID.AddInteger(SM);
|
|
|
|
ID.AddInteger(ConstArg);
|
|
|
|
ID.AddInteger(VolatileArg);
|
|
|
|
ID.AddInteger(RValueThis);
|
|
|
|
ID.AddInteger(ConstThis);
|
|
|
|
ID.AddInteger(VolatileThis);
|
|
|
|
|
|
|
|
void *InsertPoint;
|
|
|
|
SpecialMemberOverloadResult *Result =
|
|
|
|
SpecialMemberCache.FindNodeOrInsertPos(ID, InsertPoint);
|
|
|
|
|
|
|
|
// This was already cached
|
|
|
|
if (Result)
|
|
|
|
return Result;
|
|
|
|
|
2011-06-07 08:11:58 +08:00
|
|
|
Result = BumpAlloc.Allocate<SpecialMemberOverloadResult>();
|
|
|
|
Result = new (Result) SpecialMemberOverloadResult(ID);
|
2011-06-04 12:32:43 +08:00
|
|
|
SpecialMemberCache.InsertNode(Result, InsertPoint);
|
|
|
|
|
|
|
|
if (SM == CXXDestructor) {
|
2012-12-01 10:35:44 +08:00
|
|
|
if (RD->needsImplicitDestructor())
|
2011-06-24 10:11:39 +08:00
|
|
|
DeclareImplicitDestructor(RD);
|
|
|
|
CXXDestructorDecl *DD = RD->getDestructor();
|
2011-06-04 12:32:43 +08:00
|
|
|
assert(DD && "record without a destructor");
|
|
|
|
Result->setMethod(DD);
|
2012-03-31 04:53:28 +08:00
|
|
|
Result->setKind(DD->isDeleted() ?
|
|
|
|
SpecialMemberOverloadResult::NoMemberOrDeleted :
|
2012-04-21 02:46:14 +08:00
|
|
|
SpecialMemberOverloadResult::Success);
|
2011-06-04 12:32:43 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2011-06-10 11:50:41 +08:00
|
|
|
// Prepare for overload resolution. Here we construct a synthetic argument
|
|
|
|
// if necessary and make sure that implicit functions are declared.
|
2011-06-24 10:11:39 +08:00
|
|
|
CanQualType CanTy = Context.getCanonicalType(Context.getTagDeclType(RD));
|
2011-06-10 11:50:41 +08:00
|
|
|
DeclarationName Name;
|
2014-05-26 14:22:03 +08:00
|
|
|
Expr *Arg = nullptr;
|
2011-06-10 11:50:41 +08:00
|
|
|
unsigned NumArgs;
|
|
|
|
|
2012-04-21 02:46:14 +08:00
|
|
|
QualType ArgType = CanTy;
|
|
|
|
ExprValueKind VK = VK_LValue;
|
|
|
|
|
2011-06-10 11:50:41 +08:00
|
|
|
if (SM == CXXDefaultConstructor) {
|
|
|
|
Name = Context.DeclarationNames.getCXXConstructorName(CanTy);
|
|
|
|
NumArgs = 0;
|
2011-06-24 10:11:39 +08:00
|
|
|
if (RD->needsImplicitDefaultConstructor())
|
|
|
|
DeclareImplicitDefaultConstructor(RD);
|
2011-06-10 11:50:41 +08:00
|
|
|
} else {
|
|
|
|
if (SM == CXXCopyConstructor || SM == CXXMoveConstructor) {
|
|
|
|
Name = Context.DeclarationNames.getCXXConstructorName(CanTy);
|
2012-12-01 10:35:44 +08:00
|
|
|
if (RD->needsImplicitCopyConstructor())
|
2011-06-24 10:11:39 +08:00
|
|
|
DeclareImplicitCopyConstructor(RD);
|
2013-01-02 19:42:31 +08:00
|
|
|
if (getLangOpts().CPlusPlus11 && RD->needsImplicitMoveConstructor())
|
2011-08-31 03:58:05 +08:00
|
|
|
DeclareImplicitMoveConstructor(RD);
|
2011-06-10 11:50:41 +08:00
|
|
|
} else {
|
|
|
|
Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal);
|
2012-12-01 10:35:44 +08:00
|
|
|
if (RD->needsImplicitCopyAssignment())
|
2011-06-24 10:11:39 +08:00
|
|
|
DeclareImplicitCopyAssignment(RD);
|
2013-01-02 19:42:31 +08:00
|
|
|
if (getLangOpts().CPlusPlus11 && RD->needsImplicitMoveAssignment())
|
2011-08-31 03:58:05 +08:00
|
|
|
DeclareImplicitMoveAssignment(RD);
|
2011-06-10 11:50:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ConstArg)
|
|
|
|
ArgType.addConst();
|
|
|
|
if (VolatileArg)
|
|
|
|
ArgType.addVolatile();
|
|
|
|
|
|
|
|
// This isn't /really/ specified by the standard, but it's implied
|
|
|
|
// we should be working from an RValue in the case of move to ensure
|
|
|
|
// that we prefer to bind to rvalue references, and an LValue in the
|
|
|
|
// case of copy to ensure we don't bind to rvalue references.
|
|
|
|
// Possibly an XValue is actually correct in the case of move, but
|
|
|
|
// there is no semantic difference for class types in this restricted
|
|
|
|
// case.
|
2011-06-23 06:13:13 +08:00
|
|
|
if (SM == CXXCopyConstructor || SM == CXXCopyAssignment)
|
2011-06-10 11:50:41 +08:00
|
|
|
VK = VK_LValue;
|
|
|
|
else
|
|
|
|
VK = VK_RValue;
|
2012-04-21 02:46:14 +08:00
|
|
|
}
|
2011-06-10 11:50:41 +08:00
|
|
|
|
2012-04-21 02:46:14 +08:00
|
|
|
OpaqueValueExpr FakeArg(SourceLocation(), ArgType, VK);
|
|
|
|
|
|
|
|
if (SM != CXXDefaultConstructor) {
|
2011-06-10 11:50:41 +08:00
|
|
|
NumArgs = 1;
|
2012-04-21 02:46:14 +08:00
|
|
|
Arg = &FakeArg;
|
2011-06-10 11:50:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the object argument
|
|
|
|
QualType ThisTy = CanTy;
|
|
|
|
if (ConstThis)
|
|
|
|
ThisTy.addConst();
|
|
|
|
if (VolatileThis)
|
|
|
|
ThisTy.addVolatile();
|
2011-06-23 08:26:20 +08:00
|
|
|
Expr::Classification Classification =
|
2012-04-21 02:46:14 +08:00
|
|
|
OpaqueValueExpr(SourceLocation(), ThisTy,
|
|
|
|
RValueThis ? VK_RValue : VK_LValue).Classify(Context);
|
2011-06-10 11:50:41 +08:00
|
|
|
|
|
|
|
// Now we perform lookup on the name we computed earlier and do overload
|
|
|
|
// resolution. Lookup is only performed directly into the class since there
|
|
|
|
// will always be a (possibly implicit) declaration to shadow any others.
|
2014-04-17 09:52:14 +08:00
|
|
|
OverloadCandidateSet OCS(RD->getLocation(), OverloadCandidateSet::CSK_Normal);
|
2012-12-19 08:45:41 +08:00
|
|
|
DeclContext::lookup_result R = RD->lookup(Name);
|
2015-02-11 09:48:47 +08:00
|
|
|
|
|
|
|
if (R.empty()) {
|
|
|
|
// We might have no default constructor because we have a lambda's closure
|
|
|
|
// type, rather than because there's some other declared constructor.
|
|
|
|
// Every class has a copy/move constructor, copy/move assignment, and
|
|
|
|
// destructor.
|
|
|
|
assert(SM == CXXDefaultConstructor &&
|
|
|
|
"lookup for a constructor or assignment operator was empty");
|
|
|
|
Result->setMethod(nullptr);
|
|
|
|
Result->setKind(SpecialMemberOverloadResult::NoMemberOrDeleted);
|
|
|
|
return Result;
|
|
|
|
}
|
2013-08-18 15:20:52 +08:00
|
|
|
|
|
|
|
// Copy the candidates as our processing of them may load new declarations
|
|
|
|
// from an external source and invalidate lookup_result.
|
|
|
|
SmallVector<NamedDecl *, 8> Candidates(R.begin(), R.end());
|
|
|
|
|
2014-07-16 06:03:49 +08:00
|
|
|
for (auto *Cand : Candidates) {
|
2011-06-24 10:11:39 +08:00
|
|
|
if (Cand->isInvalidDecl())
|
2011-06-10 11:50:41 +08:00
|
|
|
continue;
|
|
|
|
|
2011-06-24 10:11:39 +08:00
|
|
|
if (UsingShadowDecl *U = dyn_cast<UsingShadowDecl>(Cand)) {
|
|
|
|
// FIXME: [namespace.udecl]p15 says that we should only consider a
|
|
|
|
// using declaration here if it does not match a declaration in the
|
|
|
|
// derived class. We do not implement this correctly in other cases
|
|
|
|
// either.
|
|
|
|
Cand = U->getTargetDecl();
|
|
|
|
|
|
|
|
if (Cand->isInvalidDecl())
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CXXMethodDecl *M = dyn_cast<CXXMethodDecl>(Cand)) {
|
2011-06-23 08:26:20 +08:00
|
|
|
if (SM == CXXCopyAssignment || SM == CXXMoveAssignment)
|
2011-06-24 10:11:39 +08:00
|
|
|
AddMethodCandidate(M, DeclAccessPair::make(M, AS_public), RD, ThisTy,
|
2012-02-25 19:00:22 +08:00
|
|
|
Classification, llvm::makeArrayRef(&Arg, NumArgs),
|
|
|
|
OCS, true);
|
2011-06-23 08:26:20 +08:00
|
|
|
else
|
2012-02-25 19:00:22 +08:00
|
|
|
AddOverloadCandidate(M, DeclAccessPair::make(M, AS_public),
|
|
|
|
llvm::makeArrayRef(&Arg, NumArgs), OCS, true);
|
2011-06-22 10:58:46 +08:00
|
|
|
} else if (FunctionTemplateDecl *Tmpl =
|
2011-06-24 10:11:39 +08:00
|
|
|
dyn_cast<FunctionTemplateDecl>(Cand)) {
|
2011-06-23 08:26:20 +08:00
|
|
|
if (SM == CXXCopyAssignment || SM == CXXMoveAssignment)
|
|
|
|
AddMethodTemplateCandidate(Tmpl, DeclAccessPair::make(Tmpl, AS_public),
|
2014-05-26 14:22:03 +08:00
|
|
|
RD, nullptr, ThisTy, Classification,
|
2012-02-25 19:00:22 +08:00
|
|
|
llvm::makeArrayRef(&Arg, NumArgs),
|
2011-06-23 08:26:20 +08:00
|
|
|
OCS, true);
|
|
|
|
else
|
|
|
|
AddTemplateOverloadCandidate(Tmpl, DeclAccessPair::make(Tmpl, AS_public),
|
2014-05-26 14:22:03 +08:00
|
|
|
nullptr, llvm::makeArrayRef(&Arg, NumArgs),
|
2012-02-25 19:00:22 +08:00
|
|
|
OCS, true);
|
2011-06-24 10:11:39 +08:00
|
|
|
} else {
|
|
|
|
assert(isa<UsingDecl>(Cand) && "illegal Kind of operator = Decl");
|
2011-06-10 11:50:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OverloadCandidateSet::iterator Best;
|
|
|
|
switch (OCS.BestViableFunction(*this, SourceLocation(), Best)) {
|
|
|
|
case OR_Success:
|
|
|
|
Result->setMethod(cast<CXXMethodDecl>(Best->Function));
|
2012-04-21 02:46:14 +08:00
|
|
|
Result->setKind(SpecialMemberOverloadResult::Success);
|
2011-06-10 11:50:41 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OR_Deleted:
|
|
|
|
Result->setMethod(cast<CXXMethodDecl>(Best->Function));
|
2012-03-31 04:53:28 +08:00
|
|
|
Result->setKind(SpecialMemberOverloadResult::NoMemberOrDeleted);
|
2011-06-10 11:50:41 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OR_Ambiguous:
|
2014-05-26 14:22:03 +08:00
|
|
|
Result->setMethod(nullptr);
|
2012-03-31 04:53:28 +08:00
|
|
|
Result->setKind(SpecialMemberOverloadResult::Ambiguous);
|
|
|
|
break;
|
|
|
|
|
2011-06-10 11:50:41 +08:00
|
|
|
case OR_No_Viable_Function:
|
2014-05-26 14:22:03 +08:00
|
|
|
Result->setMethod(nullptr);
|
2012-03-31 04:53:28 +08:00
|
|
|
Result->setKind(SpecialMemberOverloadResult::NoMemberOrDeleted);
|
2011-06-10 11:50:41 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Look up the default constructor for the given class.
|
|
|
|
CXXConstructorDecl *Sema::LookupDefaultConstructor(CXXRecordDecl *Class) {
|
2011-06-10 12:44:37 +08:00
|
|
|
SpecialMemberOverloadResult *Result =
|
2011-06-10 11:50:41 +08:00
|
|
|
LookupSpecialMember(Class, CXXDefaultConstructor, false, false, false,
|
|
|
|
false, false);
|
|
|
|
|
|
|
|
return cast_or_null<CXXConstructorDecl>(Result->getMethod());
|
2011-06-10 12:44:37 +08:00
|
|
|
}
|
|
|
|
|
2011-06-22 07:42:56 +08:00
|
|
|
/// \brief Look up the copying constructor for the given class.
|
|
|
|
CXXConstructorDecl *Sema::LookupCopyingConstructor(CXXRecordDecl *Class,
|
2012-04-21 02:46:14 +08:00
|
|
|
unsigned Quals) {
|
2011-06-10 12:44:37 +08:00
|
|
|
assert(!(Quals & ~(Qualifiers::Const | Qualifiers::Volatile)) &&
|
|
|
|
"non-const, non-volatile qualifiers for copy ctor arg");
|
|
|
|
SpecialMemberOverloadResult *Result =
|
|
|
|
LookupSpecialMember(Class, CXXCopyConstructor, Quals & Qualifiers::Const,
|
|
|
|
Quals & Qualifiers::Volatile, false, false, false);
|
|
|
|
|
|
|
|
return cast_or_null<CXXConstructorDecl>(Result->getMethod());
|
2011-06-04 12:32:43 +08:00
|
|
|
}
|
|
|
|
|
2011-08-31 03:58:05 +08:00
|
|
|
/// \brief Look up the moving constructor for the given class.
|
2012-07-18 11:36:00 +08:00
|
|
|
CXXConstructorDecl *Sema::LookupMovingConstructor(CXXRecordDecl *Class,
|
|
|
|
unsigned Quals) {
|
2011-08-31 03:58:05 +08:00
|
|
|
SpecialMemberOverloadResult *Result =
|
2012-07-18 11:36:00 +08:00
|
|
|
LookupSpecialMember(Class, CXXMoveConstructor, Quals & Qualifiers::Const,
|
|
|
|
Quals & Qualifiers::Volatile, false, false, false);
|
2011-08-31 03:58:05 +08:00
|
|
|
|
|
|
|
return cast_or_null<CXXConstructorDecl>(Result->getMethod());
|
|
|
|
}
|
|
|
|
|
2010-07-03 07:12:18 +08:00
|
|
|
/// \brief Look up the constructors for the given class.
|
|
|
|
DeclContext::lookup_result Sema::LookupConstructors(CXXRecordDecl *Class) {
|
2011-06-10 11:50:41 +08:00
|
|
|
// If the implicit constructors have not yet been declared, do so now.
|
2012-11-28 05:20:31 +08:00
|
|
|
if (CanDeclareSpecialMemberFunction(Class)) {
|
2011-05-12 06:34:38 +08:00
|
|
|
if (Class->needsImplicitDefaultConstructor())
|
2010-07-03 08:47:00 +08:00
|
|
|
DeclareImplicitDefaultConstructor(Class);
|
2012-12-01 10:35:44 +08:00
|
|
|
if (Class->needsImplicitCopyConstructor())
|
2010-07-03 08:47:00 +08:00
|
|
|
DeclareImplicitCopyConstructor(Class);
|
2013-01-02 19:42:31 +08:00
|
|
|
if (getLangOpts().CPlusPlus11 && Class->needsImplicitMoveConstructor())
|
2011-08-31 03:58:05 +08:00
|
|
|
DeclareImplicitMoveConstructor(Class);
|
2010-07-03 08:47:00 +08:00
|
|
|
}
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2010-07-03 07:12:18 +08:00
|
|
|
CanQualType T = Context.getCanonicalType(Context.getTypeDeclType(Class));
|
|
|
|
DeclarationName Name = Context.DeclarationNames.getCXXConstructorName(T);
|
|
|
|
return Class->lookup(Name);
|
|
|
|
}
|
|
|
|
|
2011-06-22 07:42:56 +08:00
|
|
|
/// \brief Look up the copying assignment operator for the given class.
|
|
|
|
CXXMethodDecl *Sema::LookupCopyingAssignment(CXXRecordDecl *Class,
|
|
|
|
unsigned Quals, bool RValueThis,
|
2012-04-21 02:46:14 +08:00
|
|
|
unsigned ThisQuals) {
|
2011-06-22 07:42:56 +08:00
|
|
|
assert(!(Quals & ~(Qualifiers::Const | Qualifiers::Volatile)) &&
|
|
|
|
"non-const, non-volatile qualifiers for copy assignment arg");
|
|
|
|
assert(!(ThisQuals & ~(Qualifiers::Const | Qualifiers::Volatile)) &&
|
|
|
|
"non-const, non-volatile qualifiers for copy assignment this");
|
|
|
|
SpecialMemberOverloadResult *Result =
|
|
|
|
LookupSpecialMember(Class, CXXCopyAssignment, Quals & Qualifiers::Const,
|
|
|
|
Quals & Qualifiers::Volatile, RValueThis,
|
|
|
|
ThisQuals & Qualifiers::Const,
|
|
|
|
ThisQuals & Qualifiers::Volatile);
|
|
|
|
|
|
|
|
return Result->getMethod();
|
|
|
|
}
|
|
|
|
|
2011-08-31 03:58:05 +08:00
|
|
|
/// \brief Look up the moving assignment operator for the given class.
|
|
|
|
CXXMethodDecl *Sema::LookupMovingAssignment(CXXRecordDecl *Class,
|
2012-07-18 11:36:00 +08:00
|
|
|
unsigned Quals,
|
2011-08-31 03:58:05 +08:00
|
|
|
bool RValueThis,
|
|
|
|
unsigned ThisQuals) {
|
|
|
|
assert(!(ThisQuals & ~(Qualifiers::Const | Qualifiers::Volatile)) &&
|
|
|
|
"non-const, non-volatile qualifiers for copy assignment this");
|
|
|
|
SpecialMemberOverloadResult *Result =
|
2012-07-18 11:36:00 +08:00
|
|
|
LookupSpecialMember(Class, CXXMoveAssignment, Quals & Qualifiers::Const,
|
|
|
|
Quals & Qualifiers::Volatile, RValueThis,
|
2011-08-31 03:58:05 +08:00
|
|
|
ThisQuals & Qualifiers::Const,
|
|
|
|
ThisQuals & Qualifiers::Volatile);
|
|
|
|
|
|
|
|
return Result->getMethod();
|
|
|
|
}
|
|
|
|
|
2010-07-02 06:47:18 +08:00
|
|
|
/// \brief Look for the destructor of the given class.
|
|
|
|
///
|
2011-06-04 05:10:40 +08:00
|
|
|
/// During semantic analysis, this routine should be used in lieu of
|
|
|
|
/// CXXRecordDecl::getDestructor().
|
2010-07-02 06:47:18 +08:00
|
|
|
///
|
|
|
|
/// \returns The destructor for this class.
|
|
|
|
CXXDestructorDecl *Sema::LookupDestructor(CXXRecordDecl *Class) {
|
2011-06-04 12:32:43 +08:00
|
|
|
return cast<CXXDestructorDecl>(LookupSpecialMember(Class, CXXDestructor,
|
|
|
|
false, false, false,
|
|
|
|
false, false)->getMethod());
|
2010-07-02 06:47:18 +08:00
|
|
|
}
|
|
|
|
|
2012-03-09 16:00:36 +08:00
|
|
|
/// LookupLiteralOperator - Determine which literal operator should be used for
|
|
|
|
/// a user-defined literal, per C++11 [lex.ext].
|
|
|
|
///
|
|
|
|
/// Normal overload resolution is not used to select which literal operator to
|
|
|
|
/// call for a user-defined literal. Look up the provided literal operator name,
|
|
|
|
/// and filter the results to the appropriate set for the given argument types.
|
|
|
|
Sema::LiteralOperatorLookupResult
|
|
|
|
Sema::LookupLiteralOperator(Scope *S, LookupResult &R,
|
|
|
|
ArrayRef<QualType> ArgTys,
|
2013-10-08 03:57:58 +08:00
|
|
|
bool AllowRaw, bool AllowTemplate,
|
|
|
|
bool AllowStringTemplate) {
|
2012-03-09 16:00:36 +08:00
|
|
|
LookupName(R, S);
|
|
|
|
assert(R.getResultKind() != LookupResult::Ambiguous &&
|
|
|
|
"literal operator lookup can't be ambiguous");
|
|
|
|
|
|
|
|
// Filter the lookup results appropriately.
|
|
|
|
LookupResult::Filter F = R.makeFilter();
|
|
|
|
|
|
|
|
bool FoundRaw = false;
|
2013-10-08 03:57:58 +08:00
|
|
|
bool FoundTemplate = false;
|
|
|
|
bool FoundStringTemplate = false;
|
2012-03-09 16:00:36 +08:00
|
|
|
bool FoundExactMatch = false;
|
|
|
|
|
|
|
|
while (F.hasNext()) {
|
|
|
|
Decl *D = F.next();
|
|
|
|
if (UsingShadowDecl *USD = dyn_cast<UsingShadowDecl>(D))
|
|
|
|
D = USD->getTargetDecl();
|
|
|
|
|
2013-04-10 13:18:00 +08:00
|
|
|
// If the declaration we found is invalid, skip it.
|
|
|
|
if (D->isInvalidDecl()) {
|
|
|
|
F.erase();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-10-08 03:57:58 +08:00
|
|
|
bool IsRaw = false;
|
|
|
|
bool IsTemplate = false;
|
|
|
|
bool IsStringTemplate = false;
|
|
|
|
bool IsExactMatch = false;
|
|
|
|
|
2012-03-09 16:00:36 +08:00
|
|
|
if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
|
|
|
|
if (FD->getNumParams() == 1 &&
|
|
|
|
FD->getParamDecl(0)->getType()->getAs<PointerType>())
|
|
|
|
IsRaw = true;
|
2013-01-15 15:12:59 +08:00
|
|
|
else if (FD->getNumParams() == ArgTys.size()) {
|
2012-03-09 16:00:36 +08:00
|
|
|
IsExactMatch = true;
|
|
|
|
for (unsigned ArgIdx = 0; ArgIdx != ArgTys.size(); ++ArgIdx) {
|
|
|
|
QualType ParamTy = FD->getParamDecl(ArgIdx)->getType();
|
|
|
|
if (!Context.hasSameUnqualifiedType(ArgTys[ArgIdx], ParamTy)) {
|
|
|
|
IsExactMatch = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-08 03:57:58 +08:00
|
|
|
if (FunctionTemplateDecl *FD = dyn_cast<FunctionTemplateDecl>(D)) {
|
|
|
|
TemplateParameterList *Params = FD->getTemplateParameters();
|
|
|
|
if (Params->size() == 1)
|
|
|
|
IsTemplate = true;
|
|
|
|
else
|
|
|
|
IsStringTemplate = true;
|
|
|
|
}
|
2012-03-09 16:00:36 +08:00
|
|
|
|
|
|
|
if (IsExactMatch) {
|
|
|
|
FoundExactMatch = true;
|
2013-10-08 03:57:58 +08:00
|
|
|
AllowRaw = false;
|
|
|
|
AllowTemplate = false;
|
|
|
|
AllowStringTemplate = false;
|
|
|
|
if (FoundRaw || FoundTemplate || FoundStringTemplate) {
|
2012-03-09 16:00:36 +08:00
|
|
|
// Go through again and remove the raw and template decls we've
|
|
|
|
// already found.
|
|
|
|
F.restart();
|
2013-10-08 03:57:58 +08:00
|
|
|
FoundRaw = FoundTemplate = FoundStringTemplate = false;
|
2012-03-09 16:00:36 +08:00
|
|
|
}
|
2013-10-08 03:57:58 +08:00
|
|
|
} else if (AllowRaw && IsRaw) {
|
|
|
|
FoundRaw = true;
|
|
|
|
} else if (AllowTemplate && IsTemplate) {
|
|
|
|
FoundTemplate = true;
|
|
|
|
} else if (AllowStringTemplate && IsStringTemplate) {
|
|
|
|
FoundStringTemplate = true;
|
2012-03-09 16:00:36 +08:00
|
|
|
} else {
|
|
|
|
F.erase();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
F.done();
|
|
|
|
|
|
|
|
// C++11 [lex.ext]p3, p4: If S contains a literal operator with a matching
|
|
|
|
// parameter type, that is used in preference to a raw literal operator
|
|
|
|
// or literal operator template.
|
|
|
|
if (FoundExactMatch)
|
|
|
|
return LOLR_Cooked;
|
|
|
|
|
|
|
|
// C++11 [lex.ext]p3, p4: S shall contain a raw literal operator or a literal
|
|
|
|
// operator template, but not both.
|
|
|
|
if (FoundRaw && FoundTemplate) {
|
|
|
|
Diag(R.getNameLoc(), diag::err_ovl_ambiguous_call) << R.getLookupName();
|
2014-01-22 15:29:52 +08:00
|
|
|
for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I)
|
|
|
|
NoteOverloadCandidate((*I)->getUnderlyingDecl()->getAsFunction());
|
2012-03-09 16:00:36 +08:00
|
|
|
return LOLR_Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FoundRaw)
|
|
|
|
return LOLR_Raw;
|
|
|
|
|
|
|
|
if (FoundTemplate)
|
|
|
|
return LOLR_Template;
|
|
|
|
|
2013-10-08 03:57:58 +08:00
|
|
|
if (FoundStringTemplate)
|
|
|
|
return LOLR_StringTemplate;
|
|
|
|
|
2012-03-09 16:00:36 +08:00
|
|
|
// Didn't find anything we could use.
|
|
|
|
Diag(R.getNameLoc(), diag::err_ovl_no_viable_literal_operator)
|
|
|
|
<< R.getLookupName() << (int)ArgTys.size() << ArgTys[0]
|
2013-10-08 03:57:58 +08:00
|
|
|
<< (ArgTys.size() == 2 ? ArgTys[1] : QualType()) << AllowRaw
|
|
|
|
<< (AllowTemplate || AllowStringTemplate);
|
2012-03-09 16:00:36 +08:00
|
|
|
return LOLR_Error;
|
|
|
|
}
|
|
|
|
|
2010-01-26 15:16:45 +08:00
|
|
|
void ADLResult::insert(NamedDecl *New) {
|
|
|
|
NamedDecl *&Old = Decls[cast<NamedDecl>(New->getCanonicalDecl())];
|
|
|
|
|
|
|
|
// If we haven't yet seen a decl for this key, or the last decl
|
|
|
|
// was exactly this one, we're done.
|
2014-05-26 14:22:03 +08:00
|
|
|
if (Old == nullptr || Old == New) {
|
2010-01-26 15:16:45 +08:00
|
|
|
Old = New;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, decide which is a more recent redeclaration.
|
2014-01-22 15:29:52 +08:00
|
|
|
FunctionDecl *OldFD = Old->getAsFunction();
|
|
|
|
FunctionDecl *NewFD = New->getAsFunction();
|
2010-01-26 15:16:45 +08:00
|
|
|
|
|
|
|
FunctionDecl *Cursor = NewFD;
|
|
|
|
while (true) {
|
2012-01-15 00:38:05 +08:00
|
|
|
Cursor = Cursor->getPreviousDecl();
|
2010-01-26 15:16:45 +08:00
|
|
|
|
|
|
|
// If we got to the end without finding OldFD, OldFD is the newer
|
|
|
|
// declaration; leave things as they are.
|
|
|
|
if (!Cursor) return;
|
|
|
|
|
|
|
|
// If we do find OldFD, then NewFD is newer.
|
|
|
|
if (Cursor == OldFD) break;
|
|
|
|
|
|
|
|
// Otherwise, keep looking.
|
|
|
|
}
|
|
|
|
|
|
|
|
Old = New;
|
|
|
|
}
|
|
|
|
|
2014-04-17 09:52:14 +08:00
|
|
|
void Sema::ArgumentDependentLookup(DeclarationName Name, SourceLocation Loc,
|
|
|
|
ArrayRef<Expr *> Args, ADLResult &Result) {
|
2009-03-13 08:33:25 +08:00
|
|
|
// Find all of the associated namespaces and classes based on the
|
|
|
|
// arguments we have.
|
|
|
|
AssociatedNamespaceSet AssociatedNamespaces;
|
|
|
|
AssociatedClassSet AssociatedClasses;
|
2012-08-25 04:38:34 +08:00
|
|
|
FindAssociatedClassesAndNamespaces(Loc, Args,
|
2009-08-08 06:18:02 +08:00
|
|
|
AssociatedNamespaces,
|
|
|
|
AssociatedClasses);
|
2009-03-13 08:33:25 +08:00
|
|
|
|
|
|
|
// C++ [basic.lookup.argdep]p3:
|
|
|
|
// Let X be the lookup set produced by unqualified lookup (3.4.1)
|
|
|
|
// and let Y be the lookup set produced by argument dependent
|
|
|
|
// lookup (defined as follows). If X contains [...] then Y is
|
|
|
|
// empty. Otherwise Y is the set of declarations found in the
|
|
|
|
// namespaces associated with the argument types as described
|
|
|
|
// below. The set of declarations found by the lookup of the name
|
|
|
|
// is the union of X and Y.
|
|
|
|
//
|
|
|
|
// Here, we compute Y and add its members to the overloaded
|
|
|
|
// candidate set.
|
2014-07-16 06:03:49 +08:00
|
|
|
for (auto *NS : AssociatedNamespaces) {
|
2009-03-13 08:33:25 +08:00
|
|
|
// When considering an associated namespace, the lookup is the
|
|
|
|
// same as the lookup performed when the associated namespace is
|
|
|
|
// used as a qualifier (3.4.3.2) except that:
|
|
|
|
//
|
|
|
|
// -- Any using-directives in the associated namespace are
|
|
|
|
// ignored.
|
|
|
|
//
|
2009-08-08 06:18:02 +08:00
|
|
|
// -- Any namespace-scope friend functions declared in
|
2009-03-13 08:33:25 +08:00
|
|
|
// associated classes are visible within their respective
|
|
|
|
// namespaces even if they are not visible during an ordinary
|
|
|
|
// lookup (11.4).
|
2014-07-16 06:03:49 +08:00
|
|
|
DeclContext::lookup_result R = NS->lookup(Name);
|
|
|
|
for (auto *D : R) {
|
2009-08-28 15:59:38 +08:00
|
|
|
// If the only declaration here is an ordinary friend, consider
|
|
|
|
// it only if it was declared in an associated classes.
|
2013-09-20 09:15:31 +08:00
|
|
|
if ((D->getIdentifierNamespace() & Decl::IDNS_Ordinary) == 0) {
|
|
|
|
// If it's neither ordinarily visible nor a friend, we can't find it.
|
|
|
|
if ((D->getIdentifierNamespace() & Decl::IDNS_OrdinaryFriend) == 0)
|
|
|
|
continue;
|
|
|
|
|
2013-07-18 07:53:16 +08:00
|
|
|
bool DeclaredInAssociatedClass = false;
|
|
|
|
for (Decl *DI = D; DI; DI = DI->getPreviousDecl()) {
|
|
|
|
DeclContext *LexDC = DI->getLexicalDeclContext();
|
|
|
|
if (isa<CXXRecordDecl>(LexDC) &&
|
2015-12-19 06:19:11 +08:00
|
|
|
AssociatedClasses.count(cast<CXXRecordDecl>(LexDC)) &&
|
|
|
|
isVisible(cast<NamedDecl>(DI))) {
|
2013-07-18 07:53:16 +08:00
|
|
|
DeclaredInAssociatedClass = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!DeclaredInAssociatedClass)
|
2009-08-11 14:59:38 +08:00
|
|
|
continue;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-01-26 14:04:06 +08:00
|
|
|
if (isa<UsingShadowDecl>(D))
|
|
|
|
D = cast<UsingShadowDecl>(D)->getTargetDecl();
|
|
|
|
|
2014-04-17 09:52:14 +08:00
|
|
|
if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D))
|
2010-01-26 15:16:45 +08:00
|
|
|
continue;
|
|
|
|
|
2015-06-12 09:32:13 +08:00
|
|
|
if (!isVisible(D) && !(D = findAcceptableDecl(*this, D)))
|
|
|
|
continue;
|
|
|
|
|
2010-01-26 15:16:45 +08:00
|
|
|
Result.insert(D);
|
2009-06-24 04:14:09 +08:00
|
|
|
}
|
|
|
|
}
|
2009-03-13 08:33:25 +08:00
|
|
|
}
|
2009-12-31 01:04:44 +08:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// Search for all visible declarations.
|
|
|
|
//----------------------------------------------------------------------------
|
2015-10-20 21:23:58 +08:00
|
|
|
VisibleDeclConsumer::~VisibleDeclConsumer() { }
|
2009-12-31 01:04:44 +08:00
|
|
|
|
2013-08-21 04:35:18 +08:00
|
|
|
bool VisibleDeclConsumer::includeHiddenDecls() const { return false; }
|
|
|
|
|
2009-12-31 01:04:44 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class ShadowContextRAII;
|
|
|
|
|
|
|
|
class VisibleDeclsRecord {
|
|
|
|
public:
|
|
|
|
/// \brief An entry in the shadow map, which is optimized to store a
|
|
|
|
/// single declaration (the common case) but can also store a list
|
|
|
|
/// of declarations.
|
2011-07-18 09:54:02 +08:00
|
|
|
typedef llvm::TinyPtrVector<NamedDecl*> ShadowMapEntry;
|
2009-12-31 01:04:44 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
/// \brief A mapping from declaration names to the declarations that have
|
|
|
|
/// this name within a particular scope.
|
|
|
|
typedef llvm::DenseMap<DeclarationName, ShadowMapEntry> ShadowMap;
|
|
|
|
|
|
|
|
/// \brief A list of shadow maps, which is used to model name hiding.
|
|
|
|
std::list<ShadowMap> ShadowMaps;
|
|
|
|
|
|
|
|
/// \brief The declaration contexts we have already visited.
|
|
|
|
llvm::SmallPtrSet<DeclContext *, 8> VisitedContexts;
|
|
|
|
|
|
|
|
friend class ShadowContextRAII;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/// \brief Determine whether we have already visited this context
|
|
|
|
/// (and, if not, note that we are going to visit that context now).
|
|
|
|
bool visitedContext(DeclContext *Ctx) {
|
2014-11-19 15:49:47 +08:00
|
|
|
return !VisitedContexts.insert(Ctx).second;
|
2009-12-31 01:04:44 +08:00
|
|
|
}
|
|
|
|
|
2010-08-15 14:18:01 +08:00
|
|
|
bool alreadyVisitedContext(DeclContext *Ctx) {
|
|
|
|
return VisitedContexts.count(Ctx);
|
|
|
|
}
|
|
|
|
|
2009-12-31 01:04:44 +08:00
|
|
|
/// \brief Determine whether the given declaration is hidden in the
|
|
|
|
/// current scope.
|
|
|
|
///
|
|
|
|
/// \returns the declaration that hides the given declaration, or
|
|
|
|
/// NULL if no such declaration exists.
|
|
|
|
NamedDecl *checkHidden(NamedDecl *ND);
|
|
|
|
|
|
|
|
/// \brief Add a declaration to the current shadow map.
|
2011-07-18 09:54:02 +08:00
|
|
|
void add(NamedDecl *ND) {
|
|
|
|
ShadowMaps.back()[ND->getDeclName()].push_back(ND);
|
|
|
|
}
|
2009-12-31 01:04:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// \brief RAII object that records when we've entered a shadow context.
|
|
|
|
class ShadowContextRAII {
|
|
|
|
VisibleDeclsRecord &Visible;
|
|
|
|
|
|
|
|
typedef VisibleDeclsRecord::ShadowMap ShadowMap;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ShadowContextRAII(VisibleDeclsRecord &Visible) : Visible(Visible) {
|
2015-05-30 03:42:19 +08:00
|
|
|
Visible.ShadowMaps.emplace_back();
|
2009-12-31 01:04:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
~ShadowContextRAII() {
|
|
|
|
Visible.ShadowMaps.pop_back();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
NamedDecl *VisibleDeclsRecord::checkHidden(NamedDecl *ND) {
|
|
|
|
unsigned IDNS = ND->getIdentifierNamespace();
|
|
|
|
std::list<ShadowMap>::reverse_iterator SM = ShadowMaps.rbegin();
|
|
|
|
for (std::list<ShadowMap>::reverse_iterator SMEnd = ShadowMaps.rend();
|
|
|
|
SM != SMEnd; ++SM) {
|
|
|
|
ShadowMap::iterator Pos = SM->find(ND->getDeclName());
|
|
|
|
if (Pos == SM->end())
|
|
|
|
continue;
|
|
|
|
|
2014-07-16 06:03:49 +08:00
|
|
|
for (auto *D : Pos->second) {
|
2009-12-31 01:04:44 +08:00
|
|
|
// A tag declaration does not hide a non-tag declaration.
|
2014-07-16 06:03:49 +08:00
|
|
|
if (D->hasTagIdentifierNamespace() &&
|
2011-01-27 15:10:08 +08:00
|
|
|
(IDNS & (Decl::IDNS_Member | Decl::IDNS_Ordinary |
|
2009-12-31 01:04:44 +08:00
|
|
|
Decl::IDNS_ObjCProtocol)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Protocols are in distinct namespaces from everything else.
|
2014-07-16 06:03:49 +08:00
|
|
|
if (((D->getIdentifierNamespace() & Decl::IDNS_ObjCProtocol)
|
2009-12-31 01:04:44 +08:00
|
|
|
|| (IDNS & Decl::IDNS_ObjCProtocol)) &&
|
2014-07-16 06:03:49 +08:00
|
|
|
D->getIdentifierNamespace() != IDNS)
|
2009-12-31 01:04:44 +08:00
|
|
|
continue;
|
|
|
|
|
2010-01-14 23:47:35 +08:00
|
|
|
// Functions and function templates in the same scope overload
|
|
|
|
// rather than hide. FIXME: Look for hiding based on function
|
|
|
|
// signatures!
|
2014-07-16 06:03:49 +08:00
|
|
|
if (D->getUnderlyingDecl()->isFunctionOrFunctionTemplate() &&
|
2014-01-22 15:29:52 +08:00
|
|
|
ND->getUnderlyingDecl()->isFunctionOrFunctionTemplate() &&
|
2010-01-14 23:47:35 +08:00
|
|
|
SM == ShadowMaps.rbegin())
|
2010-01-14 11:35:48 +08:00
|
|
|
continue;
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2009-12-31 01:04:44 +08:00
|
|
|
// We've found a declaration that hides this one.
|
2014-07-16 06:03:49 +08:00
|
|
|
return D;
|
2009-12-31 01:04:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2009-12-31 01:04:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void LookupVisibleDecls(DeclContext *Ctx, LookupResult &Result,
|
|
|
|
bool QualifiedNameLookup,
|
2010-01-14 23:47:35 +08:00
|
|
|
bool InBaseClass,
|
2009-12-31 01:04:44 +08:00
|
|
|
VisibleDeclConsumer &Consumer,
|
|
|
|
VisibleDeclsRecord &Visited) {
|
2010-02-05 07:42:48 +08:00
|
|
|
if (!Ctx)
|
|
|
|
return;
|
|
|
|
|
2009-12-31 01:04:44 +08:00
|
|
|
// Make sure we don't visit the same context twice.
|
|
|
|
if (Visited.visitedContext(Ctx->getPrimaryContext()))
|
|
|
|
return;
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2015-03-23 11:25:59 +08:00
|
|
|
// Outside C++, lookup results for the TU live on identifiers.
|
|
|
|
if (isa<TranslationUnitDecl>(Ctx) &&
|
|
|
|
!Result.getSema().getLangOpts().CPlusPlus) {
|
|
|
|
auto &S = Result.getSema();
|
|
|
|
auto &Idents = S.Context.Idents;
|
|
|
|
|
|
|
|
// Ensure all external identifiers are in the identifier table.
|
|
|
|
if (IdentifierInfoLookup *External = Idents.getExternalIdentifierLookup()) {
|
|
|
|
std::unique_ptr<IdentifierIterator> Iter(External->getIdentifiers());
|
|
|
|
for (StringRef Name = Iter->Next(); !Name.empty(); Name = Iter->Next())
|
|
|
|
Idents.get(Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Walk all lookup results in the TU for each identifier.
|
|
|
|
for (const auto &Ident : Idents) {
|
|
|
|
for (auto I = S.IdResolver.begin(Ident.getValue()),
|
|
|
|
E = S.IdResolver.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (S.IdResolver.isDeclInScope(*I, Ctx)) {
|
|
|
|
if (NamedDecl *ND = Result.getAcceptableDecl(*I)) {
|
|
|
|
Consumer.FoundDecl(ND, Visited.checkHidden(ND), Ctx, InBaseClass);
|
|
|
|
Visited.add(ND);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-07-03 04:37:36 +08:00
|
|
|
if (CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(Ctx))
|
|
|
|
Result.getSema().ForceDeclarationOfImplicitMembers(Class);
|
|
|
|
|
2009-12-31 01:04:44 +08:00
|
|
|
// Enumerate all of the results in this context.
|
2015-04-15 11:48:48 +08:00
|
|
|
for (DeclContextLookupResult R : Ctx->lookups()) {
|
2015-03-23 11:25:59 +08:00
|
|
|
for (auto *D : R) {
|
|
|
|
if (auto *ND = Result.getAcceptableDecl(D)) {
|
|
|
|
Consumer.FoundDecl(ND, Visited.checkHidden(ND), Ctx, InBaseClass);
|
|
|
|
Visited.add(ND);
|
2010-12-10 05:44:02 +08:00
|
|
|
}
|
2009-12-31 01:04:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Traverse using directives for qualified name lookup.
|
|
|
|
if (QualifiedNameLookup) {
|
|
|
|
ShadowContextRAII Shadow(Visited);
|
2014-03-18 01:14:12 +08:00
|
|
|
for (auto I : Ctx->using_directives()) {
|
2014-03-07 21:44:44 +08:00
|
|
|
LookupVisibleDecls(I->getNominatedNamespace(), Result,
|
2010-01-14 23:47:35 +08:00
|
|
|
QualifiedNameLookup, InBaseClass, Consumer, Visited);
|
2009-12-31 01:04:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-04 02:01:57 +08:00
|
|
|
// Traverse the contexts of inherited C++ classes.
|
2009-12-31 01:04:44 +08:00
|
|
|
if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx)) {
|
2010-02-05 06:26:26 +08:00
|
|
|
if (!Record->hasDefinition())
|
|
|
|
return;
|
|
|
|
|
2014-03-13 23:41:46 +08:00
|
|
|
for (const auto &B : Record->bases()) {
|
|
|
|
QualType BaseType = B.getType();
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2009-12-31 01:04:44 +08:00
|
|
|
// Don't look into dependent bases, because name lookup can't look
|
|
|
|
// there anyway.
|
|
|
|
if (BaseType->isDependentType())
|
|
|
|
continue;
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2009-12-31 01:04:44 +08:00
|
|
|
const RecordType *Record = BaseType->getAs<RecordType>();
|
|
|
|
if (!Record)
|
|
|
|
continue;
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2009-12-31 01:04:44 +08:00
|
|
|
// 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, we
|
|
|
|
// could be smart enough to qualify the member with the base
|
|
|
|
// class, e.g.,
|
|
|
|
//
|
|
|
|
// c->B::member
|
|
|
|
//
|
|
|
|
// or
|
|
|
|
//
|
|
|
|
// c->A::member
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2009-12-31 01:04:44 +08:00
|
|
|
// Find results in this base class (and its bases).
|
|
|
|
ShadowContextRAII Shadow(Visited);
|
|
|
|
LookupVisibleDecls(Record->getDecl(), Result, QualifiedNameLookup,
|
2010-01-14 23:47:35 +08:00
|
|
|
true, Consumer, Visited);
|
2009-12-31 01:04:44 +08:00
|
|
|
}
|
|
|
|
}
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2010-01-04 02:01:57 +08:00
|
|
|
// Traverse the contexts of Objective-C classes.
|
|
|
|
if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Ctx)) {
|
|
|
|
// Traverse categories.
|
2014-03-14 05:23:55 +08:00
|
|
|
for (auto *Cat : IFace->visible_categories()) {
|
2010-01-04 02:01:57 +08:00
|
|
|
ShadowContextRAII Shadow(Visited);
|
2014-03-14 05:23:55 +08:00
|
|
|
LookupVisibleDecls(Cat, Result, QualifiedNameLookup, false,
|
2010-01-14 23:47:35 +08:00
|
|
|
Consumer, Visited);
|
2010-01-04 02:01:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Traverse protocols.
|
2014-03-14 04:55:22 +08:00
|
|
|
for (auto *I : IFace->all_referenced_protocols()) {
|
2010-01-04 02:01:57 +08:00
|
|
|
ShadowContextRAII Shadow(Visited);
|
2014-03-14 04:55:22 +08:00
|
|
|
LookupVisibleDecls(I, Result, QualifiedNameLookup, false, Consumer,
|
2010-01-14 23:47:35 +08:00
|
|
|
Visited);
|
2010-01-04 02:01:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Traverse the superclass.
|
|
|
|
if (IFace->getSuperClass()) {
|
|
|
|
ShadowContextRAII Shadow(Visited);
|
|
|
|
LookupVisibleDecls(IFace->getSuperClass(), Result, QualifiedNameLookup,
|
2010-01-14 23:47:35 +08:00
|
|
|
true, Consumer, Visited);
|
2010-01-04 02:01:57 +08:00
|
|
|
}
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2010-04-20 02:02:19 +08:00
|
|
|
// If there is an implementation, traverse it. We do this to find
|
|
|
|
// synthesized ivars.
|
|
|
|
if (IFace->getImplementation()) {
|
|
|
|
ShadowContextRAII Shadow(Visited);
|
2011-01-27 15:10:08 +08:00
|
|
|
LookupVisibleDecls(IFace->getImplementation(), Result,
|
2012-04-04 04:26:45 +08:00
|
|
|
QualifiedNameLookup, InBaseClass, Consumer, Visited);
|
2010-04-20 02:02:19 +08:00
|
|
|
}
|
2010-01-04 02:01:57 +08:00
|
|
|
} else if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Ctx)) {
|
2014-03-14 06:58:06 +08:00
|
|
|
for (auto *I : Protocol->protocols()) {
|
2010-01-04 02:01:57 +08:00
|
|
|
ShadowContextRAII Shadow(Visited);
|
2014-03-14 06:58:06 +08:00
|
|
|
LookupVisibleDecls(I, Result, QualifiedNameLookup, false, Consumer,
|
2010-01-14 23:47:35 +08:00
|
|
|
Visited);
|
2010-01-04 02:01:57 +08:00
|
|
|
}
|
|
|
|
} else if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Ctx)) {
|
2014-03-14 20:55:57 +08:00
|
|
|
for (auto *I : Category->protocols()) {
|
2010-01-04 02:01:57 +08:00
|
|
|
ShadowContextRAII Shadow(Visited);
|
2014-03-14 20:55:57 +08:00
|
|
|
LookupVisibleDecls(I, Result, QualifiedNameLookup, false, Consumer,
|
2010-01-14 23:47:35 +08:00
|
|
|
Visited);
|
2010-01-04 02:01:57 +08:00
|
|
|
}
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2010-04-20 02:02:19 +08:00
|
|
|
// If there is an implementation, traverse it.
|
|
|
|
if (Category->getImplementation()) {
|
|
|
|
ShadowContextRAII Shadow(Visited);
|
2011-01-27 15:10:08 +08:00
|
|
|
LookupVisibleDecls(Category->getImplementation(), Result,
|
2010-04-20 02:02:19 +08:00
|
|
|
QualifiedNameLookup, true, Consumer, Visited);
|
2011-01-27 15:10:08 +08:00
|
|
|
}
|
2010-01-04 02:01:57 +08:00
|
|
|
}
|
2009-12-31 01:04:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void LookupVisibleDecls(Scope *S, LookupResult &Result,
|
|
|
|
UnqualUsingDirectiveSet &UDirs,
|
|
|
|
VisibleDeclConsumer &Consumer,
|
|
|
|
VisibleDeclsRecord &Visited) {
|
|
|
|
if (!S)
|
|
|
|
return;
|
|
|
|
|
2011-01-27 15:10:08 +08:00
|
|
|
if (!S->getEntity() ||
|
|
|
|
(!S->getParent() &&
|
2013-10-09 01:08:03 +08:00
|
|
|
!Visited.alreadyVisitedContext(S->getEntity())) ||
|
|
|
|
(S->getEntity())->isFunctionOrMethod()) {
|
2013-09-20 09:15:31 +08:00
|
|
|
FindLocalExternScope FindLocals(Result);
|
2010-01-07 08:31:29 +08:00
|
|
|
// Walk through the declarations in this Scope.
|
2014-03-18 00:55:25 +08:00
|
|
|
for (auto *D : S->decls()) {
|
|
|
|
if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
|
2011-12-15 00:03:29 +08:00
|
|
|
if ((ND = Result.getAcceptableDecl(ND))) {
|
2014-05-26 14:22:03 +08:00
|
|
|
Consumer.FoundDecl(ND, Visited.checkHidden(ND), nullptr, false);
|
2010-01-07 08:31:29 +08:00
|
|
|
Visited.add(ND);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2010-03-15 22:33:29 +08:00
|
|
|
// FIXME: C++ [temp.local]p8
|
2014-05-26 14:22:03 +08:00
|
|
|
DeclContext *Entity = nullptr;
|
2010-01-02 01:44:25 +08:00
|
|
|
if (S->getEntity()) {
|
2009-12-31 01:04:44 +08:00
|
|
|
// 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.
|
2013-10-09 01:08:03 +08:00
|
|
|
Entity = S->getEntity();
|
2010-03-15 22:33:29 +08:00
|
|
|
DeclContext *OuterCtx = findOuterContext(S).first; // FIXME
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2010-03-15 23:26:48 +08:00
|
|
|
for (DeclContext *Ctx = Entity; Ctx && !Ctx->Equals(OuterCtx);
|
2009-12-31 01:04:44 +08:00
|
|
|
Ctx = Ctx->getLookupParent()) {
|
2010-01-04 02:01:57 +08:00
|
|
|
if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(Ctx)) {
|
|
|
|
if (Method->isInstanceMethod()) {
|
|
|
|
// For instance methods, look for ivars in the method's interface.
|
|
|
|
LookupResult IvarResult(Result.getSema(), Result.getLookupName(),
|
|
|
|
Result.getNameLoc(), Sema::LookupMemberName);
|
2010-11-03 04:36:02 +08:00
|
|
|
if (ObjCInterfaceDecl *IFace = Method->getClassInterface()) {
|
2011-01-27 15:10:08 +08:00
|
|
|
LookupVisibleDecls(IFace, IvarResult, /*QualifiedNameLookup=*/false,
|
2013-08-21 04:35:18 +08:00
|
|
|
/*InBaseClass=*/false, Consumer, Visited);
|
2010-11-03 04:36:02 +08:00
|
|
|
}
|
2010-01-04 02:01:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// We've already performed all of the name lookup that we need
|
|
|
|
// to for Objective-C methods; the next context will be the
|
|
|
|
// outer scope.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-12-31 01:04:44 +08:00
|
|
|
if (Ctx->isFunctionOrMethod())
|
|
|
|
continue;
|
2011-01-27 15:10:08 +08:00
|
|
|
|
|
|
|
LookupVisibleDecls(Ctx, Result, /*QualifiedNameLookup=*/false,
|
2010-01-14 23:47:35 +08:00
|
|
|
/*InBaseClass=*/false, Consumer, Visited);
|
2009-12-31 01:04:44 +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
|
2011-01-27 15:10:08 +08:00
|
|
|
// translation unit decl when the IdentifierInfo chains would suffice.
|
2009-12-31 01:04:44 +08:00
|
|
|
// Once we fix that problem (which is part of a more general "don't look
|
2010-01-07 08:31:29 +08:00
|
|
|
// in DeclContexts unless we have to" optimization), we can eliminate this.
|
2009-12-31 01:04:44 +08:00
|
|
|
Entity = Result.getSema().Context.getTranslationUnitDecl();
|
2011-01-27 15:10:08 +08:00
|
|
|
LookupVisibleDecls(Entity, Result, /*QualifiedNameLookup=*/false,
|
2010-01-14 23:47:35 +08:00
|
|
|
/*InBaseClass=*/false, Consumer, Visited);
|
2011-01-27 15:10:08 +08:00
|
|
|
}
|
|
|
|
|
2009-12-31 01:04:44 +08:00
|
|
|
if (Entity) {
|
|
|
|
// Lookup visible declarations in any namespaces found by using
|
|
|
|
// directives.
|
2015-02-07 01:25:10 +08:00
|
|
|
for (const UnqualUsingEntry &UUE : UDirs.getNamespacesFor(Entity))
|
|
|
|
LookupVisibleDecls(const_cast<DeclContext *>(UUE.getNominatedNamespace()),
|
2011-01-27 15:10:08 +08:00
|
|
|
Result, /*QualifiedNameLookup=*/false,
|
2010-01-14 23:47:35 +08:00
|
|
|
/*InBaseClass=*/false, Consumer, Visited);
|
2009-12-31 01:04:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup names in the parent scope.
|
|
|
|
ShadowContextRAII Shadow(Visited);
|
|
|
|
LookupVisibleDecls(S->getParent(), Result, UDirs, Consumer, Visited);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sema::LookupVisibleDecls(Scope *S, LookupNameKind Kind,
|
2010-08-15 14:18:01 +08:00
|
|
|
VisibleDeclConsumer &Consumer,
|
|
|
|
bool IncludeGlobalScope) {
|
2009-12-31 01:04:44 +08:00
|
|
|
// Determine the set of using directives available during
|
|
|
|
// unqualified name lookup.
|
|
|
|
Scope *Initial = S;
|
|
|
|
UnqualUsingDirectiveSet UDirs;
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().CPlusPlus) {
|
2009-12-31 01:04:44 +08:00
|
|
|
// Find the first namespace or translation-unit scope.
|
|
|
|
while (S && !isNamespaceOrTranslationUnitScope(S))
|
|
|
|
S = S->getParent();
|
|
|
|
|
|
|
|
UDirs.visitScopeChain(Initial, S);
|
|
|
|
}
|
|
|
|
UDirs.done();
|
|
|
|
|
|
|
|
// Look for visible declarations.
|
|
|
|
LookupResult Result(*this, DeclarationName(), SourceLocation(), Kind);
|
2013-08-21 04:35:18 +08:00
|
|
|
Result.setAllowHidden(Consumer.includeHiddenDecls());
|
2009-12-31 01:04:44 +08:00
|
|
|
VisibleDeclsRecord Visited;
|
2010-08-15 14:18:01 +08:00
|
|
|
if (!IncludeGlobalScope)
|
|
|
|
Visited.visitedContext(Context.getTranslationUnitDecl());
|
2009-12-31 01:04:44 +08:00
|
|
|
ShadowContextRAII Shadow(Visited);
|
|
|
|
::LookupVisibleDecls(Initial, Result, UDirs, Consumer, Visited);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sema::LookupVisibleDecls(DeclContext *Ctx, LookupNameKind Kind,
|
2010-08-15 14:18:01 +08:00
|
|
|
VisibleDeclConsumer &Consumer,
|
|
|
|
bool IncludeGlobalScope) {
|
2009-12-31 01:04:44 +08:00
|
|
|
LookupResult Result(*this, DeclarationName(), SourceLocation(), Kind);
|
2013-08-21 04:35:18 +08:00
|
|
|
Result.setAllowHidden(Consumer.includeHiddenDecls());
|
2009-12-31 01:04:44 +08:00
|
|
|
VisibleDeclsRecord Visited;
|
2010-08-15 14:18:01 +08:00
|
|
|
if (!IncludeGlobalScope)
|
|
|
|
Visited.visitedContext(Context.getTranslationUnitDecl());
|
2009-12-31 01:04:44 +08:00
|
|
|
ShadowContextRAII Shadow(Visited);
|
2011-01-27 15:10:08 +08:00
|
|
|
::LookupVisibleDecls(Ctx, Result, /*QualifiedNameLookup=*/true,
|
2010-01-14 23:47:35 +08:00
|
|
|
/*InBaseClass=*/false, Consumer, Visited);
|
2009-12-31 01:04:44 +08:00
|
|
|
}
|
|
|
|
|
2011-02-18 10:08:43 +08:00
|
|
|
/// LookupOrCreateLabel - Do a name lookup of a label with the specified name.
|
2011-03-06 02:21:20 +08:00
|
|
|
/// If GnuLabelLoc is a valid source location, then this is a definition
|
|
|
|
/// of an __label__ label name, otherwise it is a normal label definition
|
|
|
|
/// or use.
|
2011-02-18 10:08:43 +08:00
|
|
|
LabelDecl *Sema::LookupOrCreateLabel(IdentifierInfo *II, SourceLocation Loc,
|
2011-03-06 02:21:20 +08:00
|
|
|
SourceLocation GnuLabelLoc) {
|
2011-02-18 09:27:55 +08:00
|
|
|
// Do a lookup to see if we have a label with this name already.
|
2014-05-26 14:22:03 +08:00
|
|
|
NamedDecl *Res = nullptr;
|
2011-03-06 02:21:20 +08:00
|
|
|
|
|
|
|
if (GnuLabelLoc.isValid()) {
|
|
|
|
// Local label definitions always shadow existing labels.
|
|
|
|
Res = LabelDecl::Create(Context, CurContext, Loc, II, GnuLabelLoc);
|
|
|
|
Scope *S = CurScope;
|
|
|
|
PushOnScopeChains(Res, S, true);
|
|
|
|
return cast<LabelDecl>(Res);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not a GNU local label.
|
|
|
|
Res = LookupSingleName(CurScope, II, Loc, LookupLabel, NotForRedeclaration);
|
|
|
|
// If we found a label, check to see if it is in the same context as us.
|
|
|
|
// When in a Block, we don't want to reuse a label in an enclosing function.
|
2011-02-18 09:27:55 +08:00
|
|
|
if (Res && Res->getDeclContext() != CurContext)
|
2014-05-26 14:22:03 +08:00
|
|
|
Res = nullptr;
|
|
|
|
if (!Res) {
|
2011-02-18 09:27:55 +08:00
|
|
|
// If not forward referenced or defined already, create the backing decl.
|
2011-03-06 02:21:20 +08:00
|
|
|
Res = LabelDecl::Create(Context, CurContext, Loc, II);
|
|
|
|
Scope *S = CurScope->getFnParent();
|
2011-02-19 05:16:39 +08:00
|
|
|
assert(S && "Not in a function?");
|
|
|
|
PushOnScopeChains(Res, S, true);
|
2011-02-18 09:27:55 +08:00
|
|
|
}
|
|
|
|
return cast<LabelDecl>(Res);
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-12-31 01:04:44 +08:00
|
|
|
// Typo correction
|
2011-02-18 09:27:55 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2009-12-31 01:04:44 +08:00
|
|
|
|
2014-06-12 02:06:56 +08:00
|
|
|
static bool isCandidateViable(CorrectionCandidateCallback &CCC,
|
|
|
|
TypoCorrection &Candidate) {
|
|
|
|
Candidate.setCallbackDistance(CCC.RankCandidate(Candidate));
|
|
|
|
return Candidate.getEditDistance(false) != TypoCorrection::InvalidDistance;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void LookupPotentialTypoResult(Sema &SemaRef,
|
|
|
|
LookupResult &Res,
|
|
|
|
IdentifierInfo *Name,
|
|
|
|
Scope *S, CXXScopeSpec *SS,
|
|
|
|
DeclContext *MemberContext,
|
|
|
|
bool EnteringContext,
|
|
|
|
bool isObjCIvarLookup,
|
|
|
|
bool FindHidden);
|
|
|
|
|
2014-10-28 02:07:40 +08:00
|
|
|
/// \brief Check whether the declarations found for a typo correction are
|
|
|
|
/// visible, and if none of them are, convert the correction to an 'import
|
|
|
|
/// a module' correction.
|
|
|
|
static void checkCorrectionVisibility(Sema &SemaRef, TypoCorrection &TC) {
|
|
|
|
if (TC.begin() == TC.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
TypoCorrection::decl_iterator DI = TC.begin(), DE = TC.end();
|
|
|
|
|
|
|
|
for (/**/; DI != DE; ++DI)
|
|
|
|
if (!LookupResult::isVisible(SemaRef, *DI))
|
|
|
|
break;
|
|
|
|
// Nothing to do if all decls are visible.
|
|
|
|
if (DI == DE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
llvm::SmallVector<NamedDecl*, 4> NewDecls(TC.begin(), DI);
|
|
|
|
bool AnyVisibleDecls = !NewDecls.empty();
|
|
|
|
|
|
|
|
for (/**/; DI != DE; ++DI) {
|
|
|
|
NamedDecl *VisibleDecl = *DI;
|
|
|
|
if (!LookupResult::isVisible(SemaRef, *DI))
|
|
|
|
VisibleDecl = findAcceptableDecl(SemaRef, *DI);
|
|
|
|
|
|
|
|
if (VisibleDecl) {
|
|
|
|
if (!AnyVisibleDecls) {
|
|
|
|
// Found a visible decl, discard all hidden ones.
|
|
|
|
AnyVisibleDecls = true;
|
|
|
|
NewDecls.clear();
|
|
|
|
}
|
|
|
|
NewDecls.push_back(VisibleDecl);
|
|
|
|
} else if (!AnyVisibleDecls && !(*DI)->isModulePrivate())
|
|
|
|
NewDecls.push_back(*DI);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NewDecls.empty())
|
|
|
|
TC = TypoCorrection();
|
|
|
|
else {
|
|
|
|
TC.setCorrectionDecls(NewDecls);
|
|
|
|
TC.setRequiresImport(!AnyVisibleDecls);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-12 02:06:56 +08:00
|
|
|
// Fill the supplied vector with the IdentifierInfo pointers for each piece of
|
|
|
|
// the given NestedNameSpecifier (i.e. given a NestedNameSpecifier "foo::bar::",
|
|
|
|
// fill the vector with the IdentifierInfo pointers for "foo" and "bar").
|
|
|
|
static void getNestedNameSpecifierIdentifiers(
|
|
|
|
NestedNameSpecifier *NNS,
|
|
|
|
SmallVectorImpl<const IdentifierInfo*> &Identifiers) {
|
|
|
|
if (NestedNameSpecifier *Prefix = NNS->getPrefix())
|
|
|
|
getNestedNameSpecifierIdentifiers(Prefix, Identifiers);
|
|
|
|
else
|
|
|
|
Identifiers.clear();
|
|
|
|
|
|
|
|
const IdentifierInfo *II = nullptr;
|
|
|
|
|
|
|
|
switch (NNS->getKind()) {
|
|
|
|
case NestedNameSpecifier::Identifier:
|
|
|
|
II = NNS->getAsIdentifier();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NestedNameSpecifier::Namespace:
|
|
|
|
if (NNS->getAsNamespace()->isAnonymousNamespace())
|
|
|
|
return;
|
|
|
|
II = NNS->getAsNamespace()->getIdentifier();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NestedNameSpecifier::NamespaceAlias:
|
|
|
|
II = NNS->getAsNamespaceAlias()->getIdentifier();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NestedNameSpecifier::TypeSpecWithTemplate:
|
|
|
|
case NestedNameSpecifier::TypeSpec:
|
|
|
|
II = QualType(NNS->getAsType(), 0).getBaseTypeIdentifier();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NestedNameSpecifier::Global:
|
2014-09-26 08:28:20 +08:00
|
|
|
case NestedNameSpecifier::Super:
|
2014-06-12 02:06:56 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (II)
|
|
|
|
Identifiers.push_back(II);
|
|
|
|
}
|
|
|
|
|
2011-01-27 15:10:08 +08:00
|
|
|
void TypoCorrectionConsumer::FoundDecl(NamedDecl *ND, NamedDecl *Hiding,
|
2011-10-06 15:27:49 +08:00
|
|
|
DeclContext *Ctx, bool InBaseClass) {
|
2009-12-31 01:04:44 +08:00
|
|
|
// Don't consider hidden names for typo correction.
|
|
|
|
if (Hiding)
|
|
|
|
return;
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2009-12-31 01:04:44 +08:00
|
|
|
// Only consider entities with identifiers for names, ignoring
|
|
|
|
// special names (constructors, overloaded operators, selectors,
|
|
|
|
// etc.).
|
|
|
|
IdentifierInfo *Name = ND->getIdentifier();
|
|
|
|
if (!Name)
|
|
|
|
return;
|
|
|
|
|
2013-08-21 04:35:18 +08:00
|
|
|
// Only consider visible declarations and declarations from modules with
|
|
|
|
// names that exactly match.
|
2014-06-12 02:07:01 +08:00
|
|
|
if (!LookupResult::isVisible(SemaRef, ND) && Name != Typo &&
|
2013-08-21 04:35:18 +08:00
|
|
|
!findAcceptableDecl(SemaRef, ND))
|
|
|
|
return;
|
|
|
|
|
2010-10-15 06:11:03 +08:00
|
|
|
FoundName(Name->getName());
|
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
void TypoCorrectionConsumer::FoundName(StringRef Name) {
|
2009-12-31 01:04:44 +08:00
|
|
|
// Compute the edit distance between the typo and the name of this
|
2012-02-07 09:32:58 +08:00
|
|
|
// entity, and add the identifier to the list of results.
|
2014-05-26 14:22:03 +08:00
|
|
|
addName(Name, nullptr);
|
2009-12-31 01:04:44 +08:00
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
void TypoCorrectionConsumer::addKeywordResult(StringRef Keyword) {
|
2012-02-07 09:32:58 +08:00
|
|
|
// Compute the edit distance between the typo and this keyword,
|
|
|
|
// and add the keyword to the list of results.
|
2014-05-26 14:22:03 +08:00
|
|
|
addName(Keyword, nullptr, nullptr, true);
|
2011-06-29 00:20:02 +08:00
|
|
|
}
|
|
|
|
|
2013-10-03 02:26:35 +08:00
|
|
|
void TypoCorrectionConsumer::addName(StringRef Name, NamedDecl *ND,
|
|
|
|
NestedNameSpecifier *NNS, bool isKeyword) {
|
|
|
|
// Use a simple length-based heuristic to determine the minimum possible
|
|
|
|
// edit distance. If the minimum isn't good enough, bail out early.
|
2014-06-12 02:07:01 +08:00
|
|
|
StringRef TypoStr = Typo->getName();
|
|
|
|
unsigned MinED = abs((int)Name.size() - (int)TypoStr.size());
|
|
|
|
if (MinED && TypoStr.size() / MinED < 3)
|
2013-10-03 02:26:35 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Compute an upper bound on the allowable edit distance, so that the
|
|
|
|
// edit-distance algorithm can short-circuit.
|
2014-06-12 02:07:01 +08:00
|
|
|
unsigned UpperBound = (TypoStr.size() + 2) / 3 + 1;
|
|
|
|
unsigned ED = TypoStr.edit_distance(Name, true, UpperBound);
|
2013-10-03 02:26:35 +08:00
|
|
|
if (ED >= UpperBound) return;
|
|
|
|
|
|
|
|
TypoCorrection TC(&SemaRef.Context.Idents.get(Name), ND, NNS, ED);
|
2011-08-04 04:36:05 +08:00
|
|
|
if (isKeyword) TC.makeKeyword();
|
2014-11-21 06:06:26 +08:00
|
|
|
TC.setCorrectionRange(nullptr, Result.getLookupNameInfo());
|
2011-08-04 04:36:05 +08:00
|
|
|
addCorrection(TC);
|
2011-06-29 00:20:02 +08:00
|
|
|
}
|
|
|
|
|
2014-10-28 02:07:13 +08:00
|
|
|
static const unsigned MaxTypoDistanceResultSets = 5;
|
|
|
|
|
2011-06-29 00:20:02 +08:00
|
|
|
void TypoCorrectionConsumer::addCorrection(TypoCorrection Correction) {
|
2014-06-12 02:07:01 +08:00
|
|
|
StringRef TypoStr = Typo->getName();
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Name = Correction.getCorrectionAsIdentifierInfo()->getName();
|
2014-06-12 02:07:01 +08:00
|
|
|
|
|
|
|
// For very short typos, ignore potential corrections that have a different
|
|
|
|
// base identifier from the typo or which have a normalized edit distance
|
|
|
|
// longer than the typo itself.
|
|
|
|
if (TypoStr.size() < 3 &&
|
|
|
|
(Name != TypoStr || Correction.getEditDistance(true) > TypoStr.size()))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// If the correction is resolved but is not viable, ignore it.
|
2014-10-28 02:07:40 +08:00
|
|
|
if (Correction.isResolved()) {
|
|
|
|
checkCorrectionVisibility(SemaRef, Correction);
|
|
|
|
if (!Correction || !isCandidateViable(*CorrectionValidator, Correction))
|
|
|
|
return;
|
|
|
|
}
|
2014-06-12 02:07:01 +08:00
|
|
|
|
2012-06-02 02:11:16 +08:00
|
|
|
TypoResultList &CList =
|
|
|
|
CorrectionResults[Correction.getEditDistance(false)][Name];
|
|
|
|
|
|
|
|
if (!CList.empty() && !CList.back().isResolved())
|
|
|
|
CList.pop_back();
|
|
|
|
if (NamedDecl *NewND = Correction.getCorrectionDecl()) {
|
|
|
|
std::string CorrectionStr = Correction.getAsString(SemaRef.getLangOpts());
|
|
|
|
for (TypoResultList::iterator RI = CList.begin(), RIEnd = CList.end();
|
|
|
|
RI != RIEnd; ++RI) {
|
|
|
|
// If the Correction refers to a decl already in the result list,
|
|
|
|
// replace the existing result if the string representation of Correction
|
|
|
|
// comes before the current result alphabetically, then stop as there is
|
|
|
|
// nothing more to be done to add Correction to the candidate set.
|
|
|
|
if (RI->getCorrectionDecl() == NewND) {
|
|
|
|
if (CorrectionStr < RI->getAsString(SemaRef.getLangOpts()))
|
|
|
|
*RI = Correction;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (CList.empty() || Correction.isResolved())
|
|
|
|
CList.push_back(Correction);
|
2011-06-29 00:20:02 +08:00
|
|
|
|
2012-06-01 07:32:58 +08:00
|
|
|
while (CorrectionResults.size() > MaxTypoDistanceResultSets)
|
2014-06-12 02:07:01 +08:00
|
|
|
CorrectionResults.erase(std::prev(CorrectionResults.end()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TypoCorrectionConsumer::addNamespaces(
|
|
|
|
const llvm::MapVector<NamespaceDecl *, bool> &KnownNamespaces) {
|
|
|
|
SearchNamespaces = true;
|
|
|
|
|
|
|
|
for (auto KNPair : KnownNamespaces)
|
|
|
|
Namespaces.addNameSpecifier(KNPair.first);
|
|
|
|
|
|
|
|
bool SSIsTemplate = false;
|
|
|
|
if (NestedNameSpecifier *NNS =
|
|
|
|
(SS && SS->isValid()) ? SS->getScopeRep() : nullptr) {
|
|
|
|
if (const Type *T = NNS->getAsType())
|
|
|
|
SSIsTemplate = T->getTypeClass() == Type::TemplateSpecialization;
|
|
|
|
}
|
2015-11-18 09:19:02 +08:00
|
|
|
// Do not transform this into an iterator-based loop. The loop body can
|
|
|
|
// trigger the creation of further types (through lazy deserialization) and
|
|
|
|
// invalide iterators into this list.
|
|
|
|
auto &Types = SemaRef.getASTContext().getTypes();
|
|
|
|
for (unsigned I = 0; I != Types.size(); ++I) {
|
|
|
|
const auto *TI = Types[I];
|
2014-06-12 02:07:01 +08:00
|
|
|
if (CXXRecordDecl *CD = TI->getAsCXXRecordDecl()) {
|
|
|
|
CD = CD->getCanonicalDecl();
|
|
|
|
if (!CD->isDependentType() && !CD->isAnonymousStructOrUnion() &&
|
|
|
|
!CD->isUnion() && CD->getIdentifier() &&
|
|
|
|
(SSIsTemplate || !isa<ClassTemplateSpecializationDecl>(CD)) &&
|
|
|
|
(CD->isBeingDefined() || CD->isCompleteDefinition()))
|
|
|
|
Namespaces.addNameSpecifier(CD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-28 02:07:34 +08:00
|
|
|
const TypoCorrection &TypoCorrectionConsumer::getNextCorrection() {
|
|
|
|
if (++CurrentTCIndex < ValidatedCorrections.size())
|
|
|
|
return ValidatedCorrections[CurrentTCIndex];
|
|
|
|
|
|
|
|
CurrentTCIndex = ValidatedCorrections.size();
|
2014-06-12 02:07:01 +08:00
|
|
|
while (!CorrectionResults.empty()) {
|
|
|
|
auto DI = CorrectionResults.begin();
|
|
|
|
if (DI->second.empty()) {
|
|
|
|
CorrectionResults.erase(DI);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto RI = DI->second.begin();
|
|
|
|
if (RI->second.empty()) {
|
|
|
|
DI->second.erase(RI);
|
|
|
|
performQualifiedLookups();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
TypoCorrection TC = RI->second.pop_back_val();
|
2014-10-28 02:07:40 +08:00
|
|
|
if (TC.isResolved() || TC.requiresImport() || resolveCorrection(TC)) {
|
2014-10-28 02:07:34 +08:00
|
|
|
ValidatedCorrections.push_back(TC);
|
|
|
|
return ValidatedCorrections[CurrentTCIndex];
|
|
|
|
}
|
2014-06-12 02:07:01 +08:00
|
|
|
}
|
2014-10-28 02:07:34 +08:00
|
|
|
return ValidatedCorrections[0]; // The empty correction.
|
2014-06-12 02:07:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TypoCorrectionConsumer::resolveCorrection(TypoCorrection &Candidate) {
|
|
|
|
IdentifierInfo *Name = Candidate.getCorrectionAsIdentifierInfo();
|
|
|
|
DeclContext *TempMemberContext = MemberContext;
|
2014-10-28 02:07:37 +08:00
|
|
|
CXXScopeSpec *TempSS = SS.get();
|
2014-06-12 02:07:01 +08:00
|
|
|
retry_lookup:
|
|
|
|
LookupPotentialTypoResult(SemaRef, Result, Name, S, TempSS, TempMemberContext,
|
|
|
|
EnteringContext,
|
2014-10-28 02:07:29 +08:00
|
|
|
CorrectionValidator->IsObjCIvarLookup,
|
2014-06-12 02:07:01 +08:00
|
|
|
Name == Typo && !Candidate.WillReplaceSpecifier());
|
|
|
|
switch (Result.getResultKind()) {
|
|
|
|
case LookupResult::NotFound:
|
|
|
|
case LookupResult::NotFoundInCurrentInstantiation:
|
|
|
|
case LookupResult::FoundUnresolvedValue:
|
|
|
|
if (TempSS) {
|
|
|
|
// Immediately retry the lookup without the given CXXScopeSpec
|
|
|
|
TempSS = nullptr;
|
|
|
|
Candidate.WillReplaceSpecifier(true);
|
|
|
|
goto retry_lookup;
|
|
|
|
}
|
|
|
|
if (TempMemberContext) {
|
|
|
|
if (SS && !TempSS)
|
2014-10-28 02:07:37 +08:00
|
|
|
TempSS = SS.get();
|
2014-06-12 02:07:01 +08:00
|
|
|
TempMemberContext = nullptr;
|
|
|
|
goto retry_lookup;
|
|
|
|
}
|
|
|
|
if (SearchNamespaces)
|
|
|
|
QualifiedResults.push_back(Candidate);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LookupResult::Ambiguous:
|
|
|
|
// We don't deal with ambiguities.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LookupResult::Found:
|
|
|
|
case LookupResult::FoundOverloaded:
|
|
|
|
// Store all of the Decls for overloaded symbols
|
|
|
|
for (auto *TRD : Result)
|
|
|
|
Candidate.addCorrectionDecl(TRD);
|
2014-10-28 02:07:40 +08:00
|
|
|
checkCorrectionVisibility(SemaRef, Candidate);
|
2014-10-28 02:07:29 +08:00
|
|
|
if (!isCandidateViable(*CorrectionValidator, Candidate)) {
|
2014-06-12 02:07:01 +08:00
|
|
|
if (SearchNamespaces)
|
|
|
|
QualifiedResults.push_back(Candidate);
|
|
|
|
break;
|
|
|
|
}
|
2015-01-28 08:46:09 +08:00
|
|
|
Candidate.setCorrectionRange(SS.get(), Result.getLookupNameInfo());
|
2014-06-12 02:07:01 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TypoCorrectionConsumer::performQualifiedLookups() {
|
|
|
|
unsigned TypoLen = Typo->getName().size();
|
|
|
|
for (auto QR : QualifiedResults) {
|
|
|
|
for (auto NSI : Namespaces) {
|
|
|
|
DeclContext *Ctx = NSI.DeclCtx;
|
|
|
|
const Type *NSType = NSI.NameSpecifier->getAsType();
|
|
|
|
|
|
|
|
// If the current NestedNameSpecifier refers to a class and the
|
|
|
|
// current correction candidate is the name of that class, then skip
|
|
|
|
// it as it is unlikely a qualified version of the class' constructor
|
|
|
|
// is an appropriate correction.
|
2015-10-07 07:40:43 +08:00
|
|
|
if (CXXRecordDecl *NSDecl = NSType ? NSType->getAsCXXRecordDecl() :
|
|
|
|
nullptr) {
|
2014-06-12 02:07:01 +08:00
|
|
|
if (NSDecl->getIdentifier() == QR.getCorrectionAsIdentifierInfo())
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
TypoCorrection TC(QR);
|
|
|
|
TC.ClearCorrectionDecls();
|
|
|
|
TC.setCorrectionSpecifier(NSI.NameSpecifier);
|
|
|
|
TC.setQualifierDistance(NSI.EditDistance);
|
|
|
|
TC.setCallbackDistance(0); // Reset the callback distance
|
|
|
|
|
|
|
|
// If the current correction candidate and namespace combination are
|
|
|
|
// too far away from the original typo based on the normalized edit
|
|
|
|
// distance, then skip performing a qualified name lookup.
|
|
|
|
unsigned TmpED = TC.getEditDistance(true);
|
|
|
|
if (QR.getCorrectionAsIdentifierInfo() != Typo && TmpED &&
|
|
|
|
TypoLen / TmpED < 3)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Result.clear();
|
|
|
|
Result.setLookupName(QR.getCorrectionAsIdentifierInfo());
|
|
|
|
if (!SemaRef.LookupQualifiedName(Result, Ctx))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Any corrections added below will be validated in subsequent
|
|
|
|
// iterations of the main while() loop over the Consumer's contents.
|
|
|
|
switch (Result.getResultKind()) {
|
|
|
|
case LookupResult::Found:
|
|
|
|
case LookupResult::FoundOverloaded: {
|
|
|
|
if (SS && SS->isValid()) {
|
|
|
|
std::string NewQualified = TC.getAsString(SemaRef.getLangOpts());
|
|
|
|
std::string OldQualified;
|
|
|
|
llvm::raw_string_ostream OldOStream(OldQualified);
|
|
|
|
SS->getScopeRep()->print(OldOStream, SemaRef.getPrintingPolicy());
|
|
|
|
OldOStream << Typo->getName();
|
|
|
|
// If correction candidate would be an identical written qualified
|
|
|
|
// identifer, then the existing CXXScopeSpec probably included a
|
|
|
|
// typedef that didn't get accounted for properly.
|
|
|
|
if (OldOStream.str() == NewQualified)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for (LookupResult::iterator TRD = Result.begin(), TRDEnd = Result.end();
|
|
|
|
TRD != TRDEnd; ++TRD) {
|
|
|
|
if (SemaRef.CheckMemberAccess(TC.getCorrectionRange().getBegin(),
|
|
|
|
NSType ? NSType->getAsCXXRecordDecl()
|
|
|
|
: nullptr,
|
|
|
|
TRD.getPair()) == Sema::AR_accessible)
|
|
|
|
TC.addCorrectionDecl(*TRD);
|
|
|
|
}
|
2014-11-21 06:06:26 +08:00
|
|
|
if (TC.isResolved()) {
|
|
|
|
TC.setCorrectionRange(SS.get(), Result.getLookupNameInfo());
|
2014-06-12 02:07:01 +08:00
|
|
|
addCorrection(TC);
|
2014-11-21 06:06:26 +08:00
|
|
|
}
|
2014-06-12 02:07:01 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LookupResult::NotFound:
|
|
|
|
case LookupResult::NotFoundInCurrentInstantiation:
|
|
|
|
case LookupResult::Ambiguous:
|
|
|
|
case LookupResult::FoundUnresolvedValue:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
QualifiedResults.clear();
|
2011-06-29 00:20:02 +08:00
|
|
|
}
|
|
|
|
|
2014-06-12 02:33:46 +08:00
|
|
|
TypoCorrectionConsumer::NamespaceSpecifierSet::NamespaceSpecifierSet(
|
|
|
|
ASTContext &Context, DeclContext *CurContext, CXXScopeSpec *CurScopeSpec)
|
2015-03-14 00:10:42 +08:00
|
|
|
: Context(Context), CurContextChain(buildContextChain(CurContext)) {
|
2014-06-12 02:33:46 +08:00
|
|
|
if (NestedNameSpecifier *NNS =
|
|
|
|
CurScopeSpec ? CurScopeSpec->getScopeRep() : nullptr) {
|
|
|
|
llvm::raw_string_ostream SpecifierOStream(CurNameSpecifier);
|
|
|
|
NNS->print(SpecifierOStream, Context.getPrintingPolicy());
|
|
|
|
|
|
|
|
getNestedNameSpecifierIdentifiers(NNS, CurNameSpecifierIdentifiers);
|
|
|
|
}
|
|
|
|
// Build the list of identifiers that would be used for an absolute
|
|
|
|
// (from the global context) NestedNameSpecifier referring to the current
|
|
|
|
// context.
|
|
|
|
for (DeclContextList::reverse_iterator C = CurContextChain.rbegin(),
|
|
|
|
CEnd = CurContextChain.rend();
|
|
|
|
C != CEnd; ++C) {
|
|
|
|
if (NamespaceDecl *ND = dyn_cast_or_null<NamespaceDecl>(*C))
|
|
|
|
CurContextIdentifiers.push_back(ND->getIdentifier());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the global context as a NestedNameSpecifier
|
2014-06-12 05:24:13 +08:00
|
|
|
SpecifierInfo SI = {cast<DeclContext>(Context.getTranslationUnitDecl()),
|
|
|
|
NestedNameSpecifier::GlobalSpecifier(Context), 1};
|
|
|
|
DistanceMap[1].push_back(SI);
|
2014-06-12 02:33:46 +08:00
|
|
|
}
|
|
|
|
|
2014-06-12 02:07:08 +08:00
|
|
|
auto TypoCorrectionConsumer::NamespaceSpecifierSet::buildContextChain(
|
|
|
|
DeclContext *Start) -> DeclContextList {
|
2013-04-09 05:55:21 +08:00
|
|
|
assert(Start && "Building a context chain from a null context");
|
2011-06-29 00:20:02 +08:00
|
|
|
DeclContextList Chain;
|
2014-05-26 14:22:03 +08:00
|
|
|
for (DeclContext *DC = Start->getPrimaryContext(); DC != nullptr;
|
2011-06-29 00:20:02 +08:00
|
|
|
DC = DC->getLookupParent()) {
|
|
|
|
NamespaceDecl *ND = dyn_cast_or_null<NamespaceDecl>(DC);
|
|
|
|
if (!DC->isInlineNamespace() && !DC->isTransparentContext() &&
|
|
|
|
!(ND && ND->isAnonymousNamespace()))
|
|
|
|
Chain.push_back(DC->getPrimaryContext());
|
|
|
|
}
|
|
|
|
return Chain;
|
|
|
|
}
|
|
|
|
|
2014-06-12 02:07:05 +08:00
|
|
|
unsigned
|
|
|
|
TypoCorrectionConsumer::NamespaceSpecifierSet::buildNestedNameSpecifier(
|
|
|
|
DeclContextList &DeclChain, NestedNameSpecifier *&NNS) {
|
2013-10-19 08:04:52 +08:00
|
|
|
unsigned NumSpecifiers = 0;
|
|
|
|
for (DeclContextList::reverse_iterator C = DeclChain.rbegin(),
|
|
|
|
CEnd = DeclChain.rend();
|
|
|
|
C != CEnd; ++C) {
|
|
|
|
if (NamespaceDecl *ND = dyn_cast_or_null<NamespaceDecl>(*C)) {
|
|
|
|
NNS = NestedNameSpecifier::Create(Context, NNS, ND);
|
|
|
|
++NumSpecifiers;
|
|
|
|
} else if (RecordDecl *RD = dyn_cast_or_null<RecordDecl>(*C)) {
|
|
|
|
NNS = NestedNameSpecifier::Create(Context, NNS, RD->isTemplateDecl(),
|
|
|
|
RD->getTypeForDecl());
|
|
|
|
++NumSpecifiers;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NumSpecifiers;
|
|
|
|
}
|
|
|
|
|
2014-06-12 02:07:05 +08:00
|
|
|
void TypoCorrectionConsumer::NamespaceSpecifierSet::addNameSpecifier(
|
|
|
|
DeclContext *Ctx) {
|
2014-05-26 14:22:03 +08:00
|
|
|
NestedNameSpecifier *NNS = nullptr;
|
2013-09-27 03:10:29 +08:00
|
|
|
unsigned NumSpecifiers = 0;
|
2014-06-12 02:06:56 +08:00
|
|
|
DeclContextList NamespaceDeclChain(buildContextChain(Ctx));
|
2013-09-27 03:10:29 +08:00
|
|
|
DeclContextList FullNamespaceDeclChain(NamespaceDeclChain);
|
|
|
|
|
|
|
|
// Eliminate common elements from the two DeclContext chains.
|
|
|
|
for (DeclContextList::reverse_iterator C = CurContextChain.rbegin(),
|
|
|
|
CEnd = CurContextChain.rend();
|
|
|
|
C != CEnd && !NamespaceDeclChain.empty() &&
|
|
|
|
NamespaceDeclChain.back() == *C; ++C) {
|
|
|
|
NamespaceDeclChain.pop_back();
|
|
|
|
}
|
|
|
|
|
2013-10-19 08:04:52 +08:00
|
|
|
// Build the NestedNameSpecifier from what is left of the NamespaceDeclChain
|
2014-06-12 02:07:05 +08:00
|
|
|
NumSpecifiers = buildNestedNameSpecifier(NamespaceDeclChain, NNS);
|
2013-10-19 08:04:52 +08:00
|
|
|
|
2013-09-27 03:10:29 +08:00
|
|
|
// Add an explicit leading '::' specifier if needed.
|
|
|
|
if (NamespaceDeclChain.empty()) {
|
2013-10-19 08:04:52 +08:00
|
|
|
// Rebuild the NestedNameSpecifier as a globally-qualified specifier.
|
2013-09-27 03:10:29 +08:00
|
|
|
NNS = NestedNameSpecifier::GlobalSpecifier(Context);
|
2013-10-19 08:04:52 +08:00
|
|
|
NumSpecifiers =
|
2014-06-12 02:07:05 +08:00
|
|
|
buildNestedNameSpecifier(FullNamespaceDeclChain, NNS);
|
2013-10-03 02:26:35 +08:00
|
|
|
} else if (NamedDecl *ND =
|
|
|
|
dyn_cast_or_null<NamedDecl>(NamespaceDeclChain.back())) {
|
2013-09-27 03:10:29 +08:00
|
|
|
IdentifierInfo *Name = ND->getIdentifier();
|
2013-10-19 08:04:52 +08:00
|
|
|
bool SameNameSpecifier = false;
|
|
|
|
if (std::find(CurNameSpecifierIdentifiers.begin(),
|
2013-09-27 03:10:29 +08:00
|
|
|
CurNameSpecifierIdentifiers.end(),
|
|
|
|
Name) != CurNameSpecifierIdentifiers.end()) {
|
2013-10-19 08:04:52 +08:00
|
|
|
std::string NewNameSpecifier;
|
|
|
|
llvm::raw_string_ostream SpecifierOStream(NewNameSpecifier);
|
|
|
|
SmallVector<const IdentifierInfo *, 4> NewNameSpecifierIdentifiers;
|
|
|
|
getNestedNameSpecifierIdentifiers(NNS, NewNameSpecifierIdentifiers);
|
|
|
|
NNS->print(SpecifierOStream, Context.getPrintingPolicy());
|
|
|
|
SpecifierOStream.flush();
|
|
|
|
SameNameSpecifier = NewNameSpecifier == CurNameSpecifier;
|
|
|
|
}
|
|
|
|
if (SameNameSpecifier ||
|
|
|
|
std::find(CurContextIdentifiers.begin(), CurContextIdentifiers.end(),
|
|
|
|
Name) != CurContextIdentifiers.end()) {
|
|
|
|
// Rebuild the NestedNameSpecifier as a globally-qualified specifier.
|
2013-09-27 03:10:29 +08:00
|
|
|
NNS = NestedNameSpecifier::GlobalSpecifier(Context);
|
2013-10-19 08:04:52 +08:00
|
|
|
NumSpecifiers =
|
2014-06-12 02:07:05 +08:00
|
|
|
buildNestedNameSpecifier(FullNamespaceDeclChain, NNS);
|
2013-09-27 03:10:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the built NestedNameSpecifier would be replacing an existing
|
|
|
|
// NestedNameSpecifier, use the number of component identifiers that
|
|
|
|
// would need to be changed as the edit distance instead of the number
|
|
|
|
// of components in the built NestedNameSpecifier.
|
|
|
|
if (NNS && !CurNameSpecifierIdentifiers.empty()) {
|
|
|
|
SmallVector<const IdentifierInfo*, 4> NewNameSpecifierIdentifiers;
|
|
|
|
getNestedNameSpecifierIdentifiers(NNS, NewNameSpecifierIdentifiers);
|
|
|
|
NumSpecifiers = llvm::ComputeEditDistance(
|
2014-08-31 00:55:39 +08:00
|
|
|
llvm::makeArrayRef(CurNameSpecifierIdentifiers),
|
|
|
|
llvm::makeArrayRef(NewNameSpecifierIdentifiers));
|
2013-09-27 03:10:29 +08:00
|
|
|
}
|
|
|
|
|
2014-06-12 05:24:13 +08:00
|
|
|
SpecifierInfo SI = {Ctx, NNS, NumSpecifiers};
|
|
|
|
DistanceMap[NumSpecifiers].push_back(SI);
|
2013-09-27 03:10:29 +08:00
|
|
|
}
|
|
|
|
|
2010-10-20 11:06:34 +08:00
|
|
|
/// \brief Perform name lookup for a possible result for typo correction.
|
|
|
|
static void LookupPotentialTypoResult(Sema &SemaRef,
|
|
|
|
LookupResult &Res,
|
|
|
|
IdentifierInfo *Name,
|
|
|
|
Scope *S, CXXScopeSpec *SS,
|
|
|
|
DeclContext *MemberContext,
|
|
|
|
bool EnteringContext,
|
2013-08-21 04:35:18 +08:00
|
|
|
bool isObjCIvarLookup,
|
|
|
|
bool FindHidden) {
|
2010-10-20 11:06:34 +08:00
|
|
|
Res.suppressDiagnostics();
|
|
|
|
Res.clear();
|
|
|
|
Res.setLookupName(Name);
|
2013-08-21 04:35:18 +08:00
|
|
|
Res.setAllowHidden(FindHidden);
|
2011-01-27 15:10:08 +08:00
|
|
|
if (MemberContext) {
|
2010-10-20 11:06:34 +08:00
|
|
|
if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(MemberContext)) {
|
2012-01-12 03:37:46 +08:00
|
|
|
if (isObjCIvarLookup) {
|
2010-10-20 11:06:34 +08:00
|
|
|
if (ObjCIvarDecl *Ivar = Class->lookupInstanceVariable(Name)) {
|
|
|
|
Res.addDecl(Ivar);
|
|
|
|
Res.resolveKind();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2016-01-29 02:49:28 +08:00
|
|
|
if (ObjCPropertyDecl *Prop = Class->FindPropertyDeclaration(
|
|
|
|
Name, ObjCPropertyQueryKind::OBJC_PR_query_instance)) {
|
2010-10-20 11:06:34 +08:00
|
|
|
Res.addDecl(Prop);
|
|
|
|
Res.resolveKind();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2010-10-20 11:06:34 +08:00
|
|
|
SemaRef.LookupQualifiedName(Res, MemberContext);
|
|
|
|
return;
|
|
|
|
}
|
2011-01-27 15:10:08 +08:00
|
|
|
|
|
|
|
SemaRef.LookupParsedName(Res, S, SS, /*AllowBuiltinCreation=*/false,
|
2010-10-20 11:06:34 +08:00
|
|
|
EnteringContext);
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2010-10-20 11:06:34 +08:00
|
|
|
// Fake ivar lookup; this should really be part of
|
|
|
|
// LookupParsedName.
|
|
|
|
if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) {
|
|
|
|
if (Method->isInstanceMethod() && Method->getClassInterface() &&
|
2011-01-27 15:10:08 +08:00
|
|
|
(Res.empty() ||
|
2010-10-20 11:06:34 +08:00
|
|
|
(Res.isSingleResult() &&
|
|
|
|
Res.getFoundDecl()->isDefinedOutsideFunctionOrMethod()))) {
|
2011-01-27 15:10:08 +08:00
|
|
|
if (ObjCIvarDecl *IV
|
2010-10-20 11:06:34 +08:00
|
|
|
= Method->getClassInterface()->lookupInstanceVariable(Name)) {
|
|
|
|
Res.addDecl(IV);
|
|
|
|
Res.resolveKind();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-29 00:20:02 +08:00
|
|
|
/// \brief Add keywords to the consumer as possible typo corrections.
|
|
|
|
static void AddKeywordsToConsumer(Sema &SemaRef,
|
|
|
|
TypoCorrectionConsumer &Consumer,
|
2012-06-09 05:35:42 +08:00
|
|
|
Scope *S, CorrectionCandidateCallback &CCC,
|
|
|
|
bool AfterNestedNameSpecifier) {
|
|
|
|
if (AfterNestedNameSpecifier) {
|
|
|
|
// For 'X::', we know exactly which keywords can appear next.
|
|
|
|
Consumer.addKeywordResult("template");
|
|
|
|
if (CCC.WantExpressionKeywords)
|
|
|
|
Consumer.addKeywordResult("operator");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-12 03:37:46 +08:00
|
|
|
if (CCC.WantObjCSuper)
|
|
|
|
Consumer.addKeywordResult("super");
|
2011-06-29 00:20:02 +08:00
|
|
|
|
2012-01-12 03:37:46 +08:00
|
|
|
if (CCC.WantTypeSpecifiers) {
|
2011-06-29 00:20:02 +08:00
|
|
|
// Add type-specifier keywords to the set of results.
|
2013-07-15 16:24:27 +08:00
|
|
|
static const char *const CTypeSpecs[] = {
|
2011-06-29 00:20:02 +08:00
|
|
|
"char", "const", "double", "enum", "float", "int", "long", "short",
|
2011-07-02 05:27:45 +08:00
|
|
|
"signed", "struct", "union", "unsigned", "void", "volatile",
|
2011-06-29 00:20:02 +08:00
|
|
|
"_Complex", "_Imaginary",
|
|
|
|
// storage-specifiers as well
|
|
|
|
"extern", "inline", "static", "typedef"
|
|
|
|
};
|
|
|
|
|
2013-07-15 11:38:40 +08:00
|
|
|
const unsigned NumCTypeSpecs = llvm::array_lengthof(CTypeSpecs);
|
2011-06-29 00:20:02 +08:00
|
|
|
for (unsigned I = 0; I != NumCTypeSpecs; ++I)
|
|
|
|
Consumer.addKeywordResult(CTypeSpecs[I]);
|
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
if (SemaRef.getLangOpts().C99)
|
2011-06-29 00:20:02 +08:00
|
|
|
Consumer.addKeywordResult("restrict");
|
2012-03-11 15:00:24 +08:00
|
|
|
if (SemaRef.getLangOpts().Bool || SemaRef.getLangOpts().CPlusPlus)
|
2011-06-29 00:20:02 +08:00
|
|
|
Consumer.addKeywordResult("bool");
|
2012-03-11 15:00:24 +08:00
|
|
|
else if (SemaRef.getLangOpts().C99)
|
2011-07-02 05:27:45 +08:00
|
|
|
Consumer.addKeywordResult("_Bool");
|
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
if (SemaRef.getLangOpts().CPlusPlus) {
|
2011-06-29 00:20:02 +08:00
|
|
|
Consumer.addKeywordResult("class");
|
|
|
|
Consumer.addKeywordResult("typename");
|
|
|
|
Consumer.addKeywordResult("wchar_t");
|
|
|
|
|
2013-01-02 19:42:31 +08:00
|
|
|
if (SemaRef.getLangOpts().CPlusPlus11) {
|
2011-06-29 00:20:02 +08:00
|
|
|
Consumer.addKeywordResult("char16_t");
|
|
|
|
Consumer.addKeywordResult("char32_t");
|
|
|
|
Consumer.addKeywordResult("constexpr");
|
|
|
|
Consumer.addKeywordResult("decltype");
|
|
|
|
Consumer.addKeywordResult("thread_local");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
if (SemaRef.getLangOpts().GNUMode)
|
2011-06-29 00:20:02 +08:00
|
|
|
Consumer.addKeywordResult("typeof");
|
2014-07-29 02:14:02 +08:00
|
|
|
} else if (CCC.WantFunctionLikeCasts) {
|
|
|
|
static const char *const CastableTypeSpecs[] = {
|
|
|
|
"char", "double", "float", "int", "long", "short",
|
|
|
|
"signed", "unsigned", "void"
|
|
|
|
};
|
|
|
|
for (auto *kw : CastableTypeSpecs)
|
|
|
|
Consumer.addKeywordResult(kw);
|
2011-06-29 00:20:02 +08:00
|
|
|
}
|
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
if (CCC.WantCXXNamedCasts && SemaRef.getLangOpts().CPlusPlus) {
|
2011-06-29 00:20:02 +08:00
|
|
|
Consumer.addKeywordResult("const_cast");
|
|
|
|
Consumer.addKeywordResult("dynamic_cast");
|
|
|
|
Consumer.addKeywordResult("reinterpret_cast");
|
|
|
|
Consumer.addKeywordResult("static_cast");
|
|
|
|
}
|
|
|
|
|
2012-01-12 03:37:46 +08:00
|
|
|
if (CCC.WantExpressionKeywords) {
|
2011-06-29 00:20:02 +08:00
|
|
|
Consumer.addKeywordResult("sizeof");
|
2012-03-11 15:00:24 +08:00
|
|
|
if (SemaRef.getLangOpts().Bool || SemaRef.getLangOpts().CPlusPlus) {
|
2011-06-29 00:20:02 +08:00
|
|
|
Consumer.addKeywordResult("false");
|
|
|
|
Consumer.addKeywordResult("true");
|
|
|
|
}
|
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
if (SemaRef.getLangOpts().CPlusPlus) {
|
2013-07-15 16:24:27 +08:00
|
|
|
static const char *const CXXExprs[] = {
|
2011-06-29 00:20:02 +08:00
|
|
|
"delete", "new", "operator", "throw", "typeid"
|
|
|
|
};
|
2013-07-15 11:38:40 +08:00
|
|
|
const unsigned NumCXXExprs = llvm::array_lengthof(CXXExprs);
|
2011-06-29 00:20:02 +08:00
|
|
|
for (unsigned I = 0; I != NumCXXExprs; ++I)
|
|
|
|
Consumer.addKeywordResult(CXXExprs[I]);
|
|
|
|
|
|
|
|
if (isa<CXXMethodDecl>(SemaRef.CurContext) &&
|
|
|
|
cast<CXXMethodDecl>(SemaRef.CurContext)->isInstance())
|
|
|
|
Consumer.addKeywordResult("this");
|
|
|
|
|
2013-01-02 19:42:31 +08:00
|
|
|
if (SemaRef.getLangOpts().CPlusPlus11) {
|
2011-06-29 00:20:02 +08:00
|
|
|
Consumer.addKeywordResult("alignof");
|
|
|
|
Consumer.addKeywordResult("nullptr");
|
|
|
|
}
|
|
|
|
}
|
2012-07-01 05:33:57 +08:00
|
|
|
|
|
|
|
if (SemaRef.getLangOpts().C11) {
|
|
|
|
// FIXME: We should not suggest _Alignof if the alignof macro
|
|
|
|
// is present.
|
|
|
|
Consumer.addKeywordResult("_Alignof");
|
|
|
|
}
|
2011-06-29 00:20:02 +08:00
|
|
|
}
|
|
|
|
|
2012-01-12 03:37:46 +08:00
|
|
|
if (CCC.WantRemainingKeywords) {
|
2011-06-29 00:20:02 +08:00
|
|
|
if (SemaRef.getCurFunctionOrMethodDecl() || SemaRef.getCurBlock()) {
|
|
|
|
// Statements.
|
2013-07-15 16:24:27 +08:00
|
|
|
static const char *const CStmts[] = {
|
2011-06-29 00:20:02 +08:00
|
|
|
"do", "else", "for", "goto", "if", "return", "switch", "while" };
|
2013-07-15 11:38:40 +08:00
|
|
|
const unsigned NumCStmts = llvm::array_lengthof(CStmts);
|
2011-06-29 00:20:02 +08:00
|
|
|
for (unsigned I = 0; I != NumCStmts; ++I)
|
|
|
|
Consumer.addKeywordResult(CStmts[I]);
|
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
if (SemaRef.getLangOpts().CPlusPlus) {
|
2011-06-29 00:20:02 +08:00
|
|
|
Consumer.addKeywordResult("catch");
|
|
|
|
Consumer.addKeywordResult("try");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (S && S->getBreakParent())
|
|
|
|
Consumer.addKeywordResult("break");
|
|
|
|
|
|
|
|
if (S && S->getContinueParent())
|
|
|
|
Consumer.addKeywordResult("continue");
|
|
|
|
|
|
|
|
if (!SemaRef.getCurFunction()->SwitchStack.empty()) {
|
|
|
|
Consumer.addKeywordResult("case");
|
|
|
|
Consumer.addKeywordResult("default");
|
|
|
|
}
|
|
|
|
} else {
|
2012-03-11 15:00:24 +08:00
|
|
|
if (SemaRef.getLangOpts().CPlusPlus) {
|
2011-06-29 00:20:02 +08:00
|
|
|
Consumer.addKeywordResult("namespace");
|
|
|
|
Consumer.addKeywordResult("template");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (S && S->isClassScope()) {
|
|
|
|
Consumer.addKeywordResult("explicit");
|
|
|
|
Consumer.addKeywordResult("friend");
|
|
|
|
Consumer.addKeywordResult("mutable");
|
|
|
|
Consumer.addKeywordResult("private");
|
|
|
|
Consumer.addKeywordResult("protected");
|
|
|
|
Consumer.addKeywordResult("public");
|
|
|
|
Consumer.addKeywordResult("virtual");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
if (SemaRef.getLangOpts().CPlusPlus) {
|
2011-06-29 00:20:02 +08:00
|
|
|
Consumer.addKeywordResult("using");
|
|
|
|
|
2013-01-02 19:42:31 +08:00
|
|
|
if (SemaRef.getLangOpts().CPlusPlus11)
|
2011-06-29 00:20:02 +08:00
|
|
|
Consumer.addKeywordResult("static_assert");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-28 02:07:37 +08:00
|
|
|
std::unique_ptr<TypoCorrectionConsumer> Sema::makeTypoCorrectionConsumer(
|
|
|
|
const DeclarationNameInfo &TypoName, Sema::LookupNameKind LookupKind,
|
|
|
|
Scope *S, CXXScopeSpec *SS,
|
|
|
|
std::unique_ptr<CorrectionCandidateCallback> CCC,
|
|
|
|
DeclContext *MemberContext, bool EnteringContext,
|
2014-12-17 05:39:02 +08:00
|
|
|
const ObjCObjectPointerType *OPT, bool ErrorRecovery) {
|
2013-08-13 03:54:38 +08:00
|
|
|
|
2013-09-13 07:28:08 +08:00
|
|
|
if (Diags.hasFatalErrorOccurred() || !getLangOpts().SpellChecking ||
|
|
|
|
DisableTypoCorrection)
|
2014-10-28 02:07:37 +08:00
|
|
|
return nullptr;
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2011-12-03 23:55:29 +08:00
|
|
|
// In Microsoft mode, don't perform typo correction in a template member
|
|
|
|
// function dependent context because it interferes with the "lookup into
|
|
|
|
// dependent bases of class templates" feature.
|
2014-01-14 20:51:41 +08:00
|
|
|
if (getLangOpts().MSVCCompat && CurContext->isDependentContext() &&
|
2011-12-03 23:55:29 +08:00
|
|
|
isa<CXXMethodDecl>(CurContext))
|
2014-10-28 02:07:37 +08:00
|
|
|
return nullptr;
|
2011-12-03 23:55:29 +08:00
|
|
|
|
2009-12-31 01:04:44 +08:00
|
|
|
// We only attempt to correct typos for identifiers.
|
2011-06-29 00:20:02 +08:00
|
|
|
IdentifierInfo *Typo = TypoName.getName().getAsIdentifierInfo();
|
2009-12-31 01:04:44 +08:00
|
|
|
if (!Typo)
|
2014-10-28 02:07:37 +08:00
|
|
|
return nullptr;
|
2009-12-31 01:04:44 +08:00
|
|
|
|
|
|
|
// If the scope specifier itself was invalid, don't try to correct
|
|
|
|
// typos.
|
|
|
|
if (SS && SS->isInvalid())
|
2014-10-28 02:07:37 +08:00
|
|
|
return nullptr;
|
2009-12-31 01:04:44 +08:00
|
|
|
|
|
|
|
// Never try to correct typos during template deduction or
|
|
|
|
// instantiation.
|
|
|
|
if (!ActiveTemplateInstantiations.empty())
|
2014-10-28 02:07:37 +08:00
|
|
|
return nullptr;
|
2011-06-29 00:20:02 +08:00
|
|
|
|
2013-03-15 06:56:43 +08:00
|
|
|
// Don't try to correct 'super'.
|
|
|
|
if (S && S->isInObjcMethodScope() && Typo == getSuperIdentifier())
|
2014-10-28 02:07:37 +08:00
|
|
|
return nullptr;
|
2013-03-15 06:56:43 +08:00
|
|
|
|
2013-09-28 03:40:08 +08:00
|
|
|
// Abort if typo correction already failed for this specific typo.
|
|
|
|
IdentifierSourceLocations::iterator locs = TypoCorrectionFailures.find(Typo);
|
|
|
|
if (locs != TypoCorrectionFailures.end() &&
|
2013-10-06 03:56:07 +08:00
|
|
|
locs->second.count(TypoName.getLoc()))
|
2014-10-28 02:07:37 +08:00
|
|
|
return nullptr;
|
2013-09-28 03:40:08 +08:00
|
|
|
|
2013-10-03 02:26:35 +08:00
|
|
|
// Don't try to correct the identifier "vector" when in AltiVec mode.
|
|
|
|
// TODO: Figure out why typo correction misbehaves in this case, fix it, and
|
|
|
|
// remove this workaround.
|
2015-07-30 22:08:36 +08:00
|
|
|
if ((getLangOpts().AltiVec || getLangOpts().ZVector) && Typo->isStr("vector"))
|
2014-10-28 02:07:37 +08:00
|
|
|
return nullptr;
|
2013-10-03 02:26:35 +08:00
|
|
|
|
2014-12-17 05:39:02 +08:00
|
|
|
// Provide a stop gap for files that are just seriously broken. Trying
|
|
|
|
// to correct all typos can turn into a HUGE performance penalty, causing
|
|
|
|
// some files to take minutes to get rejected by the parser.
|
|
|
|
unsigned Limit = getDiagnostics().getDiagnosticOptions().SpellCheckingLimit;
|
|
|
|
if (Limit && TyposCorrected >= Limit)
|
|
|
|
return nullptr;
|
|
|
|
++TyposCorrected;
|
|
|
|
|
2014-05-08 06:47:08 +08:00
|
|
|
// If we're handling a missing symbol error, using modules, and the
|
|
|
|
// special search all modules option is used, look for a missing import.
|
2014-10-28 02:07:37 +08:00
|
|
|
if (ErrorRecovery && getLangOpts().Modules &&
|
2014-04-24 03:04:32 +08:00
|
|
|
getLangOpts().ModulesSearchAll) {
|
2014-05-08 06:47:08 +08:00
|
|
|
// The following has the side effect of loading the missing module.
|
|
|
|
getModuleLoader().lookupMissingImports(Typo->getName(),
|
|
|
|
TypoName.getLocStart());
|
2014-04-23 20:57:01 +08:00
|
|
|
}
|
|
|
|
|
2014-10-28 02:07:29 +08:00
|
|
|
CorrectionCandidateCallback &CCCRef = *CCC;
|
2014-10-28 02:07:37 +08:00
|
|
|
auto Consumer = llvm::make_unique<TypoCorrectionConsumer>(
|
|
|
|
*this, TypoName, LookupKind, S, SS, std::move(CCC), MemberContext,
|
|
|
|
EnteringContext);
|
2014-04-23 20:57:01 +08:00
|
|
|
|
2010-04-15 04:04:41 +08:00
|
|
|
// Perform name lookup to find visible, similarly-named entities.
|
2014-12-17 05:39:02 +08:00
|
|
|
bool IsUnqualifiedLookup = false;
|
2012-02-16 06:14:18 +08:00
|
|
|
DeclContext *QualifiedDC = MemberContext;
|
2010-01-04 02:01:57 +08:00
|
|
|
if (MemberContext) {
|
2014-10-28 02:07:37 +08:00
|
|
|
LookupVisibleDecls(MemberContext, LookupKind, *Consumer);
|
2010-01-04 02:01:57 +08:00
|
|
|
|
|
|
|
// Look in qualified interfaces.
|
|
|
|
if (OPT) {
|
2014-03-18 00:14:00 +08:00
|
|
|
for (auto *I : OPT->quals())
|
2014-10-28 02:07:37 +08:00
|
|
|
LookupVisibleDecls(I, LookupKind, *Consumer);
|
2010-01-04 02:01:57 +08:00
|
|
|
}
|
|
|
|
} else if (SS && SS->isSet()) {
|
2012-02-16 06:14:18 +08:00
|
|
|
QualifiedDC = computeDeclContext(*SS, EnteringContext);
|
|
|
|
if (!QualifiedDC)
|
2014-10-28 02:07:37 +08:00
|
|
|
return nullptr;
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2014-10-28 02:07:37 +08:00
|
|
|
LookupVisibleDecls(QualifiedDC, LookupKind, *Consumer);
|
2009-12-31 01:04:44 +08:00
|
|
|
} else {
|
2010-10-20 09:32:02 +08:00
|
|
|
IsUnqualifiedLookup = true;
|
2012-02-16 06:14:18 +08:00
|
|
|
}
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2012-03-27 00:54:18 +08:00
|
|
|
// Determine whether we are going to search in the various namespaces for
|
|
|
|
// corrections.
|
|
|
|
bool SearchNamespaces
|
2012-04-04 02:20:11 +08:00
|
|
|
= getLangOpts().CPlusPlus &&
|
2013-10-19 08:05:00 +08:00
|
|
|
(IsUnqualifiedLookup || (SS && SS->isSet()));
|
2013-10-03 02:26:35 +08:00
|
|
|
|
2012-03-27 00:54:18 +08:00
|
|
|
if (IsUnqualifiedLookup || SearchNamespaces) {
|
2012-01-24 04:18:59 +08:00
|
|
|
// For unqualified lookup, look through all of the names that we have
|
|
|
|
// seen in this translation unit.
|
2012-02-07 09:32:58 +08:00
|
|
|
// FIXME: Re-add the ability to skip very unlikely potential corrections.
|
2014-07-16 06:03:49 +08:00
|
|
|
for (const auto &I : Context.Idents)
|
2014-10-28 02:07:37 +08:00
|
|
|
Consumer->FoundName(I.getKey());
|
2010-10-15 06:11:03 +08:00
|
|
|
|
2012-01-24 04:18:59 +08:00
|
|
|
// Walk through identifiers in external identifier sources.
|
2012-02-07 09:32:58 +08:00
|
|
|
// FIXME: Re-add the ability to skip very unlikely potential corrections.
|
2012-01-24 04:18:59 +08:00
|
|
|
if (IdentifierInfoLookup *External
|
|
|
|
= Context.Idents.getExternalIdentifierLookup()) {
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<IdentifierIterator> Iter(External->getIdentifiers());
|
2012-01-24 04:18:59 +08:00
|
|
|
do {
|
|
|
|
StringRef Name = Iter->Next();
|
|
|
|
if (Name.empty())
|
|
|
|
break;
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2014-10-28 02:07:37 +08:00
|
|
|
Consumer->FoundName(Name);
|
2012-01-24 04:18:59 +08:00
|
|
|
} while (true);
|
2010-10-15 06:11:03 +08:00
|
|
|
}
|
2009-12-31 01:04:44 +08:00
|
|
|
}
|
|
|
|
|
2014-10-28 02:07:37 +08:00
|
|
|
AddKeywordsToConsumer(*this, *Consumer, S, CCCRef, SS && SS->isNotEmpty());
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2012-03-27 00:54:18 +08:00
|
|
|
// Build the NestedNameSpecifiers for the KnownNamespaces, if we're going
|
|
|
|
// to search those namespaces.
|
|
|
|
if (SearchNamespaces) {
|
2011-06-29 00:20:02 +08:00
|
|
|
// Load any externally-known namespaces.
|
|
|
|
if (ExternalSource && !LoadedExternalKnownNamespaces) {
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<NamespaceDecl *, 4> ExternalKnownNamespaces;
|
2011-06-29 00:20:02 +08:00
|
|
|
LoadedExternalKnownNamespaces = true;
|
|
|
|
ExternalSource->ReadKnownNamespaces(ExternalKnownNamespaces);
|
2014-07-16 06:03:49 +08:00
|
|
|
for (auto *N : ExternalKnownNamespaces)
|
|
|
|
KnownNamespaces[N] = true;
|
2011-06-29 00:20:02 +08:00
|
|
|
}
|
2013-09-27 03:10:29 +08:00
|
|
|
|
2014-10-28 02:07:37 +08:00
|
|
|
Consumer->addNamespaces(KnownNamespaces);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Consumer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Try to "correct" a typo in the source code by finding
|
|
|
|
/// visible declarations whose names are similar to the name that was
|
|
|
|
/// present in the source code.
|
|
|
|
///
|
|
|
|
/// \param TypoName the \c DeclarationNameInfo structure that contains
|
|
|
|
/// the name that was present in the source code along with its location.
|
|
|
|
///
|
|
|
|
/// \param LookupKind the name-lookup criteria used to search for the name.
|
|
|
|
///
|
|
|
|
/// \param S the scope in which name lookup occurs.
|
|
|
|
///
|
|
|
|
/// \param SS the nested-name-specifier that precedes the name we're
|
|
|
|
/// looking for, if present.
|
|
|
|
///
|
|
|
|
/// \param CCC A CorrectionCandidateCallback object that provides further
|
|
|
|
/// validation of typo correction candidates. It also provides flags for
|
|
|
|
/// determining the set of keywords permitted.
|
|
|
|
///
|
|
|
|
/// \param MemberContext if non-NULL, the context in which to look for
|
|
|
|
/// a member access expression.
|
|
|
|
///
|
|
|
|
/// \param EnteringContext whether we're entering the context described by
|
|
|
|
/// the nested-name-specifier SS.
|
|
|
|
///
|
|
|
|
/// \param OPT when non-NULL, the search for visible declarations will
|
|
|
|
/// also walk the protocols in the qualified interfaces of \p OPT.
|
|
|
|
///
|
|
|
|
/// \returns a \c TypoCorrection containing the corrected name if the typo
|
|
|
|
/// along with information such as the \c NamedDecl where the corrected name
|
|
|
|
/// was declared, and any additional \c NestedNameSpecifier needed to access
|
|
|
|
/// it (C++ only). The \c TypoCorrection is empty if there is no correction.
|
|
|
|
TypoCorrection Sema::CorrectTypo(const DeclarationNameInfo &TypoName,
|
|
|
|
Sema::LookupNameKind LookupKind,
|
|
|
|
Scope *S, CXXScopeSpec *SS,
|
|
|
|
std::unique_ptr<CorrectionCandidateCallback> CCC,
|
|
|
|
CorrectTypoKind Mode,
|
|
|
|
DeclContext *MemberContext,
|
|
|
|
bool EnteringContext,
|
|
|
|
const ObjCObjectPointerType *OPT,
|
|
|
|
bool RecordFailure) {
|
|
|
|
assert(CCC && "CorrectTypo requires a CorrectionCandidateCallback");
|
|
|
|
|
|
|
|
// Always let the ExternalSource have the first chance at correction, even
|
|
|
|
// if we would otherwise have given up.
|
|
|
|
if (ExternalSource) {
|
|
|
|
if (TypoCorrection Correction = ExternalSource->CorrectTypo(
|
|
|
|
TypoName, LookupKind, S, SS, *CCC, MemberContext, EnteringContext, OPT))
|
|
|
|
return Correction;
|
2010-10-20 09:32:02 +08:00
|
|
|
}
|
2009-12-31 01:04:44 +08:00
|
|
|
|
2014-10-28 02:07:37 +08:00
|
|
|
// Ugly hack equivalent to CTC == CTC_ObjCMessageReceiver;
|
|
|
|
// WantObjCSuper is only true for CTC_ObjCMessageReceiver and for
|
|
|
|
// some instances of CTC_Unknown, while WantRemainingKeywords is true
|
|
|
|
// for CTC_Unknown but not for CTC_ObjCMessageReceiver.
|
|
|
|
bool ObjCMessageReceiver = CCC->WantObjCSuper && !CCC->WantRemainingKeywords;
|
|
|
|
|
|
|
|
IdentifierInfo *Typo = TypoName.getName().getAsIdentifierInfo();
|
|
|
|
auto Consumer = makeTypoCorrectionConsumer(
|
|
|
|
TypoName, LookupKind, S, SS, std::move(CCC), MemberContext,
|
2014-12-17 05:39:02 +08:00
|
|
|
EnteringContext, OPT, Mode == CTK_ErrorRecovery);
|
2014-10-28 02:07:37 +08:00
|
|
|
|
|
|
|
if (!Consumer)
|
|
|
|
return TypoCorrection();
|
|
|
|
|
|
|
|
// If we haven't found anything, we're done.
|
|
|
|
if (Consumer->empty())
|
2014-12-17 05:39:02 +08:00
|
|
|
return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure);
|
2014-10-28 02:07:37 +08:00
|
|
|
|
|
|
|
// Make sure the best edit distance (prior to adding any namespace qualifiers)
|
|
|
|
// is not more that about a third of the length of the typo's identifier.
|
|
|
|
unsigned ED = Consumer->getBestEditDistance(true);
|
|
|
|
unsigned TypoLen = Typo->getName().size();
|
|
|
|
if (ED > 0 && TypoLen / ED < 3)
|
2014-12-17 05:39:02 +08:00
|
|
|
return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure);
|
2014-10-28 02:07:37 +08:00
|
|
|
|
|
|
|
TypoCorrection BestTC = Consumer->getNextCorrection();
|
|
|
|
TypoCorrection SecondBestTC = Consumer->getNextCorrection();
|
2014-06-12 02:07:01 +08:00
|
|
|
if (!BestTC)
|
2013-09-28 03:40:08 +08:00
|
|
|
return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure);
|
2011-06-29 00:20:02 +08:00
|
|
|
|
2014-06-12 02:07:01 +08:00
|
|
|
ED = BestTC.getEditDistance();
|
2010-10-27 01:18:00 +08:00
|
|
|
|
2014-10-28 02:07:37 +08:00
|
|
|
if (TypoLen >= 3 && ED > 0 && TypoLen / ED < 3) {
|
2012-01-24 04:18:59 +08:00
|
|
|
// If this was an unqualified lookup and we believe the callback
|
|
|
|
// object wouldn't have filtered out possible corrections, note
|
|
|
|
// that no correction was found.
|
2014-12-17 05:39:02 +08:00
|
|
|
return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure);
|
2010-10-20 09:32:02 +08:00
|
|
|
}
|
2010-10-15 04:34:08 +08:00
|
|
|
|
|
|
|
// If only a single name remains, return that result.
|
2014-06-12 02:07:01 +08:00
|
|
|
if (!SecondBestTC ||
|
|
|
|
SecondBestTC.getEditDistance(false) > BestTC.getEditDistance(false)) {
|
|
|
|
const TypoCorrection &Result = BestTC;
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2011-06-29 00:20:02 +08:00
|
|
|
// Don't correct to a keyword that's the same as the typo; the keyword
|
|
|
|
// wasn't actually in scope.
|
2013-09-28 03:40:08 +08:00
|
|
|
if (ED == 0 && Result.isKeyword())
|
|
|
|
return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure);
|
2010-10-16 00:49:56 +08:00
|
|
|
|
2012-10-13 04:00:44 +08:00
|
|
|
TypoCorrection TC = Result;
|
|
|
|
TC.setCorrectionRange(SS, TypoName);
|
2014-06-18 07:47:29 +08:00
|
|
|
checkCorrectionVisibility(*this, TC);
|
2012-10-13 04:00:44 +08:00
|
|
|
return TC;
|
2014-10-28 02:07:37 +08:00
|
|
|
} else if (SecondBestTC && ObjCMessageReceiver) {
|
2011-06-29 00:20:02 +08:00
|
|
|
// Prefer 'super' when we're completing in a message-receiver
|
2010-10-15 21:35:25 +08:00
|
|
|
// context.
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2014-06-12 02:07:01 +08:00
|
|
|
if (BestTC.getCorrection().getAsString() != "super") {
|
|
|
|
if (SecondBestTC.getCorrection().getAsString() == "super")
|
|
|
|
BestTC = SecondBestTC;
|
2014-10-28 02:07:37 +08:00
|
|
|
else if ((*Consumer)["super"].front().isKeyword())
|
|
|
|
BestTC = (*Consumer)["super"].front();
|
2014-06-12 02:07:01 +08:00
|
|
|
}
|
2010-10-27 01:18:00 +08:00
|
|
|
// Don't correct to a keyword that's the same as the typo; the keyword
|
|
|
|
// wasn't actually in scope.
|
2014-06-12 02:07:01 +08:00
|
|
|
if (BestTC.getEditDistance() == 0 ||
|
|
|
|
BestTC.getCorrection().getAsString() != "super")
|
2013-09-28 03:40:08 +08:00
|
|
|
return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure);
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2014-06-12 02:07:01 +08:00
|
|
|
BestTC.setCorrectionRange(SS, TypoName);
|
|
|
|
return BestTC;
|
2010-10-15 21:35:25 +08:00
|
|
|
}
|
2011-01-27 15:10:08 +08:00
|
|
|
|
2014-06-11 05:03:49 +08:00
|
|
|
// Record the failure's location if needed and return an empty correction. If
|
|
|
|
// this was an unqualified lookup and we believe the callback object did not
|
|
|
|
// filter out possible corrections, also cache the failure for the typo.
|
2015-01-17 06:11:04 +08:00
|
|
|
return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure && !SecondBestTC);
|
2011-06-29 00:20:02 +08:00
|
|
|
}
|
|
|
|
|
2014-10-28 02:07:37 +08:00
|
|
|
/// \brief Try to "correct" a typo in the source code by finding
|
|
|
|
/// visible declarations whose names are similar to the name that was
|
|
|
|
/// present in the source code.
|
|
|
|
///
|
|
|
|
/// \param TypoName the \c DeclarationNameInfo structure that contains
|
|
|
|
/// the name that was present in the source code along with its location.
|
|
|
|
///
|
|
|
|
/// \param LookupKind the name-lookup criteria used to search for the name.
|
|
|
|
///
|
|
|
|
/// \param S the scope in which name lookup occurs.
|
|
|
|
///
|
|
|
|
/// \param SS the nested-name-specifier that precedes the name we're
|
|
|
|
/// looking for, if present.
|
|
|
|
///
|
|
|
|
/// \param CCC A CorrectionCandidateCallback object that provides further
|
|
|
|
/// validation of typo correction candidates. It also provides flags for
|
|
|
|
/// determining the set of keywords permitted.
|
|
|
|
///
|
|
|
|
/// \param TDG A TypoDiagnosticGenerator functor that will be used to print
|
|
|
|
/// diagnostics when the actual typo correction is attempted.
|
|
|
|
///
|
2014-10-28 02:07:42 +08:00
|
|
|
/// \param TRC A TypoRecoveryCallback functor that will be used to build an
|
|
|
|
/// Expr from a typo correction candidate.
|
|
|
|
///
|
2014-10-28 02:07:37 +08:00
|
|
|
/// \param MemberContext if non-NULL, the context in which to look for
|
|
|
|
/// a member access expression.
|
|
|
|
///
|
|
|
|
/// \param EnteringContext whether we're entering the context described by
|
|
|
|
/// the nested-name-specifier SS.
|
|
|
|
///
|
|
|
|
/// \param OPT when non-NULL, the search for visible declarations will
|
|
|
|
/// also walk the protocols in the qualified interfaces of \p OPT.
|
|
|
|
///
|
|
|
|
/// \returns a new \c TypoExpr that will later be replaced in the AST with an
|
|
|
|
/// Expr representing the result of performing typo correction, or nullptr if
|
|
|
|
/// typo correction is not possible. If nullptr is returned, no diagnostics will
|
|
|
|
/// be emitted and it is the responsibility of the caller to emit any that are
|
|
|
|
/// needed.
|
|
|
|
TypoExpr *Sema::CorrectTypoDelayed(
|
|
|
|
const DeclarationNameInfo &TypoName, Sema::LookupNameKind LookupKind,
|
|
|
|
Scope *S, CXXScopeSpec *SS,
|
|
|
|
std::unique_ptr<CorrectionCandidateCallback> CCC,
|
2014-10-28 02:07:42 +08:00
|
|
|
TypoDiagnosticGenerator TDG, TypoRecoveryCallback TRC, CorrectTypoKind Mode,
|
2014-10-28 02:07:37 +08:00
|
|
|
DeclContext *MemberContext, bool EnteringContext,
|
|
|
|
const ObjCObjectPointerType *OPT) {
|
|
|
|
assert(CCC && "CorrectTypoDelayed requires a CorrectionCandidateCallback");
|
|
|
|
|
|
|
|
TypoCorrection Empty;
|
|
|
|
auto Consumer = makeTypoCorrectionConsumer(
|
|
|
|
TypoName, LookupKind, S, SS, std::move(CCC), MemberContext,
|
2015-01-17 06:11:04 +08:00
|
|
|
EnteringContext, OPT, Mode == CTK_ErrorRecovery);
|
2014-10-28 02:07:37 +08:00
|
|
|
|
|
|
|
if (!Consumer || Consumer->empty())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// Make sure the best edit distance (prior to adding any namespace qualifiers)
|
|
|
|
// is not more that about a third of the length of the typo's identifier.
|
|
|
|
unsigned ED = Consumer->getBestEditDistance(true);
|
|
|
|
IdentifierInfo *Typo = TypoName.getName().getAsIdentifierInfo();
|
|
|
|
if (ED > 0 && Typo->getName().size() / ED < 3)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
ExprEvalContexts.back().NumTypos++;
|
2014-10-28 02:07:42 +08:00
|
|
|
return createDelayedTypo(std::move(Consumer), std::move(TDG), std::move(TRC));
|
2014-10-28 02:07:37 +08:00
|
|
|
}
|
|
|
|
|
2011-08-04 04:36:05 +08:00
|
|
|
void TypoCorrection::addCorrectionDecl(NamedDecl *CDecl) {
|
|
|
|
if (!CDecl) return;
|
|
|
|
|
|
|
|
if (isKeyword())
|
|
|
|
CorrectionDecls.clear();
|
|
|
|
|
2015-12-30 03:43:10 +08:00
|
|
|
CorrectionDecls.push_back(CDecl);
|
2011-08-04 04:36:05 +08:00
|
|
|
|
|
|
|
if (!CorrectionName)
|
|
|
|
CorrectionName = CDecl->getDeclName();
|
|
|
|
}
|
|
|
|
|
2011-06-29 00:20:02 +08:00
|
|
|
std::string TypoCorrection::getAsString(const LangOptions &LO) const {
|
|
|
|
if (CorrectionNameSpec) {
|
|
|
|
std::string tmpBuffer;
|
|
|
|
llvm::raw_string_ostream PrefixOStream(tmpBuffer);
|
|
|
|
CorrectionNameSpec->print(PrefixOStream, PrintingPolicy(LO));
|
2013-05-15 05:04:00 +08:00
|
|
|
PrefixOStream << CorrectionName;
|
2012-04-14 16:26:28 +08:00
|
|
|
return PrefixOStream.str();
|
2011-06-29 00:20:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return CorrectionName.getAsString();
|
2009-12-31 01:04:44 +08:00
|
|
|
}
|
2013-04-04 00:59:49 +08:00
|
|
|
|
2014-11-19 13:21:39 +08:00
|
|
|
bool CorrectionCandidateCallback::ValidateCandidate(
|
|
|
|
const TypoCorrection &candidate) {
|
2013-04-04 00:59:49 +08:00
|
|
|
if (!candidate.isResolved())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (candidate.isKeyword())
|
|
|
|
return WantTypeSpecifiers || WantExpressionKeywords || WantCXXNamedCasts ||
|
|
|
|
WantRemainingKeywords || WantObjCSuper;
|
|
|
|
|
2014-06-24 06:57:51 +08:00
|
|
|
bool HasNonType = false;
|
|
|
|
bool HasStaticMethod = false;
|
|
|
|
bool HasNonStaticMethod = false;
|
|
|
|
for (Decl *D : candidate) {
|
|
|
|
if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D))
|
|
|
|
D = FTD->getTemplatedDecl();
|
|
|
|
if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
|
|
|
|
if (Method->isStatic())
|
|
|
|
HasStaticMethod = true;
|
|
|
|
else
|
|
|
|
HasNonStaticMethod = true;
|
|
|
|
}
|
|
|
|
if (!isa<TypeDecl>(D))
|
|
|
|
HasNonType = true;
|
2013-04-04 00:59:49 +08:00
|
|
|
}
|
|
|
|
|
2014-06-24 06:57:51 +08:00
|
|
|
if (IsAddressOfOperand && HasNonStaticMethod && !HasStaticMethod &&
|
|
|
|
!candidate.getCorrectionSpecifier())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return WantTypeSpecifiers || HasNonType;
|
2013-04-04 00:59:49 +08:00
|
|
|
}
|
2013-07-09 07:13:39 +08:00
|
|
|
|
|
|
|
FunctionCallFilterCCC::FunctionCallFilterCCC(Sema &SemaRef, unsigned NumArgs,
|
2014-03-01 02:12:42 +08:00
|
|
|
bool HasExplicitTemplateArgs,
|
2014-04-05 06:16:30 +08:00
|
|
|
MemberExpr *ME)
|
2014-03-01 02:12:42 +08:00
|
|
|
: NumArgs(NumArgs), HasExplicitTemplateArgs(HasExplicitTemplateArgs),
|
2014-04-05 06:16:30 +08:00
|
|
|
CurContext(SemaRef.CurContext), MemberFn(ME) {
|
2014-07-29 02:14:02 +08:00
|
|
|
WantTypeSpecifiers = false;
|
|
|
|
WantFunctionLikeCasts = SemaRef.getLangOpts().CPlusPlus && NumArgs == 1;
|
2013-07-09 07:13:39 +08:00
|
|
|
WantRemainingKeywords = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FunctionCallFilterCCC::ValidateCandidate(const TypoCorrection &candidate) {
|
|
|
|
if (!candidate.getCorrectionDecl())
|
|
|
|
return candidate.isKeyword();
|
|
|
|
|
2014-07-16 06:03:49 +08:00
|
|
|
for (auto *C : candidate) {
|
2014-05-26 14:22:03 +08:00
|
|
|
FunctionDecl *FD = nullptr;
|
2014-07-16 06:03:49 +08:00
|
|
|
NamedDecl *ND = C->getUnderlyingDecl();
|
2013-07-09 07:13:39 +08:00
|
|
|
if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
|
|
|
|
FD = FTD->getTemplatedDecl();
|
|
|
|
if (!HasExplicitTemplateArgs && !FD) {
|
|
|
|
if (!(FD = dyn_cast<FunctionDecl>(ND)) && isa<ValueDecl>(ND)) {
|
|
|
|
// If the Decl is neither a function nor a template function,
|
|
|
|
// determine if it is a pointer or reference to a function. If so,
|
|
|
|
// check against the number of arguments expected for the pointee.
|
|
|
|
QualType ValType = cast<ValueDecl>(ND)->getType();
|
|
|
|
if (ValType->isAnyPointerType() || ValType->isReferenceType())
|
|
|
|
ValType = ValType->getPointeeType();
|
|
|
|
if (const FunctionProtoType *FPT = ValType->getAs<FunctionProtoType>())
|
2014-01-21 04:26:09 +08:00
|
|
|
if (FPT->getNumParams() == NumArgs)
|
2013-07-09 07:13:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2014-03-01 02:12:42 +08:00
|
|
|
|
|
|
|
// Skip the current candidate if it is not a FunctionDecl or does not accept
|
|
|
|
// the current number of arguments.
|
|
|
|
if (!FD || !(FD->getNumParams() >= NumArgs &&
|
|
|
|
FD->getMinRequiredArguments() <= NumArgs))
|
|
|
|
continue;
|
|
|
|
|
2014-04-05 06:16:30 +08:00
|
|
|
// If the current candidate is a non-static C++ method, skip the candidate
|
|
|
|
// unless the method being corrected--or the current DeclContext, if the
|
|
|
|
// function being corrected is not a method--is a method in the same class
|
|
|
|
// or a descendent class of the candidate's parent class.
|
2014-03-01 02:12:42 +08:00
|
|
|
if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
|
2014-04-05 06:16:30 +08:00
|
|
|
if (MemberFn || !MD->isStatic()) {
|
|
|
|
CXXMethodDecl *CurMD =
|
|
|
|
MemberFn
|
|
|
|
? dyn_cast_or_null<CXXMethodDecl>(MemberFn->getMemberDecl())
|
|
|
|
: dyn_cast_or_null<CXXMethodDecl>(CurContext);
|
2014-03-01 02:12:42 +08:00
|
|
|
CXXRecordDecl *CurRD =
|
2014-05-26 14:22:03 +08:00
|
|
|
CurMD ? CurMD->getParent()->getCanonicalDecl() : nullptr;
|
2014-03-01 02:12:42 +08:00
|
|
|
CXXRecordDecl *RD = MD->getParent()->getCanonicalDecl();
|
|
|
|
if (!CurRD || (CurRD != RD && !CurRD->isDerivedFrom(RD)))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2013-07-09 07:13:39 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2013-08-17 08:46:16 +08:00
|
|
|
|
|
|
|
void Sema::diagnoseTypo(const TypoCorrection &Correction,
|
|
|
|
const PartialDiagnostic &TypoDiag,
|
|
|
|
bool ErrorRecovery) {
|
|
|
|
diagnoseTypo(Correction, TypoDiag, PDiag(diag::note_previous_decl),
|
|
|
|
ErrorRecovery);
|
|
|
|
}
|
|
|
|
|
2013-08-21 04:35:18 +08:00
|
|
|
/// Find which declaration we should import to provide the definition of
|
|
|
|
/// the given declaration.
|
2015-05-16 04:05:43 +08:00
|
|
|
static NamedDecl *getDefinitionToImport(NamedDecl *D) {
|
|
|
|
if (VarDecl *VD = dyn_cast<VarDecl>(D))
|
2013-08-21 04:35:18 +08:00
|
|
|
return VD->getDefinition();
|
|
|
|
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
|
2015-05-16 04:05:43 +08:00
|
|
|
return FD->isDefined(FD) ? const_cast<FunctionDecl*>(FD) : nullptr;
|
|
|
|
if (TagDecl *TD = dyn_cast<TagDecl>(D))
|
2013-08-21 04:35:18 +08:00
|
|
|
return TD->getDefinition();
|
2015-05-16 04:05:43 +08:00
|
|
|
if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D))
|
2013-08-21 04:35:18 +08:00
|
|
|
return ID->getDefinition();
|
2015-05-16 04:05:43 +08:00
|
|
|
if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D))
|
2013-08-21 04:35:18 +08:00
|
|
|
return PD->getDefinition();
|
2015-05-16 04:05:43 +08:00
|
|
|
if (TemplateDecl *TD = dyn_cast<TemplateDecl>(D))
|
2013-08-21 04:35:18 +08:00
|
|
|
return getDefinitionToImport(TD->getTemplatedDecl());
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2013-08-21 04:35:18 +08:00
|
|
|
}
|
|
|
|
|
2015-06-16 04:15:48 +08:00
|
|
|
void Sema::diagnoseMissingImport(SourceLocation Loc, NamedDecl *Decl,
|
|
|
|
bool NeedDefinition, bool Recover) {
|
|
|
|
assert(!isVisible(Decl) && "missing import for non-hidden decl?");
|
|
|
|
|
|
|
|
// Suggest importing a module providing the definition of this entity, if
|
|
|
|
// possible.
|
|
|
|
NamedDecl *Def = getDefinitionToImport(Decl);
|
|
|
|
if (!Def)
|
|
|
|
Def = Decl;
|
|
|
|
|
|
|
|
// FIXME: Add a Fix-It that imports the corresponding module or includes
|
|
|
|
// the header.
|
|
|
|
Module *Owner = getOwningModule(Decl);
|
|
|
|
assert(Owner && "definition of hidden declaration is not in a module");
|
|
|
|
|
2015-06-18 04:16:32 +08:00
|
|
|
llvm::SmallVector<Module*, 8> OwningModules;
|
|
|
|
OwningModules.push_back(Owner);
|
2015-06-16 04:15:48 +08:00
|
|
|
auto Merged = Context.getModulesWithMergedDefinition(Decl);
|
2015-06-18 04:16:32 +08:00
|
|
|
OwningModules.insert(OwningModules.end(), Merged.begin(), Merged.end());
|
|
|
|
|
|
|
|
diagnoseMissingImport(Loc, Decl, Decl->getLocation(), OwningModules,
|
|
|
|
NeedDefinition ? MissingImportKind::Definition
|
|
|
|
: MissingImportKind::Declaration,
|
|
|
|
Recover);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sema::diagnoseMissingImport(SourceLocation UseLoc, NamedDecl *Decl,
|
|
|
|
SourceLocation DeclLoc,
|
|
|
|
ArrayRef<Module *> Modules,
|
|
|
|
MissingImportKind MIK, bool Recover) {
|
|
|
|
assert(!Modules.empty());
|
|
|
|
|
|
|
|
if (Modules.size() > 1) {
|
2015-06-16 04:15:48 +08:00
|
|
|
std::string ModuleList;
|
|
|
|
unsigned N = 0;
|
2015-06-18 04:16:32 +08:00
|
|
|
for (Module *M : Modules) {
|
2015-06-16 04:15:48 +08:00
|
|
|
ModuleList += "\n ";
|
2015-06-18 04:16:32 +08:00
|
|
|
if (++N == 5 && N != Modules.size()) {
|
2015-06-16 04:15:48 +08:00
|
|
|
ModuleList += "[...]";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ModuleList += M->getFullModuleName();
|
|
|
|
}
|
|
|
|
|
2015-06-18 04:16:32 +08:00
|
|
|
Diag(UseLoc, diag::err_module_unimported_use_multiple)
|
|
|
|
<< (int)MIK << Decl << ModuleList;
|
2015-06-16 04:15:48 +08:00
|
|
|
} else {
|
2015-06-18 04:16:32 +08:00
|
|
|
Diag(UseLoc, diag::err_module_unimported_use)
|
|
|
|
<< (int)MIK << Decl << Modules[0]->getFullModuleName();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned DiagID;
|
|
|
|
switch (MIK) {
|
|
|
|
case MissingImportKind::Declaration:
|
|
|
|
DiagID = diag::note_previous_declaration;
|
|
|
|
break;
|
|
|
|
case MissingImportKind::Definition:
|
|
|
|
DiagID = diag::note_previous_definition;
|
|
|
|
break;
|
|
|
|
case MissingImportKind::DefaultArgument:
|
|
|
|
DiagID = diag::note_default_argument_declared_here;
|
|
|
|
break;
|
2015-06-16 04:15:48 +08:00
|
|
|
}
|
2015-06-18 04:16:32 +08:00
|
|
|
Diag(DeclLoc, DiagID);
|
2015-06-16 04:15:48 +08:00
|
|
|
|
|
|
|
// Try to recover by implicitly importing this module.
|
|
|
|
if (Recover)
|
2015-06-18 04:16:32 +08:00
|
|
|
createImplicitModuleImportForErrorRecovery(UseLoc, Modules[0]);
|
2015-06-16 04:15:48 +08:00
|
|
|
}
|
|
|
|
|
2013-08-17 08:46:16 +08:00
|
|
|
/// \brief Diagnose a successfully-corrected typo. Separated from the correction
|
|
|
|
/// itself to allow external validation of the result, etc.
|
|
|
|
///
|
|
|
|
/// \param Correction The result of performing typo correction.
|
|
|
|
/// \param TypoDiag The diagnostic to produce. This will have the corrected
|
|
|
|
/// string added to it (and usually also a fixit).
|
|
|
|
/// \param PrevNote A note to use when indicating the location of the entity to
|
|
|
|
/// which we are correcting. Will have the correction string added to it.
|
|
|
|
/// \param ErrorRecovery If \c true (the default), the caller is going to
|
|
|
|
/// recover from the typo as if the corrected string had been typed.
|
|
|
|
/// In this case, \c PDiag must be an error, and we will attach a fixit
|
|
|
|
/// to it.
|
|
|
|
void Sema::diagnoseTypo(const TypoCorrection &Correction,
|
|
|
|
const PartialDiagnostic &TypoDiag,
|
|
|
|
const PartialDiagnostic &PrevNote,
|
|
|
|
bool ErrorRecovery) {
|
|
|
|
std::string CorrectedStr = Correction.getAsString(getLangOpts());
|
|
|
|
std::string CorrectedQuotedStr = Correction.getQuoted(getLangOpts());
|
|
|
|
FixItHint FixTypo = FixItHint::CreateReplacement(
|
|
|
|
Correction.getCorrectionRange(), CorrectedStr);
|
|
|
|
|
2013-08-21 04:35:18 +08:00
|
|
|
// Maybe we're just missing a module import.
|
|
|
|
if (Correction.requiresImport()) {
|
2015-12-30 03:43:10 +08:00
|
|
|
NamedDecl *Decl = Correction.getFoundDecl();
|
2013-08-21 04:35:18 +08:00
|
|
|
assert(Decl && "import required but no declaration to import");
|
|
|
|
|
2015-06-16 04:15:48 +08:00
|
|
|
diagnoseMissingImport(Correction.getCorrectionRange().getBegin(), Decl,
|
|
|
|
/*NeedDefinition*/ false, ErrorRecovery);
|
2013-08-21 04:35:18 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-17 08:46:16 +08:00
|
|
|
Diag(Correction.getCorrectionRange().getBegin(), TypoDiag)
|
|
|
|
<< CorrectedQuotedStr << (ErrorRecovery ? FixTypo : FixItHint());
|
|
|
|
|
|
|
|
NamedDecl *ChosenDecl =
|
2015-12-30 03:43:10 +08:00
|
|
|
Correction.isKeyword() ? nullptr : Correction.getFoundDecl();
|
2013-08-17 08:46:16 +08:00
|
|
|
if (PrevNote.getDiagID() && ChosenDecl)
|
|
|
|
Diag(ChosenDecl->getLocation(), PrevNote)
|
|
|
|
<< CorrectedQuotedStr << (ErrorRecovery ? FixItHint() : FixTypo);
|
|
|
|
}
|
2014-10-28 02:07:37 +08:00
|
|
|
|
2014-10-28 02:07:42 +08:00
|
|
|
TypoExpr *Sema::createDelayedTypo(std::unique_ptr<TypoCorrectionConsumer> TCC,
|
|
|
|
TypoDiagnosticGenerator TDG,
|
|
|
|
TypoRecoveryCallback TRC) {
|
2014-10-28 02:07:37 +08:00
|
|
|
assert(TCC && "createDelayedTypo requires a valid TypoCorrectionConsumer");
|
|
|
|
auto TE = new (Context) TypoExpr(Context.DependentTy);
|
|
|
|
auto &State = DelayedTypos[TE];
|
|
|
|
State.Consumer = std::move(TCC);
|
|
|
|
State.DiagHandler = std::move(TDG);
|
2014-10-28 02:07:42 +08:00
|
|
|
State.RecoveryHandler = std::move(TRC);
|
2014-10-28 02:07:37 +08:00
|
|
|
return TE;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Sema::TypoExprState &Sema::getTypoExprState(TypoExpr *TE) const {
|
|
|
|
auto Entry = DelayedTypos.find(TE);
|
|
|
|
assert(Entry != DelayedTypos.end() &&
|
|
|
|
"Failed to get the state for a TypoExpr!");
|
|
|
|
return Entry->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sema::clearDelayedTypo(TypoExpr *TE) {
|
|
|
|
DelayedTypos.erase(TE);
|
|
|
|
}
|
2016-01-13 05:59:26 +08:00
|
|
|
|
|
|
|
void Sema::ActOnPragmaDump(Scope *S, SourceLocation IILoc, IdentifierInfo *II) {
|
|
|
|
DeclarationNameInfo Name(II, IILoc);
|
|
|
|
LookupResult R(*this, Name, LookupAnyName, Sema::NotForRedeclaration);
|
|
|
|
R.suppressDiagnostics();
|
|
|
|
R.setHideTags(false);
|
|
|
|
LookupName(R, S);
|
|
|
|
R.dump();
|
|
|
|
}
|