| File: | build/source/clang/lib/Sema/SemaLookup.cpp |
| Warning: | line 4926, column 7 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | //===--------------------- SemaLookup.cpp - Name Lookup ------------------===// | |||
| 2 | // | |||
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |||
| 4 | // See https://llvm.org/LICENSE.txt for license information. | |||
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |||
| 6 | // | |||
| 7 | //===----------------------------------------------------------------------===// | |||
| 8 | // | |||
| 9 | // This file implements name lookup for C, C++, Objective-C, and | |||
| 10 | // Objective-C++. | |||
| 11 | // | |||
| 12 | //===----------------------------------------------------------------------===// | |||
| 13 | ||||
| 14 | #include "clang/AST/ASTContext.h" | |||
| 15 | #include "clang/AST/CXXInheritance.h" | |||
| 16 | #include "clang/AST/Decl.h" | |||
| 17 | #include "clang/AST/DeclCXX.h" | |||
| 18 | #include "clang/AST/DeclLookups.h" | |||
| 19 | #include "clang/AST/DeclObjC.h" | |||
| 20 | #include "clang/AST/DeclTemplate.h" | |||
| 21 | #include "clang/AST/Expr.h" | |||
| 22 | #include "clang/AST/ExprCXX.h" | |||
| 23 | #include "clang/Basic/Builtins.h" | |||
| 24 | #include "clang/Basic/FileManager.h" | |||
| 25 | #include "clang/Basic/LangOptions.h" | |||
| 26 | #include "clang/Lex/HeaderSearch.h" | |||
| 27 | #include "clang/Lex/ModuleLoader.h" | |||
| 28 | #include "clang/Lex/Preprocessor.h" | |||
| 29 | #include "clang/Sema/DeclSpec.h" | |||
| 30 | #include "clang/Sema/Lookup.h" | |||
| 31 | #include "clang/Sema/Overload.h" | |||
| 32 | #include "clang/Sema/RISCVIntrinsicManager.h" | |||
| 33 | #include "clang/Sema/Scope.h" | |||
| 34 | #include "clang/Sema/ScopeInfo.h" | |||
| 35 | #include "clang/Sema/Sema.h" | |||
| 36 | #include "clang/Sema/SemaInternal.h" | |||
| 37 | #include "clang/Sema/TemplateDeduction.h" | |||
| 38 | #include "clang/Sema/TypoCorrection.h" | |||
| 39 | #include "llvm/ADT/STLExtras.h" | |||
| 40 | #include "llvm/ADT/SmallPtrSet.h" | |||
| 41 | #include "llvm/ADT/TinyPtrVector.h" | |||
| 42 | #include "llvm/ADT/edit_distance.h" | |||
| 43 | #include "llvm/Support/Casting.h" | |||
| 44 | #include "llvm/Support/ErrorHandling.h" | |||
| 45 | #include <algorithm> | |||
| 46 | #include <iterator> | |||
| 47 | #include <list> | |||
| 48 | #include <optional> | |||
| 49 | #include <set> | |||
| 50 | #include <utility> | |||
| 51 | #include <vector> | |||
| 52 | ||||
| 53 | #include "OpenCLBuiltins.inc" | |||
| 54 | ||||
| 55 | using namespace clang; | |||
| 56 | using namespace sema; | |||
| 57 | ||||
| 58 | namespace { | |||
| 59 | class UnqualUsingEntry { | |||
| 60 | const DeclContext *Nominated; | |||
| 61 | const DeclContext *CommonAncestor; | |||
| 62 | ||||
| 63 | public: | |||
| 64 | UnqualUsingEntry(const DeclContext *Nominated, | |||
| 65 | const DeclContext *CommonAncestor) | |||
| 66 | : Nominated(Nominated), CommonAncestor(CommonAncestor) { | |||
| 67 | } | |||
| 68 | ||||
| 69 | const DeclContext *getCommonAncestor() const { | |||
| 70 | return CommonAncestor; | |||
| 71 | } | |||
| 72 | ||||
| 73 | const DeclContext *getNominatedNamespace() const { | |||
| 74 | return Nominated; | |||
| 75 | } | |||
| 76 | ||||
| 77 | // Sort by the pointer value of the common ancestor. | |||
| 78 | struct Comparator { | |||
| 79 | bool operator()(const UnqualUsingEntry &L, const UnqualUsingEntry &R) { | |||
| 80 | return L.getCommonAncestor() < R.getCommonAncestor(); | |||
| 81 | } | |||
| 82 | ||||
| 83 | bool operator()(const UnqualUsingEntry &E, const DeclContext *DC) { | |||
| 84 | return E.getCommonAncestor() < DC; | |||
| 85 | } | |||
| 86 | ||||
| 87 | bool operator()(const DeclContext *DC, const UnqualUsingEntry &E) { | |||
| 88 | return DC < E.getCommonAncestor(); | |||
| 89 | } | |||
| 90 | }; | |||
| 91 | }; | |||
| 92 | ||||
| 93 | /// A collection of using directives, as used by C++ unqualified | |||
| 94 | /// lookup. | |||
| 95 | class UnqualUsingDirectiveSet { | |||
| 96 | Sema &SemaRef; | |||
| 97 | ||||
| 98 | typedef SmallVector<UnqualUsingEntry, 8> ListTy; | |||
| 99 | ||||
| 100 | ListTy list; | |||
| 101 | llvm::SmallPtrSet<DeclContext*, 8> visited; | |||
| 102 | ||||
| 103 | public: | |||
| 104 | UnqualUsingDirectiveSet(Sema &SemaRef) : SemaRef(SemaRef) {} | |||
| 105 | ||||
| 106 | void visitScopeChain(Scope *S, Scope *InnermostFileScope) { | |||
| 107 | // C++ [namespace.udir]p1: | |||
| 108 | // During unqualified name lookup, the names appear as if they | |||
| 109 | // were declared in the nearest enclosing namespace which contains | |||
| 110 | // both the using-directive and the nominated namespace. | |||
| 111 | DeclContext *InnermostFileDC = InnermostFileScope->getEntity(); | |||
| 112 | assert(InnermostFileDC && InnermostFileDC->isFileContext())(static_cast <bool> (InnermostFileDC && InnermostFileDC ->isFileContext()) ? void (0) : __assert_fail ("InnermostFileDC && InnermostFileDC->isFileContext()" , "clang/lib/Sema/SemaLookup.cpp", 112, __extension__ __PRETTY_FUNCTION__ )); | |||
| 113 | ||||
| 114 | for (; S; S = S->getParent()) { | |||
| 115 | // C++ [namespace.udir]p1: | |||
| 116 | // A using-directive shall not appear in class scope, but may | |||
| 117 | // appear in namespace scope or in block scope. | |||
| 118 | DeclContext *Ctx = S->getEntity(); | |||
| 119 | if (Ctx && Ctx->isFileContext()) { | |||
| 120 | visit(Ctx, Ctx); | |||
| 121 | } else if (!Ctx || Ctx->isFunctionOrMethod()) { | |||
| 122 | for (auto *I : S->using_directives()) | |||
| 123 | if (SemaRef.isVisible(I)) | |||
| 124 | visit(I, InnermostFileDC); | |||
| 125 | } | |||
| 126 | } | |||
| 127 | } | |||
| 128 | ||||
| 129 | // Visits a context and collect all of its using directives | |||
| 130 | // recursively. Treats all using directives as if they were | |||
| 131 | // declared in the context. | |||
| 132 | // | |||
| 133 | // A given context is only every visited once, so it is important | |||
| 134 | // that contexts be visited from the inside out in order to get | |||
| 135 | // the effective DCs right. | |||
| 136 | void visit(DeclContext *DC, DeclContext *EffectiveDC) { | |||
| 137 | if (!visited.insert(DC).second) | |||
| 138 | return; | |||
| 139 | ||||
| 140 | addUsingDirectives(DC, EffectiveDC); | |||
| 141 | } | |||
| 142 | ||||
| 143 | // Visits a using directive and collects all of its using | |||
| 144 | // directives recursively. Treats all using directives as if they | |||
| 145 | // were declared in the effective DC. | |||
| 146 | void visit(UsingDirectiveDecl *UD, DeclContext *EffectiveDC) { | |||
| 147 | DeclContext *NS = UD->getNominatedNamespace(); | |||
| 148 | if (!visited.insert(NS).second) | |||
| 149 | return; | |||
| 150 | ||||
| 151 | addUsingDirective(UD, EffectiveDC); | |||
| 152 | addUsingDirectives(NS, EffectiveDC); | |||
| 153 | } | |||
| 154 | ||||
| 155 | // Adds all the using directives in a context (and those nominated | |||
| 156 | // by its using directives, transitively) as if they appeared in | |||
| 157 | // the given effective context. | |||
| 158 | void addUsingDirectives(DeclContext *DC, DeclContext *EffectiveDC) { | |||
| 159 | SmallVector<DeclContext*, 4> queue; | |||
| 160 | while (true) { | |||
| 161 | for (auto *UD : DC->using_directives()) { | |||
| 162 | DeclContext *NS = UD->getNominatedNamespace(); | |||
| 163 | if (SemaRef.isVisible(UD) && visited.insert(NS).second) { | |||
| 164 | addUsingDirective(UD, EffectiveDC); | |||
| 165 | queue.push_back(NS); | |||
| 166 | } | |||
| 167 | } | |||
| 168 | ||||
| 169 | if (queue.empty()) | |||
| 170 | return; | |||
| 171 | ||||
| 172 | DC = queue.pop_back_val(); | |||
| 173 | } | |||
| 174 | } | |||
| 175 | ||||
| 176 | // Add a using directive as if it had been declared in the given | |||
| 177 | // context. This helps implement C++ [namespace.udir]p3: | |||
| 178 | // The using-directive is transitive: if a scope contains a | |||
| 179 | // using-directive that nominates a second namespace that itself | |||
| 180 | // contains using-directives, the effect is as if the | |||
| 181 | // using-directives from the second namespace also appeared in | |||
| 182 | // the first. | |||
| 183 | void addUsingDirective(UsingDirectiveDecl *UD, DeclContext *EffectiveDC) { | |||
| 184 | // Find the common ancestor between the effective context and | |||
| 185 | // the nominated namespace. | |||
| 186 | DeclContext *Common = UD->getNominatedNamespace(); | |||
| 187 | while (!Common->Encloses(EffectiveDC)) | |||
| 188 | Common = Common->getParent(); | |||
| 189 | Common = Common->getPrimaryContext(); | |||
| 190 | ||||
| 191 | list.push_back(UnqualUsingEntry(UD->getNominatedNamespace(), Common)); | |||
| 192 | } | |||
| 193 | ||||
| 194 | void done() { llvm::sort(list, UnqualUsingEntry::Comparator()); } | |||
| 195 | ||||
| 196 | typedef ListTy::const_iterator const_iterator; | |||
| 197 | ||||
| 198 | const_iterator begin() const { return list.begin(); } | |||
| 199 | const_iterator end() const { return list.end(); } | |||
| 200 | ||||
| 201 | llvm::iterator_range<const_iterator> | |||
| 202 | getNamespacesFor(const DeclContext *DC) const { | |||
| 203 | return llvm::make_range(std::equal_range(begin(), end(), | |||
| 204 | DC->getPrimaryContext(), | |||
| 205 | UnqualUsingEntry::Comparator())); | |||
| 206 | } | |||
| 207 | }; | |||
| 208 | } // end anonymous namespace | |||
| 209 | ||||
| 210 | // Retrieve the set of identifier namespaces that correspond to a | |||
| 211 | // specific kind of name lookup. | |||
| 212 | static inline unsigned getIDNS(Sema::LookupNameKind NameKind, | |||
| 213 | bool CPlusPlus, | |||
| 214 | bool Redeclaration) { | |||
| 215 | unsigned IDNS = 0; | |||
| 216 | switch (NameKind) { | |||
| 217 | case Sema::LookupObjCImplicitSelfParam: | |||
| 218 | case Sema::LookupOrdinaryName: | |||
| 219 | case Sema::LookupRedeclarationWithLinkage: | |||
| 220 | case Sema::LookupLocalFriendName: | |||
| 221 | case Sema::LookupDestructorName: | |||
| 222 | IDNS = Decl::IDNS_Ordinary; | |||
| 223 | if (CPlusPlus) { | |||
| 224 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Member | Decl::IDNS_Namespace; | |||
| 225 | if (Redeclaration) | |||
| 226 | IDNS |= Decl::IDNS_TagFriend | Decl::IDNS_OrdinaryFriend; | |||
| 227 | } | |||
| 228 | if (Redeclaration) | |||
| 229 | IDNS |= Decl::IDNS_LocalExtern; | |||
| 230 | break; | |||
| 231 | ||||
| 232 | case Sema::LookupOperatorName: | |||
| 233 | // Operator lookup is its own crazy thing; it is not the same | |||
| 234 | // as (e.g.) looking up an operator name for redeclaration. | |||
| 235 | assert(!Redeclaration && "cannot do redeclaration operator lookup")(static_cast <bool> (!Redeclaration && "cannot do redeclaration operator lookup" ) ? void (0) : __assert_fail ("!Redeclaration && \"cannot do redeclaration operator lookup\"" , "clang/lib/Sema/SemaLookup.cpp", 235, __extension__ __PRETTY_FUNCTION__ )); | |||
| 236 | IDNS = Decl::IDNS_NonMemberOperator; | |||
| 237 | break; | |||
| 238 | ||||
| 239 | case Sema::LookupTagName: | |||
| 240 | if (CPlusPlus) { | |||
| 241 | IDNS = Decl::IDNS_Type; | |||
| 242 | ||||
| 243 | // When looking for a redeclaration of a tag name, we add: | |||
| 244 | // 1) TagFriend to find undeclared friend decls | |||
| 245 | // 2) Namespace because they can't "overload" with tag decls. | |||
| 246 | // 3) Tag because it includes class templates, which can't | |||
| 247 | // "overload" with tag decls. | |||
| 248 | if (Redeclaration) | |||
| 249 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_TagFriend | Decl::IDNS_Namespace; | |||
| 250 | } else { | |||
| 251 | IDNS = Decl::IDNS_Tag; | |||
| 252 | } | |||
| 253 | break; | |||
| 254 | ||||
| 255 | case Sema::LookupLabel: | |||
| 256 | IDNS = Decl::IDNS_Label; | |||
| 257 | break; | |||
| 258 | ||||
| 259 | case Sema::LookupMemberName: | |||
| 260 | IDNS = Decl::IDNS_Member; | |||
| 261 | if (CPlusPlus) | |||
| 262 | IDNS |= Decl::IDNS_Tag | Decl::IDNS_Ordinary; | |||
| 263 | break; | |||
| 264 | ||||
| 265 | case Sema::LookupNestedNameSpecifierName: | |||
| 266 | IDNS = Decl::IDNS_Type | Decl::IDNS_Namespace; | |||
| 267 | break; | |||
| 268 | ||||
| 269 | case Sema::LookupNamespaceName: | |||
| 270 | IDNS = Decl::IDNS_Namespace; | |||
| 271 | break; | |||
| 272 | ||||
| 273 | case Sema::LookupUsingDeclName: | |||
| 274 | assert(Redeclaration && "should only be used for redecl lookup")(static_cast <bool> (Redeclaration && "should only be used for redecl lookup" ) ? void (0) : __assert_fail ("Redeclaration && \"should only be used for redecl lookup\"" , "clang/lib/Sema/SemaLookup.cpp", 274, __extension__ __PRETTY_FUNCTION__ )); | |||
| 275 | IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Member | | |||
| 276 | Decl::IDNS_Using | Decl::IDNS_TagFriend | Decl::IDNS_OrdinaryFriend | | |||
| 277 | Decl::IDNS_LocalExtern; | |||
| 278 | break; | |||
| 279 | ||||
| 280 | case Sema::LookupObjCProtocolName: | |||
| 281 | IDNS = Decl::IDNS_ObjCProtocol; | |||
| 282 | break; | |||
| 283 | ||||
| 284 | case Sema::LookupOMPReductionName: | |||
| 285 | IDNS = Decl::IDNS_OMPReduction; | |||
| 286 | break; | |||
| 287 | ||||
| 288 | case Sema::LookupOMPMapperName: | |||
| 289 | IDNS = Decl::IDNS_OMPMapper; | |||
| 290 | break; | |||
| 291 | ||||
| 292 | case Sema::LookupAnyName: | |||
| 293 | IDNS = Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Member | |||
| 294 | | Decl::IDNS_Using | Decl::IDNS_Namespace | Decl::IDNS_ObjCProtocol | |||
| 295 | | Decl::IDNS_Type; | |||
| 296 | break; | |||
| 297 | } | |||
| 298 | return IDNS; | |||
| 299 | } | |||
| 300 | ||||
| 301 | void LookupResult::configure() { | |||
| 302 | IDNS = getIDNS(LookupKind, getSema().getLangOpts().CPlusPlus, | |||
| 303 | isForRedeclaration()); | |||
| 304 | ||||
| 305 | // If we're looking for one of the allocation or deallocation | |||
| 306 | // operators, make sure that the implicitly-declared new and delete | |||
| 307 | // operators can be found. | |||
| 308 | switch (NameInfo.getName().getCXXOverloadedOperator()) { | |||
| 309 | case OO_New: | |||
| 310 | case OO_Delete: | |||
| 311 | case OO_Array_New: | |||
| 312 | case OO_Array_Delete: | |||
| 313 | getSema().DeclareGlobalNewDelete(); | |||
| 314 | break; | |||
| 315 | ||||
| 316 | default: | |||
| 317 | break; | |||
| 318 | } | |||
| 319 | ||||
| 320 | // Compiler builtins are always visible, regardless of where they end | |||
| 321 | // up being declared. | |||
| 322 | if (IdentifierInfo *Id = NameInfo.getName().getAsIdentifierInfo()) { | |||
| 323 | if (unsigned BuiltinID = Id->getBuiltinID()) { | |||
| 324 | if (!getSema().Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) | |||
| 325 | AllowHidden = true; | |||
| 326 | } | |||
| 327 | } | |||
| 328 | } | |||
| 329 | ||||
| 330 | bool LookupResult::checkDebugAssumptions() const { | |||
| 331 | // This function is never called by NDEBUG builds. | |||
| 332 | assert(ResultKind != NotFound || Decls.size() == 0)(static_cast <bool> (ResultKind != NotFound || Decls.size () == 0) ? void (0) : __assert_fail ("ResultKind != NotFound || Decls.size() == 0" , "clang/lib/Sema/SemaLookup.cpp", 332, __extension__ __PRETTY_FUNCTION__ )); | |||
| 333 | assert(ResultKind != Found || Decls.size() == 1)(static_cast <bool> (ResultKind != Found || Decls.size( ) == 1) ? void (0) : __assert_fail ("ResultKind != Found || Decls.size() == 1" , "clang/lib/Sema/SemaLookup.cpp", 333, __extension__ __PRETTY_FUNCTION__ )); | |||
| 334 | assert(ResultKind != FoundOverloaded || Decls.size() > 1 ||(static_cast <bool> (ResultKind != FoundOverloaded || Decls .size() > 1 || (Decls.size() == 1 && isa<FunctionTemplateDecl >((*begin())->getUnderlyingDecl()))) ? void (0) : __assert_fail ("ResultKind != FoundOverloaded || Decls.size() > 1 || (Decls.size() == 1 && isa<FunctionTemplateDecl>((*begin())->getUnderlyingDecl()))" , "clang/lib/Sema/SemaLookup.cpp", 336, __extension__ __PRETTY_FUNCTION__ )) | |||
| 335 | (Decls.size() == 1 &&(static_cast <bool> (ResultKind != FoundOverloaded || Decls .size() > 1 || (Decls.size() == 1 && isa<FunctionTemplateDecl >((*begin())->getUnderlyingDecl()))) ? void (0) : __assert_fail ("ResultKind != FoundOverloaded || Decls.size() > 1 || (Decls.size() == 1 && isa<FunctionTemplateDecl>((*begin())->getUnderlyingDecl()))" , "clang/lib/Sema/SemaLookup.cpp", 336, __extension__ __PRETTY_FUNCTION__ )) | |||
| 336 | isa<FunctionTemplateDecl>((*begin())->getUnderlyingDecl())))(static_cast <bool> (ResultKind != FoundOverloaded || Decls .size() > 1 || (Decls.size() == 1 && isa<FunctionTemplateDecl >((*begin())->getUnderlyingDecl()))) ? void (0) : __assert_fail ("ResultKind != FoundOverloaded || Decls.size() > 1 || (Decls.size() == 1 && isa<FunctionTemplateDecl>((*begin())->getUnderlyingDecl()))" , "clang/lib/Sema/SemaLookup.cpp", 336, __extension__ __PRETTY_FUNCTION__ )); | |||
| 337 | assert(ResultKind != FoundUnresolvedValue || checkUnresolved())(static_cast <bool> (ResultKind != FoundUnresolvedValue || checkUnresolved()) ? void (0) : __assert_fail ("ResultKind != FoundUnresolvedValue || checkUnresolved()" , "clang/lib/Sema/SemaLookup.cpp", 337, __extension__ __PRETTY_FUNCTION__ )); | |||
| 338 | assert(ResultKind != Ambiguous || Decls.size() > 1 ||(static_cast <bool> (ResultKind != Ambiguous || Decls.size () > 1 || (Decls.size() == 1 && (Ambiguity == AmbiguousBaseSubobjects || Ambiguity == AmbiguousBaseSubobjectTypes))) ? void (0) : __assert_fail ("ResultKind != Ambiguous || Decls.size() > 1 || (Decls.size() == 1 && (Ambiguity == AmbiguousBaseSubobjects || Ambiguity == AmbiguousBaseSubobjectTypes))" , "clang/lib/Sema/SemaLookup.cpp", 340, __extension__ __PRETTY_FUNCTION__ )) | |||
| 339 | (Decls.size() == 1 && (Ambiguity == AmbiguousBaseSubobjects ||(static_cast <bool> (ResultKind != Ambiguous || Decls.size () > 1 || (Decls.size() == 1 && (Ambiguity == AmbiguousBaseSubobjects || Ambiguity == AmbiguousBaseSubobjectTypes))) ? void (0) : __assert_fail ("ResultKind != Ambiguous || Decls.size() > 1 || (Decls.size() == 1 && (Ambiguity == AmbiguousBaseSubobjects || Ambiguity == AmbiguousBaseSubobjectTypes))" , "clang/lib/Sema/SemaLookup.cpp", 340, __extension__ __PRETTY_FUNCTION__ )) | |||
| 340 | Ambiguity == AmbiguousBaseSubobjectTypes)))(static_cast <bool> (ResultKind != Ambiguous || Decls.size () > 1 || (Decls.size() == 1 && (Ambiguity == AmbiguousBaseSubobjects || Ambiguity == AmbiguousBaseSubobjectTypes))) ? void (0) : __assert_fail ("ResultKind != Ambiguous || Decls.size() > 1 || (Decls.size() == 1 && (Ambiguity == AmbiguousBaseSubobjects || Ambiguity == AmbiguousBaseSubobjectTypes))" , "clang/lib/Sema/SemaLookup.cpp", 340, __extension__ __PRETTY_FUNCTION__ )); | |||
| 341 | assert((Paths != nullptr) == (ResultKind == Ambiguous &&(static_cast <bool> ((Paths != nullptr) == (ResultKind == Ambiguous && (Ambiguity == AmbiguousBaseSubobjectTypes || Ambiguity == AmbiguousBaseSubobjects))) ? void (0) : __assert_fail ("(Paths != nullptr) == (ResultKind == Ambiguous && (Ambiguity == AmbiguousBaseSubobjectTypes || Ambiguity == AmbiguousBaseSubobjects))" , "clang/lib/Sema/SemaLookup.cpp", 343, __extension__ __PRETTY_FUNCTION__ )) | |||
| 342 | (Ambiguity == AmbiguousBaseSubobjectTypes ||(static_cast <bool> ((Paths != nullptr) == (ResultKind == Ambiguous && (Ambiguity == AmbiguousBaseSubobjectTypes || Ambiguity == AmbiguousBaseSubobjects))) ? void (0) : __assert_fail ("(Paths != nullptr) == (ResultKind == Ambiguous && (Ambiguity == AmbiguousBaseSubobjectTypes || Ambiguity == AmbiguousBaseSubobjects))" , "clang/lib/Sema/SemaLookup.cpp", 343, __extension__ __PRETTY_FUNCTION__ )) | |||
| 343 | Ambiguity == AmbiguousBaseSubobjects)))(static_cast <bool> ((Paths != nullptr) == (ResultKind == Ambiguous && (Ambiguity == AmbiguousBaseSubobjectTypes || Ambiguity == AmbiguousBaseSubobjects))) ? void (0) : __assert_fail ("(Paths != nullptr) == (ResultKind == Ambiguous && (Ambiguity == AmbiguousBaseSubobjectTypes || Ambiguity == AmbiguousBaseSubobjects))" , "clang/lib/Sema/SemaLookup.cpp", 343, __extension__ __PRETTY_FUNCTION__ )); | |||
| 344 | return true; | |||
| 345 | } | |||
| 346 | ||||
| 347 | // Necessary because CXXBasePaths is not complete in Sema.h | |||
| 348 | void LookupResult::deletePaths(CXXBasePaths *Paths) { | |||
| 349 | delete Paths; | |||
| 350 | } | |||
| 351 | ||||
| 352 | /// Get a representative context for a declaration such that two declarations | |||
| 353 | /// will have the same context if they were found within the same scope. | |||
| 354 | static const DeclContext *getContextForScopeMatching(const Decl *D) { | |||
| 355 | // For function-local declarations, use that function as the context. This | |||
| 356 | // doesn't account for scopes within the function; the caller must deal with | |||
| 357 | // those. | |||
| 358 | if (const DeclContext *DC = D->getLexicalDeclContext(); | |||
| 359 | DC->isFunctionOrMethod()) | |||
| 360 | return DC; | |||
| 361 | ||||
| 362 | // Otherwise, look at the semantic context of the declaration. The | |||
| 363 | // declaration must have been found there. | |||
| 364 | return D->getDeclContext()->getRedeclContext(); | |||
| 365 | } | |||
| 366 | ||||
| 367 | /// Determine whether \p D is a better lookup result than \p Existing, | |||
| 368 | /// given that they declare the same entity. | |||
| 369 | static bool isPreferredLookupResult(Sema &S, Sema::LookupNameKind Kind, | |||
| 370 | const NamedDecl *D, | |||
| 371 | const NamedDecl *Existing) { | |||
| 372 | // When looking up redeclarations of a using declaration, prefer a using | |||
| 373 | // shadow declaration over any other declaration of the same entity. | |||
| 374 | if (Kind == Sema::LookupUsingDeclName && isa<UsingShadowDecl>(D) && | |||
| 375 | !isa<UsingShadowDecl>(Existing)) | |||
| 376 | return true; | |||
| 377 | ||||
| 378 | const auto *DUnderlying = D->getUnderlyingDecl(); | |||
| 379 | const auto *EUnderlying = Existing->getUnderlyingDecl(); | |||
| 380 | ||||
| 381 | // If they have different underlying declarations, prefer a typedef over the | |||
| 382 | // original type (this happens when two type declarations denote the same | |||
| 383 | // type), per a generous reading of C++ [dcl.typedef]p3 and p4. The typedef | |||
| 384 | // might carry additional semantic information, such as an alignment override. | |||
| 385 | // However, per C++ [dcl.typedef]p5, when looking up a tag name, prefer a tag | |||
| 386 | // declaration over a typedef. Also prefer a tag over a typedef for | |||
| 387 | // destructor name lookup because in some contexts we only accept a | |||
| 388 | // class-name in a destructor declaration. | |||
| 389 | if (DUnderlying->getCanonicalDecl() != EUnderlying->getCanonicalDecl()) { | |||
| 390 | assert(isa<TypeDecl>(DUnderlying) && isa<TypeDecl>(EUnderlying))(static_cast <bool> (isa<TypeDecl>(DUnderlying) && isa<TypeDecl>(EUnderlying)) ? void (0) : __assert_fail ("isa<TypeDecl>(DUnderlying) && isa<TypeDecl>(EUnderlying)" , "clang/lib/Sema/SemaLookup.cpp", 390, __extension__ __PRETTY_FUNCTION__ )); | |||
| 391 | bool HaveTag = isa<TagDecl>(EUnderlying); | |||
| 392 | bool WantTag = | |||
| 393 | Kind == Sema::LookupTagName || Kind == Sema::LookupDestructorName; | |||
| 394 | return HaveTag != WantTag; | |||
| 395 | } | |||
| 396 | ||||
| 397 | // Pick the function with more default arguments. | |||
| 398 | // FIXME: In the presence of ambiguous default arguments, we should keep both, | |||
| 399 | // so we can diagnose the ambiguity if the default argument is needed. | |||
| 400 | // See C++ [over.match.best]p3. | |||
| 401 | if (const auto *DFD = dyn_cast<FunctionDecl>(DUnderlying)) { | |||
| 402 | const auto *EFD = cast<FunctionDecl>(EUnderlying); | |||
| 403 | unsigned DMin = DFD->getMinRequiredArguments(); | |||
| 404 | unsigned EMin = EFD->getMinRequiredArguments(); | |||
| 405 | // If D has more default arguments, it is preferred. | |||
| 406 | if (DMin != EMin) | |||
| 407 | return DMin < EMin; | |||
| 408 | // FIXME: When we track visibility for default function arguments, check | |||
| 409 | // that we pick the declaration with more visible default arguments. | |||
| 410 | } | |||
| 411 | ||||
| 412 | // Pick the template with more default template arguments. | |||
| 413 | if (const auto *DTD = dyn_cast<TemplateDecl>(DUnderlying)) { | |||
| 414 | const auto *ETD = cast<TemplateDecl>(EUnderlying); | |||
| 415 | unsigned DMin = DTD->getTemplateParameters()->getMinRequiredArguments(); | |||
| 416 | unsigned EMin = ETD->getTemplateParameters()->getMinRequiredArguments(); | |||
| 417 | // If D has more default arguments, it is preferred. Note that default | |||
| 418 | // arguments (and their visibility) is monotonically increasing across the | |||
| 419 | // redeclaration chain, so this is a quick proxy for "is more recent". | |||
| 420 | if (DMin != EMin) | |||
| 421 | return DMin < EMin; | |||
| 422 | // If D has more *visible* default arguments, it is preferred. Note, an | |||
| 423 | // earlier default argument being visible does not imply that a later | |||
| 424 | // default argument is visible, so we can't just check the first one. | |||
| 425 | for (unsigned I = DMin, N = DTD->getTemplateParameters()->size(); | |||
| 426 | I != N; ++I) { | |||
| 427 | if (!S.hasVisibleDefaultArgument( | |||
| 428 | ETD->getTemplateParameters()->getParam(I)) && | |||
| 429 | S.hasVisibleDefaultArgument( | |||
| 430 | DTD->getTemplateParameters()->getParam(I))) | |||
| 431 | return true; | |||
| 432 | } | |||
| 433 | } | |||
| 434 | ||||
| 435 | // VarDecl can have incomplete array types, prefer the one with more complete | |||
| 436 | // array type. | |||
| 437 | if (const auto *DVD = dyn_cast<VarDecl>(DUnderlying)) { | |||
| 438 | const auto *EVD = cast<VarDecl>(EUnderlying); | |||
| 439 | if (EVD->getType()->isIncompleteType() && | |||
| 440 | !DVD->getType()->isIncompleteType()) { | |||
| 441 | // Prefer the decl with a more complete type if visible. | |||
| 442 | return S.isVisible(DVD); | |||
| 443 | } | |||
| 444 | return false; // Avoid picking up a newer decl, just because it was newer. | |||
| 445 | } | |||
| 446 | ||||
| 447 | // For most kinds of declaration, it doesn't really matter which one we pick. | |||
| 448 | if (!isa<FunctionDecl>(DUnderlying) && !isa<VarDecl>(DUnderlying)) { | |||
| 449 | // If the existing declaration is hidden, prefer the new one. Otherwise, | |||
| 450 | // keep what we've got. | |||
| 451 | return !S.isVisible(Existing); | |||
| 452 | } | |||
| 453 | ||||
| 454 | // Pick the newer declaration; it might have a more precise type. | |||
| 455 | for (const Decl *Prev = DUnderlying->getPreviousDecl(); Prev; | |||
| 456 | Prev = Prev->getPreviousDecl()) | |||
| 457 | if (Prev == EUnderlying) | |||
| 458 | return true; | |||
| 459 | return false; | |||
| 460 | } | |||
| 461 | ||||
| 462 | /// Determine whether \p D can hide a tag declaration. | |||
| 463 | static bool canHideTag(const NamedDecl *D) { | |||
| 464 | // C++ [basic.scope.declarative]p4: | |||
| 465 | // Given a set of declarations in a single declarative region [...] | |||
| 466 | // exactly one declaration shall declare a class name or enumeration name | |||
| 467 | // that is not a typedef name and the other declarations shall all refer to | |||
| 468 | // the same variable, non-static data member, or enumerator, or all refer | |||
| 469 | // to functions and function templates; in this case the class name or | |||
| 470 | // enumeration name is hidden. | |||
| 471 | // C++ [basic.scope.hiding]p2: | |||
| 472 | // A class name or enumeration name can be hidden by the name of a | |||
| 473 | // variable, data member, function, or enumerator declared in the same | |||
| 474 | // scope. | |||
| 475 | // An UnresolvedUsingValueDecl always instantiates to one of these. | |||
| 476 | D = D->getUnderlyingDecl(); | |||
| 477 | return isa<VarDecl>(D) || isa<EnumConstantDecl>(D) || isa<FunctionDecl>(D) || | |||
| 478 | isa<FunctionTemplateDecl>(D) || isa<FieldDecl>(D) || | |||
| 479 | isa<UnresolvedUsingValueDecl>(D); | |||
| 480 | } | |||
| 481 | ||||
| 482 | /// Resolves the result kind of this lookup. | |||
| 483 | void LookupResult::resolveKind() { | |||
| 484 | unsigned N = Decls.size(); | |||
| 485 | ||||
| 486 | // Fast case: no possible ambiguity. | |||
| 487 | if (N == 0) { | |||
| 488 | assert(ResultKind == NotFound ||(static_cast <bool> (ResultKind == NotFound || ResultKind == NotFoundInCurrentInstantiation) ? void (0) : __assert_fail ("ResultKind == NotFound || ResultKind == NotFoundInCurrentInstantiation" , "clang/lib/Sema/SemaLookup.cpp", 489, __extension__ __PRETTY_FUNCTION__ )) | |||
| 489 | ResultKind == NotFoundInCurrentInstantiation)(static_cast <bool> (ResultKind == NotFound || ResultKind == NotFoundInCurrentInstantiation) ? void (0) : __assert_fail ("ResultKind == NotFound || ResultKind == NotFoundInCurrentInstantiation" , "clang/lib/Sema/SemaLookup.cpp", 489, __extension__ __PRETTY_FUNCTION__ )); | |||
| 490 | return; | |||
| 491 | } | |||
| 492 | ||||
| 493 | // If there's a single decl, we need to examine it to decide what | |||
| 494 | // kind of lookup this is. | |||
| 495 | if (N == 1) { | |||
| 496 | const NamedDecl *D = (*Decls.begin())->getUnderlyingDecl(); | |||
| 497 | if (isa<FunctionTemplateDecl>(D)) | |||
| 498 | ResultKind = FoundOverloaded; | |||
| 499 | else if (isa<UnresolvedUsingValueDecl>(D)) | |||
| 500 | ResultKind = FoundUnresolvedValue; | |||
| 501 | return; | |||
| 502 | } | |||
| 503 | ||||
| 504 | // Don't do any extra resolution if we've already resolved as ambiguous. | |||
| 505 | if (ResultKind == Ambiguous) return; | |||
| 506 | ||||
| 507 | llvm::SmallDenseMap<const NamedDecl *, unsigned, 16> Unique; | |||
| 508 | llvm::SmallDenseMap<QualType, unsigned, 16> UniqueTypes; | |||
| 509 | ||||
| 510 | bool Ambiguous = false; | |||
| 511 | bool HasTag = false, HasFunction = false; | |||
| 512 | bool HasFunctionTemplate = false, HasUnresolved = false; | |||
| 513 | const NamedDecl *HasNonFunction = nullptr; | |||
| 514 | ||||
| 515 | llvm::SmallVector<const NamedDecl *, 4> EquivalentNonFunctions; | |||
| 516 | ||||
| 517 | unsigned UniqueTagIndex = 0; | |||
| 518 | ||||
| 519 | unsigned I = 0; | |||
| 520 | while (I < N) { | |||
| 521 | const NamedDecl *D = Decls[I]->getUnderlyingDecl(); | |||
| 522 | D = cast<NamedDecl>(D->getCanonicalDecl()); | |||
| 523 | ||||
| 524 | // Ignore an invalid declaration unless it's the only one left. | |||
| 525 | // Also ignore HLSLBufferDecl which not have name conflict with other Decls. | |||
| 526 | if ((D->isInvalidDecl() || isa<HLSLBufferDecl>(D)) && !(I == 0 && N == 1)) { | |||
| 527 | Decls[I] = Decls[--N]; | |||
| 528 | continue; | |||
| 529 | } | |||
| 530 | ||||
| 531 | std::optional<unsigned> ExistingI; | |||
| 532 | ||||
| 533 | // Redeclarations of types via typedef can occur both within a scope | |||
| 534 | // and, through using declarations and directives, across scopes. There is | |||
| 535 | // no ambiguity if they all refer to the same type, so unique based on the | |||
| 536 | // canonical type. | |||
| 537 | if (const auto *TD = dyn_cast<TypeDecl>(D)) { | |||
| 538 | QualType T = getSema().Context.getTypeDeclType(TD); | |||
| 539 | auto UniqueResult = UniqueTypes.insert( | |||
| 540 | std::make_pair(getSema().Context.getCanonicalType(T), I)); | |||
| 541 | if (!UniqueResult.second) { | |||
| 542 | // The type is not unique. | |||
| 543 | ExistingI = UniqueResult.first->second; | |||
| 544 | } | |||
| 545 | } | |||
| 546 | ||||
| 547 | // For non-type declarations, check for a prior lookup result naming this | |||
| 548 | // canonical declaration. | |||
| 549 | if (!ExistingI) { | |||
| 550 | auto UniqueResult = Unique.insert(std::make_pair(D, I)); | |||
| 551 | if (!UniqueResult.second) { | |||
| 552 | // We've seen this entity before. | |||
| 553 | ExistingI = UniqueResult.first->second; | |||
| 554 | } | |||
| 555 | } | |||
| 556 | ||||
| 557 | if (ExistingI) { | |||
| 558 | // This is not a unique lookup result. Pick one of the results and | |||
| 559 | // discard the other. | |||
| 560 | if (isPreferredLookupResult(getSema(), getLookupKind(), Decls[I], | |||
| 561 | Decls[*ExistingI])) | |||
| 562 | Decls[*ExistingI] = Decls[I]; | |||
| 563 | Decls[I] = Decls[--N]; | |||
| 564 | continue; | |||
| 565 | } | |||
| 566 | ||||
| 567 | // Otherwise, do some decl type analysis and then continue. | |||
| 568 | ||||
| 569 | if (isa<UnresolvedUsingValueDecl>(D)) { | |||
| 570 | HasUnresolved = true; | |||
| 571 | } else if (isa<TagDecl>(D)) { | |||
| 572 | if (HasTag) | |||
| 573 | Ambiguous = true; | |||
| 574 | UniqueTagIndex = I; | |||
| 575 | HasTag = true; | |||
| 576 | } else if (isa<FunctionTemplateDecl>(D)) { | |||
| 577 | HasFunction = true; | |||
| 578 | HasFunctionTemplate = true; | |||
| 579 | } else if (isa<FunctionDecl>(D)) { | |||
| 580 | HasFunction = true; | |||
| 581 | } else { | |||
| 582 | if (HasNonFunction) { | |||
| 583 | // If we're about to create an ambiguity between two declarations that | |||
| 584 | // are equivalent, but one is an internal linkage declaration from one | |||
| 585 | // module and the other is an internal linkage declaration from another | |||
| 586 | // module, just skip it. | |||
| 587 | if (getSema().isEquivalentInternalLinkageDeclaration(HasNonFunction, | |||
| 588 | D)) { | |||
| 589 | EquivalentNonFunctions.push_back(D); | |||
| 590 | Decls[I] = Decls[--N]; | |||
| 591 | continue; | |||
| 592 | } | |||
| 593 | ||||
| 594 | Ambiguous = true; | |||
| 595 | } | |||
| 596 | HasNonFunction = D; | |||
| 597 | } | |||
| 598 | I++; | |||
| 599 | } | |||
| 600 | ||||
| 601 | // C++ [basic.scope.hiding]p2: | |||
| 602 | // A class name or enumeration name can be hidden by the name of | |||
| 603 | // an object, function, or enumerator declared in the same | |||
| 604 | // scope. If a class or enumeration name and an object, function, | |||
| 605 | // or enumerator are declared in the same scope (in any order) | |||
| 606 | // with the same name, the class or enumeration name is hidden | |||
| 607 | // wherever the object, function, or enumerator name is visible. | |||
| 608 | // But it's still an error if there are distinct tag types found, | |||
| 609 | // even if they're not visible. (ref?) | |||
| 610 | if (N > 1 && HideTags && HasTag && !Ambiguous && | |||
| 611 | (HasFunction || HasNonFunction || HasUnresolved)) { | |||
| 612 | const NamedDecl *OtherDecl = Decls[UniqueTagIndex ? 0 : N - 1]; | |||
| 613 | if (isa<TagDecl>(Decls[UniqueTagIndex]->getUnderlyingDecl()) && | |||
| 614 | getContextForScopeMatching(Decls[UniqueTagIndex])->Equals( | |||
| 615 | getContextForScopeMatching(OtherDecl)) && | |||
| 616 | canHideTag(OtherDecl)) | |||
| 617 | Decls[UniqueTagIndex] = Decls[--N]; | |||
| 618 | else | |||
| 619 | Ambiguous = true; | |||
| 620 | } | |||
| 621 | ||||
| 622 | // FIXME: This diagnostic should really be delayed until we're done with | |||
| 623 | // the lookup result, in case the ambiguity is resolved by the caller. | |||
| 624 | if (!EquivalentNonFunctions.empty() && !Ambiguous) | |||
| 625 | getSema().diagnoseEquivalentInternalLinkageDeclarations( | |||
| 626 | getNameLoc(), HasNonFunction, EquivalentNonFunctions); | |||
| 627 | ||||
| 628 | Decls.truncate(N); | |||
| 629 | ||||
| 630 | if (HasNonFunction && (HasFunction || HasUnresolved)) | |||
| 631 | Ambiguous = true; | |||
| 632 | ||||
| 633 | if (Ambiguous) | |||
| 634 | setAmbiguous(LookupResult::AmbiguousReference); | |||
| 635 | else if (HasUnresolved) | |||
| 636 | ResultKind = LookupResult::FoundUnresolvedValue; | |||
| 637 | else if (N > 1 || HasFunctionTemplate) | |||
| 638 | ResultKind = LookupResult::FoundOverloaded; | |||
| 639 | else | |||
| 640 | ResultKind = LookupResult::Found; | |||
| 641 | } | |||
| 642 | ||||
| 643 | void LookupResult::addDeclsFromBasePaths(const CXXBasePaths &P) { | |||
| 644 | CXXBasePaths::const_paths_iterator I, E; | |||
| 645 | for (I = P.begin(), E = P.end(); I != E; ++I) | |||
| 646 | for (DeclContext::lookup_iterator DI = I->Decls, DE = DI.end(); DI != DE; | |||
| 647 | ++DI) | |||
| 648 | addDecl(*DI); | |||
| 649 | } | |||
| 650 | ||||
| 651 | void LookupResult::setAmbiguousBaseSubobjects(CXXBasePaths &P) { | |||
| 652 | Paths = new CXXBasePaths; | |||
| 653 | Paths->swap(P); | |||
| 654 | addDeclsFromBasePaths(*Paths); | |||
| 655 | resolveKind(); | |||
| 656 | setAmbiguous(AmbiguousBaseSubobjects); | |||
| 657 | } | |||
| 658 | ||||
| 659 | void LookupResult::setAmbiguousBaseSubobjectTypes(CXXBasePaths &P) { | |||
| 660 | Paths = new CXXBasePaths; | |||
| 661 | Paths->swap(P); | |||
| 662 | addDeclsFromBasePaths(*Paths); | |||
| 663 | resolveKind(); | |||
| 664 | setAmbiguous(AmbiguousBaseSubobjectTypes); | |||
| 665 | } | |||
| 666 | ||||
| 667 | void LookupResult::print(raw_ostream &Out) { | |||
| 668 | Out << Decls.size() << " result(s)"; | |||
| 669 | if (isAmbiguous()) Out << ", ambiguous"; | |||
| 670 | if (Paths) Out << ", base paths present"; | |||
| 671 | ||||
| 672 | for (iterator I = begin(), E = end(); I != E; ++I) { | |||
| 673 | Out << "\n"; | |||
| 674 | (*I)->print(Out, 2); | |||
| 675 | } | |||
| 676 | } | |||
| 677 | ||||
| 678 | LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void LookupResult::dump() { | |||
| 679 | llvm::errs() << "lookup results for " << getLookupName().getAsString() | |||
| 680 | << ":\n"; | |||
| 681 | for (NamedDecl *D : *this) | |||
| 682 | D->dump(); | |||
| 683 | } | |||
| 684 | ||||
| 685 | /// Diagnose a missing builtin type. | |||
| 686 | static QualType diagOpenCLBuiltinTypeError(Sema &S, llvm::StringRef TypeClass, | |||
| 687 | llvm::StringRef Name) { | |||
| 688 | S.Diag(SourceLocation(), diag::err_opencl_type_not_found) | |||
| 689 | << TypeClass << Name; | |||
| 690 | return S.Context.VoidTy; | |||
| 691 | } | |||
| 692 | ||||
| 693 | /// Lookup an OpenCL enum type. | |||
| 694 | static QualType getOpenCLEnumType(Sema &S, llvm::StringRef Name) { | |||
| 695 | LookupResult Result(S, &S.Context.Idents.get(Name), SourceLocation(), | |||
| 696 | Sema::LookupTagName); | |||
| 697 | S.LookupName(Result, S.TUScope); | |||
| 698 | if (Result.empty()) | |||
| 699 | return diagOpenCLBuiltinTypeError(S, "enum", Name); | |||
| 700 | EnumDecl *Decl = Result.getAsSingle<EnumDecl>(); | |||
| 701 | if (!Decl) | |||
| 702 | return diagOpenCLBuiltinTypeError(S, "enum", Name); | |||
| 703 | return S.Context.getEnumType(Decl); | |||
| 704 | } | |||
| 705 | ||||
| 706 | /// Lookup an OpenCL typedef type. | |||
| 707 | static QualType getOpenCLTypedefType(Sema &S, llvm::StringRef Name) { | |||
| 708 | LookupResult Result(S, &S.Context.Idents.get(Name), SourceLocation(), | |||
| 709 | Sema::LookupOrdinaryName); | |||
| 710 | S.LookupName(Result, S.TUScope); | |||
| 711 | if (Result.empty()) | |||
| 712 | return diagOpenCLBuiltinTypeError(S, "typedef", Name); | |||
| 713 | TypedefNameDecl *Decl = Result.getAsSingle<TypedefNameDecl>(); | |||
| 714 | if (!Decl) | |||
| 715 | return diagOpenCLBuiltinTypeError(S, "typedef", Name); | |||
| 716 | return S.Context.getTypedefType(Decl); | |||
| 717 | } | |||
| 718 | ||||
| 719 | /// Get the QualType instances of the return type and arguments for an OpenCL | |||
| 720 | /// builtin function signature. | |||
| 721 | /// \param S (in) The Sema instance. | |||
| 722 | /// \param OpenCLBuiltin (in) The signature currently handled. | |||
| 723 | /// \param GenTypeMaxCnt (out) Maximum number of types contained in a generic | |||
| 724 | /// type used as return type or as argument. | |||
| 725 | /// Only meaningful for generic types, otherwise equals 1. | |||
| 726 | /// \param RetTypes (out) List of the possible return types. | |||
| 727 | /// \param ArgTypes (out) List of the possible argument types. For each | |||
| 728 | /// argument, ArgTypes contains QualTypes for the Cartesian product | |||
| 729 | /// of (vector sizes) x (types) . | |||
| 730 | static void GetQualTypesForOpenCLBuiltin( | |||
| 731 | Sema &S, const OpenCLBuiltinStruct &OpenCLBuiltin, unsigned &GenTypeMaxCnt, | |||
| 732 | SmallVector<QualType, 1> &RetTypes, | |||
| 733 | SmallVector<SmallVector<QualType, 1>, 5> &ArgTypes) { | |||
| 734 | // Get the QualType instances of the return types. | |||
| 735 | unsigned Sig = SignatureTable[OpenCLBuiltin.SigTableIndex]; | |||
| 736 | OCL2Qual(S, TypeTable[Sig], RetTypes); | |||
| 737 | GenTypeMaxCnt = RetTypes.size(); | |||
| 738 | ||||
| 739 | // Get the QualType instances of the arguments. | |||
| 740 | // First type is the return type, skip it. | |||
| 741 | for (unsigned Index = 1; Index < OpenCLBuiltin.NumTypes; Index++) { | |||
| 742 | SmallVector<QualType, 1> Ty; | |||
| 743 | OCL2Qual(S, TypeTable[SignatureTable[OpenCLBuiltin.SigTableIndex + Index]], | |||
| 744 | Ty); | |||
| 745 | GenTypeMaxCnt = (Ty.size() > GenTypeMaxCnt) ? Ty.size() : GenTypeMaxCnt; | |||
| 746 | ArgTypes.push_back(std::move(Ty)); | |||
| 747 | } | |||
| 748 | } | |||
| 749 | ||||
| 750 | /// Create a list of the candidate function overloads for an OpenCL builtin | |||
| 751 | /// function. | |||
| 752 | /// \param Context (in) The ASTContext instance. | |||
| 753 | /// \param GenTypeMaxCnt (in) Maximum number of types contained in a generic | |||
| 754 | /// type used as return type or as argument. | |||
| 755 | /// Only meaningful for generic types, otherwise equals 1. | |||
| 756 | /// \param FunctionList (out) List of FunctionTypes. | |||
| 757 | /// \param RetTypes (in) List of the possible return types. | |||
| 758 | /// \param ArgTypes (in) List of the possible types for the arguments. | |||
| 759 | static void GetOpenCLBuiltinFctOverloads( | |||
| 760 | ASTContext &Context, unsigned GenTypeMaxCnt, | |||
| 761 | std::vector<QualType> &FunctionList, SmallVector<QualType, 1> &RetTypes, | |||
| 762 | SmallVector<SmallVector<QualType, 1>, 5> &ArgTypes) { | |||
| 763 | FunctionProtoType::ExtProtoInfo PI( | |||
| 764 | Context.getDefaultCallingConvention(false, false, true)); | |||
| 765 | PI.Variadic = false; | |||
| 766 | ||||
| 767 | // Do not attempt to create any FunctionTypes if there are no return types, | |||
| 768 | // which happens when a type belongs to a disabled extension. | |||
| 769 | if (RetTypes.size() == 0) | |||
| 770 | return; | |||
| 771 | ||||
| 772 | // Create FunctionTypes for each (gen)type. | |||
| 773 | for (unsigned IGenType = 0; IGenType < GenTypeMaxCnt; IGenType++) { | |||
| 774 | SmallVector<QualType, 5> ArgList; | |||
| 775 | ||||
| 776 | for (unsigned A = 0; A < ArgTypes.size(); A++) { | |||
| 777 | // Bail out if there is an argument that has no available types. | |||
| 778 | if (ArgTypes[A].size() == 0) | |||
| 779 | return; | |||
| 780 | ||||
| 781 | // Builtins such as "max" have an "sgentype" argument that represents | |||
| 782 | // the corresponding scalar type of a gentype. The number of gentypes | |||
| 783 | // must be a multiple of the number of sgentypes. | |||
| 784 | assert(GenTypeMaxCnt % ArgTypes[A].size() == 0 &&(static_cast <bool> (GenTypeMaxCnt % ArgTypes[A].size() == 0 && "argument type count not compatible with gentype type count" ) ? void (0) : __assert_fail ("GenTypeMaxCnt % ArgTypes[A].size() == 0 && \"argument type count not compatible with gentype type count\"" , "clang/lib/Sema/SemaLookup.cpp", 785, __extension__ __PRETTY_FUNCTION__ )) | |||
| 785 | "argument type count not compatible with gentype type count")(static_cast <bool> (GenTypeMaxCnt % ArgTypes[A].size() == 0 && "argument type count not compatible with gentype type count" ) ? void (0) : __assert_fail ("GenTypeMaxCnt % ArgTypes[A].size() == 0 && \"argument type count not compatible with gentype type count\"" , "clang/lib/Sema/SemaLookup.cpp", 785, __extension__ __PRETTY_FUNCTION__ )); | |||
| 786 | unsigned Idx = IGenType % ArgTypes[A].size(); | |||
| 787 | ArgList.push_back(ArgTypes[A][Idx]); | |||
| 788 | } | |||
| 789 | ||||
| 790 | FunctionList.push_back(Context.getFunctionType( | |||
| 791 | RetTypes[(RetTypes.size() != 1) ? IGenType : 0], ArgList, PI)); | |||
| 792 | } | |||
| 793 | } | |||
| 794 | ||||
| 795 | /// When trying to resolve a function name, if isOpenCLBuiltin() returns a | |||
| 796 | /// non-null <Index, Len> pair, then the name is referencing an OpenCL | |||
| 797 | /// builtin function. Add all candidate signatures to the LookUpResult. | |||
| 798 | /// | |||
| 799 | /// \param S (in) The Sema instance. | |||
| 800 | /// \param LR (inout) The LookupResult instance. | |||
| 801 | /// \param II (in) The identifier being resolved. | |||
| 802 | /// \param FctIndex (in) Starting index in the BuiltinTable. | |||
| 803 | /// \param Len (in) The signature list has Len elements. | |||
| 804 | static void InsertOCLBuiltinDeclarationsFromTable(Sema &S, LookupResult &LR, | |||
| 805 | IdentifierInfo *II, | |||
| 806 | const unsigned FctIndex, | |||
| 807 | const unsigned Len) { | |||
| 808 | // The builtin function declaration uses generic types (gentype). | |||
| 809 | bool HasGenType = false; | |||
| 810 | ||||
| 811 | // Maximum number of types contained in a generic type used as return type or | |||
| 812 | // as argument. Only meaningful for generic types, otherwise equals 1. | |||
| 813 | unsigned GenTypeMaxCnt; | |||
| 814 | ||||
| 815 | ASTContext &Context = S.Context; | |||
| 816 | ||||
| 817 | for (unsigned SignatureIndex = 0; SignatureIndex < Len; SignatureIndex++) { | |||
| 818 | const OpenCLBuiltinStruct &OpenCLBuiltin = | |||
| 819 | BuiltinTable[FctIndex + SignatureIndex]; | |||
| 820 | ||||
| 821 | // Ignore this builtin function if it is not available in the currently | |||
| 822 | // selected language version. | |||
| 823 | if (!isOpenCLVersionContainedInMask(Context.getLangOpts(), | |||
| 824 | OpenCLBuiltin.Versions)) | |||
| 825 | continue; | |||
| 826 | ||||
| 827 | // Ignore this builtin function if it carries an extension macro that is | |||
| 828 | // not defined. This indicates that the extension is not supported by the | |||
| 829 | // target, so the builtin function should not be available. | |||
| 830 | StringRef Extensions = FunctionExtensionTable[OpenCLBuiltin.Extension]; | |||
| 831 | if (!Extensions.empty()) { | |||
| 832 | SmallVector<StringRef, 2> ExtVec; | |||
| 833 | Extensions.split(ExtVec, " "); | |||
| 834 | bool AllExtensionsDefined = true; | |||
| 835 | for (StringRef Ext : ExtVec) { | |||
| 836 | if (!S.getPreprocessor().isMacroDefined(Ext)) { | |||
| 837 | AllExtensionsDefined = false; | |||
| 838 | break; | |||
| 839 | } | |||
| 840 | } | |||
| 841 | if (!AllExtensionsDefined) | |||
| 842 | continue; | |||
| 843 | } | |||
| 844 | ||||
| 845 | SmallVector<QualType, 1> RetTypes; | |||
| 846 | SmallVector<SmallVector<QualType, 1>, 5> ArgTypes; | |||
| 847 | ||||
| 848 | // Obtain QualType lists for the function signature. | |||
| 849 | GetQualTypesForOpenCLBuiltin(S, OpenCLBuiltin, GenTypeMaxCnt, RetTypes, | |||
| 850 | ArgTypes); | |||
| 851 | if (GenTypeMaxCnt > 1) { | |||
| 852 | HasGenType = true; | |||
| 853 | } | |||
| 854 | ||||
| 855 | // Create function overload for each type combination. | |||
| 856 | std::vector<QualType> FunctionList; | |||
| 857 | GetOpenCLBuiltinFctOverloads(Context, GenTypeMaxCnt, FunctionList, RetTypes, | |||
| 858 | ArgTypes); | |||
| 859 | ||||
| 860 | SourceLocation Loc = LR.getNameLoc(); | |||
| 861 | DeclContext *Parent = Context.getTranslationUnitDecl(); | |||
| 862 | FunctionDecl *NewOpenCLBuiltin; | |||
| 863 | ||||
| 864 | for (const auto &FTy : FunctionList) { | |||
| 865 | NewOpenCLBuiltin = FunctionDecl::Create( | |||
| 866 | Context, Parent, Loc, Loc, II, FTy, /*TInfo=*/nullptr, SC_Extern, | |||
| 867 | S.getCurFPFeatures().isFPConstrained(), false, | |||
| 868 | FTy->isFunctionProtoType()); | |||
| 869 | NewOpenCLBuiltin->setImplicit(); | |||
| 870 | ||||
| 871 | // Create Decl objects for each parameter, adding them to the | |||
| 872 | // FunctionDecl. | |||
| 873 | const auto *FP = cast<FunctionProtoType>(FTy); | |||
| 874 | SmallVector<ParmVarDecl *, 4> ParmList; | |||
| 875 | for (unsigned IParm = 0, e = FP->getNumParams(); IParm != e; ++IParm) { | |||
| 876 | ParmVarDecl *Parm = ParmVarDecl::Create( | |||
| 877 | Context, NewOpenCLBuiltin, SourceLocation(), SourceLocation(), | |||
| 878 | nullptr, FP->getParamType(IParm), nullptr, SC_None, nullptr); | |||
| 879 | Parm->setScopeInfo(0, IParm); | |||
| 880 | ParmList.push_back(Parm); | |||
| 881 | } | |||
| 882 | NewOpenCLBuiltin->setParams(ParmList); | |||
| 883 | ||||
| 884 | // Add function attributes. | |||
| 885 | if (OpenCLBuiltin.IsPure) | |||
| 886 | NewOpenCLBuiltin->addAttr(PureAttr::CreateImplicit(Context)); | |||
| 887 | if (OpenCLBuiltin.IsConst) | |||
| 888 | NewOpenCLBuiltin->addAttr(ConstAttr::CreateImplicit(Context)); | |||
| 889 | if (OpenCLBuiltin.IsConv) | |||
| 890 | NewOpenCLBuiltin->addAttr(ConvergentAttr::CreateImplicit(Context)); | |||
| 891 | ||||
| 892 | if (!S.getLangOpts().OpenCLCPlusPlus) | |||
| 893 | NewOpenCLBuiltin->addAttr(OverloadableAttr::CreateImplicit(Context)); | |||
| 894 | ||||
| 895 | LR.addDecl(NewOpenCLBuiltin); | |||
| 896 | } | |||
| 897 | } | |||
| 898 | ||||
| 899 | // If we added overloads, need to resolve the lookup result. | |||
| 900 | if (Len > 1 || HasGenType) | |||
| 901 | LR.resolveKind(); | |||
| 902 | } | |||
| 903 | ||||
| 904 | /// Lookup a builtin function, when name lookup would otherwise | |||
| 905 | /// fail. | |||
| 906 | bool Sema::LookupBuiltin(LookupResult &R) { | |||
| 907 | Sema::LookupNameKind NameKind = R.getLookupKind(); | |||
| 908 | ||||
| 909 | // If we didn't find a use of this identifier, and if the identifier | |||
| 910 | // corresponds to a compiler builtin, create the decl object for the builtin | |||
| 911 | // now, injecting it into translation unit scope, and return it. | |||
| 912 | if (NameKind == Sema::LookupOrdinaryName || | |||
| 913 | NameKind == Sema::LookupRedeclarationWithLinkage) { | |||
| 914 | IdentifierInfo *II = R.getLookupName().getAsIdentifierInfo(); | |||
| 915 | if (II) { | |||
| 916 | if (getLangOpts().CPlusPlus && NameKind == Sema::LookupOrdinaryName) { | |||
| 917 | if (II == getASTContext().getMakeIntegerSeqName()) { | |||
| 918 | R.addDecl(getASTContext().getMakeIntegerSeqDecl()); | |||
| 919 | return true; | |||
| 920 | } else if (II == getASTContext().getTypePackElementName()) { | |||
| 921 | R.addDecl(getASTContext().getTypePackElementDecl()); | |||
| 922 | return true; | |||
| 923 | } | |||
| 924 | } | |||
| 925 | ||||
| 926 | // Check if this is an OpenCL Builtin, and if so, insert its overloads. | |||
| 927 | if (getLangOpts().OpenCL && getLangOpts().DeclareOpenCLBuiltins) { | |||
| 928 | auto Index = isOpenCLBuiltin(II->getName()); | |||
| 929 | if (Index.first) { | |||
| 930 | InsertOCLBuiltinDeclarationsFromTable(*this, R, II, Index.first - 1, | |||
| 931 | Index.second); | |||
| 932 | return true; | |||
| 933 | } | |||
| 934 | } | |||
| 935 | ||||
| 936 | if (DeclareRISCVVBuiltins || DeclareRISCVVectorBuiltins) { | |||
| 937 | if (!RVIntrinsicManager) | |||
| 938 | RVIntrinsicManager = CreateRISCVIntrinsicManager(*this); | |||
| 939 | ||||
| 940 | if (RVIntrinsicManager->CreateIntrinsicIfFound(R, II, PP)) | |||
| 941 | return true; | |||
| 942 | } | |||
| 943 | ||||
| 944 | // If this is a builtin on this (or all) targets, create the decl. | |||
| 945 | if (unsigned BuiltinID = II->getBuiltinID()) { | |||
| 946 | // In C++ and OpenCL (spec v1.2 s6.9.f), we don't have any predefined | |||
| 947 | // library functions like 'malloc'. Instead, we'll just error. | |||
| 948 | if ((getLangOpts().CPlusPlus || getLangOpts().OpenCL) && | |||
| 949 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) | |||
| 950 | return false; | |||
| 951 | ||||
| 952 | if (NamedDecl *D = | |||
| 953 | LazilyCreateBuiltin(II, BuiltinID, TUScope, | |||
| 954 | R.isForRedeclaration(), R.getNameLoc())) { | |||
| 955 | R.addDecl(D); | |||
| 956 | return true; | |||
| 957 | } | |||
| 958 | } | |||
| 959 | } | |||
| 960 | } | |||
| 961 | ||||
| 962 | return false; | |||
| 963 | } | |||
| 964 | ||||
| 965 | /// Looks up the declaration of "struct objc_super" and | |||
| 966 | /// saves it for later use in building builtin declaration of | |||
| 967 | /// objc_msgSendSuper and objc_msgSendSuper_stret. | |||
| 968 | static void LookupPredefedObjCSuperType(Sema &Sema, Scope *S) { | |||
| 969 | ASTContext &Context = Sema.Context; | |||
| 970 | LookupResult Result(Sema, &Context.Idents.get("objc_super"), SourceLocation(), | |||
| 971 | Sema::LookupTagName); | |||
| 972 | Sema.LookupName(Result, S); | |||
| 973 | if (Result.getResultKind() == LookupResult::Found) | |||
| 974 | if (const TagDecl *TD = Result.getAsSingle<TagDecl>()) | |||
| 975 | Context.setObjCSuperType(Context.getTagDeclType(TD)); | |||
| 976 | } | |||
| 977 | ||||
| 978 | void Sema::LookupNecessaryTypesForBuiltin(Scope *S, unsigned ID) { | |||
| 979 | if (ID == Builtin::BIobjc_msgSendSuper) | |||
| 980 | LookupPredefedObjCSuperType(*this, S); | |||
| 981 | } | |||
| 982 | ||||
| 983 | /// Determine whether we can declare a special member function within | |||
| 984 | /// the class at this point. | |||
| 985 | static bool CanDeclareSpecialMemberFunction(const CXXRecordDecl *Class) { | |||
| 986 | // We need to have a definition for the class. | |||
| 987 | if (!Class->getDefinition() || Class->isDependentContext()) | |||
| 988 | return false; | |||
| 989 | ||||
| 990 | // We can't be in the middle of defining the class. | |||
| 991 | return !Class->isBeingDefined(); | |||
| 992 | } | |||
| 993 | ||||
| 994 | void Sema::ForceDeclarationOfImplicitMembers(CXXRecordDecl *Class) { | |||
| 995 | if (!CanDeclareSpecialMemberFunction(Class)) | |||
| 996 | return; | |||
| 997 | ||||
| 998 | // If the default constructor has not yet been declared, do so now. | |||
| 999 | if (Class->needsImplicitDefaultConstructor()) | |||
| 1000 | DeclareImplicitDefaultConstructor(Class); | |||
| 1001 | ||||
| 1002 | // If the copy constructor has not yet been declared, do so now. | |||
| 1003 | if (Class->needsImplicitCopyConstructor()) | |||
| 1004 | DeclareImplicitCopyConstructor(Class); | |||
| 1005 | ||||
| 1006 | // If the copy assignment operator has not yet been declared, do so now. | |||
| 1007 | if (Class->needsImplicitCopyAssignment()) | |||
| 1008 | DeclareImplicitCopyAssignment(Class); | |||
| 1009 | ||||
| 1010 | if (getLangOpts().CPlusPlus11) { | |||
| 1011 | // If the move constructor has not yet been declared, do so now. | |||
| 1012 | if (Class->needsImplicitMoveConstructor()) | |||
| 1013 | DeclareImplicitMoveConstructor(Class); | |||
| 1014 | ||||
| 1015 | // If the move assignment operator has not yet been declared, do so now. | |||
| 1016 | if (Class->needsImplicitMoveAssignment()) | |||
| 1017 | DeclareImplicitMoveAssignment(Class); | |||
| 1018 | } | |||
| 1019 | ||||
| 1020 | // If the destructor has not yet been declared, do so now. | |||
| 1021 | if (Class->needsImplicitDestructor()) | |||
| 1022 | DeclareImplicitDestructor(Class); | |||
| 1023 | } | |||
| 1024 | ||||
| 1025 | /// Determine whether this is the name of an implicitly-declared | |||
| 1026 | /// special member function. | |||
| 1027 | static bool isImplicitlyDeclaredMemberFunctionName(DeclarationName Name) { | |||
| 1028 | switch (Name.getNameKind()) { | |||
| 1029 | case DeclarationName::CXXConstructorName: | |||
| 1030 | case DeclarationName::CXXDestructorName: | |||
| 1031 | return true; | |||
| 1032 | ||||
| 1033 | case DeclarationName::CXXOperatorName: | |||
| 1034 | return Name.getCXXOverloadedOperator() == OO_Equal; | |||
| 1035 | ||||
| 1036 | default: | |||
| 1037 | break; | |||
| 1038 | } | |||
| 1039 | ||||
| 1040 | return false; | |||
| 1041 | } | |||
| 1042 | ||||
| 1043 | /// If there are any implicit member functions with the given name | |||
| 1044 | /// that need to be declared in the given declaration context, do so. | |||
| 1045 | static void DeclareImplicitMemberFunctionsWithName(Sema &S, | |||
| 1046 | DeclarationName Name, | |||
| 1047 | SourceLocation Loc, | |||
| 1048 | const DeclContext *DC) { | |||
| 1049 | if (!DC) | |||
| 1050 | return; | |||
| 1051 | ||||
| 1052 | switch (Name.getNameKind()) { | |||
| 1053 | case DeclarationName::CXXConstructorName: | |||
| 1054 | if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(DC)) | |||
| 1055 | if (Record->getDefinition() && CanDeclareSpecialMemberFunction(Record)) { | |||
| 1056 | CXXRecordDecl *Class = const_cast<CXXRecordDecl *>(Record); | |||
| 1057 | if (Record->needsImplicitDefaultConstructor()) | |||
| 1058 | S.DeclareImplicitDefaultConstructor(Class); | |||
| 1059 | if (Record->needsImplicitCopyConstructor()) | |||
| 1060 | S.DeclareImplicitCopyConstructor(Class); | |||
| 1061 | if (S.getLangOpts().CPlusPlus11 && | |||
| 1062 | Record->needsImplicitMoveConstructor()) | |||
| 1063 | S.DeclareImplicitMoveConstructor(Class); | |||
| 1064 | } | |||
| 1065 | break; | |||
| 1066 | ||||
| 1067 | case DeclarationName::CXXDestructorName: | |||
| 1068 | if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(DC)) | |||
| 1069 | if (Record->getDefinition() && Record->needsImplicitDestructor() && | |||
| 1070 | CanDeclareSpecialMemberFunction(Record)) | |||
| 1071 | S.DeclareImplicitDestructor(const_cast<CXXRecordDecl *>(Record)); | |||
| 1072 | break; | |||
| 1073 | ||||
| 1074 | case DeclarationName::CXXOperatorName: | |||
| 1075 | if (Name.getCXXOverloadedOperator() != OO_Equal) | |||
| 1076 | break; | |||
| 1077 | ||||
| 1078 | if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(DC)) { | |||
| 1079 | if (Record->getDefinition() && CanDeclareSpecialMemberFunction(Record)) { | |||
| 1080 | CXXRecordDecl *Class = const_cast<CXXRecordDecl *>(Record); | |||
| 1081 | if (Record->needsImplicitCopyAssignment()) | |||
| 1082 | S.DeclareImplicitCopyAssignment(Class); | |||
| 1083 | if (S.getLangOpts().CPlusPlus11 && | |||
| 1084 | Record->needsImplicitMoveAssignment()) | |||
| 1085 | S.DeclareImplicitMoveAssignment(Class); | |||
| 1086 | } | |||
| 1087 | } | |||
| 1088 | break; | |||
| 1089 | ||||
| 1090 | case DeclarationName::CXXDeductionGuideName: | |||
| 1091 | S.DeclareImplicitDeductionGuides(Name.getCXXDeductionGuideTemplate(), Loc); | |||
| 1092 | break; | |||
| 1093 | ||||
| 1094 | default: | |||
| 1095 | break; | |||
| 1096 | } | |||
| 1097 | } | |||
| 1098 | ||||
| 1099 | // Adds all qualifying matches for a name within a decl context to the | |||
| 1100 | // given lookup result. Returns true if any matches were found. | |||
| 1101 | static bool LookupDirect(Sema &S, LookupResult &R, const DeclContext *DC) { | |||
| 1102 | bool Found = false; | |||
| 1103 | ||||
| 1104 | // Lazily declare C++ special member functions. | |||
| 1105 | if (S.getLangOpts().CPlusPlus) | |||
| 1106 | DeclareImplicitMemberFunctionsWithName(S, R.getLookupName(), R.getNameLoc(), | |||
| 1107 | DC); | |||
| 1108 | ||||
| 1109 | // Perform lookup into this declaration context. | |||
| 1110 | DeclContext::lookup_result DR = DC->lookup(R.getLookupName()); | |||
| 1111 | for (NamedDecl *D : DR) { | |||
| 1112 | if ((D = R.getAcceptableDecl(D))) { | |||
| 1113 | R.addDecl(D); | |||
| 1114 | Found = true; | |||
| 1115 | } | |||
| 1116 | } | |||
| 1117 | ||||
| 1118 | if (!Found && DC->isTranslationUnit() && S.LookupBuiltin(R)) | |||
| 1119 | return true; | |||
| 1120 | ||||
| 1121 | if (R.getLookupName().getNameKind() | |||
| 1122 | != DeclarationName::CXXConversionFunctionName || | |||
| 1123 | R.getLookupName().getCXXNameType()->isDependentType() || | |||
| 1124 | !isa<CXXRecordDecl>(DC)) | |||
| 1125 | return Found; | |||
| 1126 | ||||
| 1127 | // C++ [temp.mem]p6: | |||
| 1128 | // A specialization of a conversion function template is not found by | |||
| 1129 | // name lookup. Instead, any conversion function templates visible in the | |||
| 1130 | // context of the use are considered. [...] | |||
| 1131 | const CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); | |||
| 1132 | if (!Record->isCompleteDefinition()) | |||
| 1133 | return Found; | |||
| 1134 | ||||
| 1135 | // For conversion operators, 'operator auto' should only match | |||
| 1136 | // 'operator auto'. Since 'auto' is not a type, it shouldn't be considered | |||
| 1137 | // as a candidate for template substitution. | |||
| 1138 | auto *ContainedDeducedType = | |||
| 1139 | R.getLookupName().getCXXNameType()->getContainedDeducedType(); | |||
| 1140 | if (R.getLookupName().getNameKind() == | |||
| 1141 | DeclarationName::CXXConversionFunctionName && | |||
| 1142 | ContainedDeducedType && ContainedDeducedType->isUndeducedType()) | |||
| 1143 | return Found; | |||
| 1144 | ||||
| 1145 | for (CXXRecordDecl::conversion_iterator U = Record->conversion_begin(), | |||
| 1146 | UEnd = Record->conversion_end(); U != UEnd; ++U) { | |||
| 1147 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(*U); | |||
| 1148 | if (!ConvTemplate) | |||
| 1149 | continue; | |||
| 1150 | ||||
| 1151 | // When we're performing lookup for the purposes of redeclaration, just | |||
| 1152 | // add the conversion function template. When we deduce template | |||
| 1153 | // arguments for specializations, we'll end up unifying the return | |||
| 1154 | // type of the new declaration with the type of the function template. | |||
| 1155 | if (R.isForRedeclaration()) { | |||
| 1156 | R.addDecl(ConvTemplate); | |||
| 1157 | Found = true; | |||
| 1158 | continue; | |||
| 1159 | } | |||
| 1160 | ||||
| 1161 | // C++ [temp.mem]p6: | |||
| 1162 | // [...] For each such operator, if argument deduction succeeds | |||
| 1163 | // (14.9.2.3), the resulting specialization is used as if found by | |||
| 1164 | // name lookup. | |||
| 1165 | // | |||
| 1166 | // When referencing a conversion function for any purpose other than | |||
| 1167 | // a redeclaration (such that we'll be building an expression with the | |||
| 1168 | // result), perform template argument deduction and place the | |||
| 1169 | // specialization into the result set. We do this to avoid forcing all | |||
| 1170 | // callers to perform special deduction for conversion functions. | |||
| 1171 | TemplateDeductionInfo Info(R.getNameLoc()); | |||
| 1172 | FunctionDecl *Specialization = nullptr; | |||
| 1173 | ||||
| 1174 | const FunctionProtoType *ConvProto | |||
| 1175 | = ConvTemplate->getTemplatedDecl()->getType()->getAs<FunctionProtoType>(); | |||
| 1176 | assert(ConvProto && "Nonsensical conversion function template type")(static_cast <bool> (ConvProto && "Nonsensical conversion function template type" ) ? void (0) : __assert_fail ("ConvProto && \"Nonsensical conversion function template type\"" , "clang/lib/Sema/SemaLookup.cpp", 1176, __extension__ __PRETTY_FUNCTION__ )); | |||
| 1177 | ||||
| 1178 | // Compute the type of the function that we would expect the conversion | |||
| 1179 | // function to have, if it were to match the name given. | |||
| 1180 | // FIXME: Calling convention! | |||
| 1181 | FunctionProtoType::ExtProtoInfo EPI = ConvProto->getExtProtoInfo(); | |||
| 1182 | EPI.ExtInfo = EPI.ExtInfo.withCallingConv(CC_C); | |||
| 1183 | EPI.ExceptionSpec = EST_None; | |||
| 1184 | QualType ExpectedType = R.getSema().Context.getFunctionType( | |||
| 1185 | R.getLookupName().getCXXNameType(), std::nullopt, EPI); | |||
| 1186 | ||||
| 1187 | // Perform template argument deduction against the type that we would | |||
| 1188 | // expect the function to have. | |||
| 1189 | if (R.getSema().DeduceTemplateArguments(ConvTemplate, nullptr, ExpectedType, | |||
| 1190 | Specialization, Info) | |||
| 1191 | == Sema::TDK_Success) { | |||
| 1192 | R.addDecl(Specialization); | |||
| 1193 | Found = true; | |||
| 1194 | } | |||
| 1195 | } | |||
| 1196 | ||||
| 1197 | return Found; | |||
| 1198 | } | |||
| 1199 | ||||
| 1200 | // Performs C++ unqualified lookup into the given file context. | |||
| 1201 | static bool CppNamespaceLookup(Sema &S, LookupResult &R, ASTContext &Context, | |||
| 1202 | const DeclContext *NS, | |||
| 1203 | UnqualUsingDirectiveSet &UDirs) { | |||
| 1204 | ||||
| 1205 | assert(NS && NS->isFileContext() && "CppNamespaceLookup() requires namespace!")(static_cast <bool> (NS && NS->isFileContext () && "CppNamespaceLookup() requires namespace!") ? void (0) : __assert_fail ("NS && NS->isFileContext() && \"CppNamespaceLookup() requires namespace!\"" , "clang/lib/Sema/SemaLookup.cpp", 1205, __extension__ __PRETTY_FUNCTION__ )); | |||
| 1206 | ||||
| 1207 | // Perform direct name lookup into the LookupCtx. | |||
| 1208 | bool Found = LookupDirect(S, R, NS); | |||
| 1209 | ||||
| 1210 | // Perform direct name lookup into the namespaces nominated by the | |||
| 1211 | // using directives whose common ancestor is this namespace. | |||
| 1212 | for (const UnqualUsingEntry &UUE : UDirs.getNamespacesFor(NS)) | |||
| 1213 | if (LookupDirect(S, R, UUE.getNominatedNamespace())) | |||
| 1214 | Found = true; | |||
| 1215 | ||||
| 1216 | R.resolveKind(); | |||
| 1217 | ||||
| 1218 | return Found; | |||
| 1219 | } | |||
| 1220 | ||||
| 1221 | static bool isNamespaceOrTranslationUnitScope(Scope *S) { | |||
| 1222 | if (DeclContext *Ctx = S->getEntity()) | |||
| 1223 | return Ctx->isFileContext(); | |||
| 1224 | return false; | |||
| 1225 | } | |||
| 1226 | ||||
| 1227 | /// Find the outer declaration context from this scope. This indicates the | |||
| 1228 | /// context that we should search up to (exclusive) before considering the | |||
| 1229 | /// parent of the specified scope. | |||
| 1230 | static DeclContext *findOuterContext(Scope *S) { | |||
| 1231 | for (Scope *OuterS = S->getParent(); OuterS; OuterS = OuterS->getParent()) | |||
| 1232 | if (DeclContext *DC = OuterS->getLookupEntity()) | |||
| 1233 | return DC; | |||
| 1234 | return nullptr; | |||
| 1235 | } | |||
| 1236 | ||||
| 1237 | namespace { | |||
| 1238 | /// An RAII object to specify that we want to find block scope extern | |||
| 1239 | /// declarations. | |||
| 1240 | struct FindLocalExternScope { | |||
| 1241 | FindLocalExternScope(LookupResult &R) | |||
| 1242 | : R(R), OldFindLocalExtern(R.getIdentifierNamespace() & | |||
| 1243 | Decl::IDNS_LocalExtern) { | |||
| 1244 | R.setFindLocalExtern(R.getIdentifierNamespace() & | |||
| 1245 | (Decl::IDNS_Ordinary | Decl::IDNS_NonMemberOperator)); | |||
| 1246 | } | |||
| 1247 | void restore() { | |||
| 1248 | R.setFindLocalExtern(OldFindLocalExtern); | |||
| 1249 | } | |||
| 1250 | ~FindLocalExternScope() { | |||
| 1251 | restore(); | |||
| 1252 | } | |||
| 1253 | LookupResult &R; | |||
| 1254 | bool OldFindLocalExtern; | |||
| 1255 | }; | |||
| 1256 | } // end anonymous namespace | |||
| 1257 | ||||
| 1258 | bool Sema::CppLookupName(LookupResult &R, Scope *S) { | |||
| 1259 | assert(getLangOpts().CPlusPlus && "Can perform only C++ lookup")(static_cast <bool> (getLangOpts().CPlusPlus && "Can perform only C++ lookup") ? void (0) : __assert_fail ("getLangOpts().CPlusPlus && \"Can perform only C++ lookup\"" , "clang/lib/Sema/SemaLookup.cpp", 1259, __extension__ __PRETTY_FUNCTION__ )); | |||
| 1260 | ||||
| 1261 | DeclarationName Name = R.getLookupName(); | |||
| 1262 | Sema::LookupNameKind NameKind = R.getLookupKind(); | |||
| 1263 | ||||
| 1264 | // If this is the name of an implicitly-declared special member function, | |||
| 1265 | // go through the scope stack to implicitly declare | |||
| 1266 | if (isImplicitlyDeclaredMemberFunctionName(Name)) { | |||
| 1267 | for (Scope *PreS = S; PreS; PreS = PreS->getParent()) | |||
| 1268 | if (DeclContext *DC = PreS->getEntity()) | |||
| 1269 | DeclareImplicitMemberFunctionsWithName(*this, Name, R.getNameLoc(), DC); | |||
| 1270 | } | |||
| 1271 | ||||
| 1272 | // Implicitly declare member functions with the name we're looking for, if in | |||
| 1273 | // fact we are in a scope where it matters. | |||
| 1274 | ||||
| 1275 | Scope *Initial = S; | |||
| 1276 | IdentifierResolver::iterator | |||
| 1277 | I = IdResolver.begin(Name), | |||
| 1278 | IEnd = IdResolver.end(); | |||
| 1279 | ||||
| 1280 | // First we lookup local scope. | |||
| 1281 | // We don't consider using-directives, as per 7.3.4.p1 [namespace.udir] | |||
| 1282 | // ...During unqualified name lookup (3.4.1), the names appear as if | |||
| 1283 | // they were declared in the nearest enclosing namespace which contains | |||
| 1284 | // both the using-directive and the nominated namespace. | |||
| 1285 | // [Note: in this context, "contains" means "contains directly or | |||
| 1286 | // indirectly". | |||
| 1287 | // | |||
| 1288 | // For example: | |||
| 1289 | // namespace A { int i; } | |||
| 1290 | // void foo() { | |||
| 1291 | // int i; | |||
| 1292 | // { | |||
| 1293 | // using namespace A; | |||
| 1294 | // ++i; // finds local 'i', A::i appears at global scope | |||
| 1295 | // } | |||
| 1296 | // } | |||
| 1297 | // | |||
| 1298 | UnqualUsingDirectiveSet UDirs(*this); | |||
| 1299 | bool VisitedUsingDirectives = false; | |||
| 1300 | bool LeftStartingScope = false; | |||
| 1301 | ||||
| 1302 | // When performing a scope lookup, we want to find local extern decls. | |||
| 1303 | FindLocalExternScope FindLocals(R); | |||
| 1304 | ||||
| 1305 | for (; S && !isNamespaceOrTranslationUnitScope(S); S = S->getParent()) { | |||
| 1306 | bool SearchNamespaceScope = true; | |||
| 1307 | // Check whether the IdResolver has anything in this scope. | |||
| 1308 | for (; I != IEnd && S->isDeclScope(*I); ++I) { | |||
| 1309 | if (NamedDecl *ND = R.getAcceptableDecl(*I)) { | |||
| 1310 | if (NameKind == LookupRedeclarationWithLinkage && | |||
| 1311 | !(*I)->isTemplateParameter()) { | |||
| 1312 | // If it's a template parameter, we still find it, so we can diagnose | |||
| 1313 | // the invalid redeclaration. | |||
| 1314 | ||||
| 1315 | // Determine whether this (or a previous) declaration is | |||
| 1316 | // out-of-scope. | |||
| 1317 | if (!LeftStartingScope && !Initial->isDeclScope(*I)) | |||
| 1318 | LeftStartingScope = true; | |||
| 1319 | ||||
| 1320 | // If we found something outside of our starting scope that | |||
| 1321 | // does not have linkage, skip it. | |||
| 1322 | if (LeftStartingScope && !((*I)->hasLinkage())) { | |||
| 1323 | R.setShadowed(); | |||
| 1324 | continue; | |||
| 1325 | } | |||
| 1326 | } else { | |||
| 1327 | // We found something in this scope, we should not look at the | |||
| 1328 | // namespace scope | |||
| 1329 | SearchNamespaceScope = false; | |||
| 1330 | } | |||
| 1331 | R.addDecl(ND); | |||
| 1332 | } | |||
| 1333 | } | |||
| 1334 | if (!SearchNamespaceScope) { | |||
| 1335 | R.resolveKind(); | |||
| 1336 | if (S->isClassScope()) | |||
| 1337 | if (auto *Record = dyn_cast_if_present<CXXRecordDecl>(S->getEntity())) | |||
| 1338 | R.setNamingClass(Record); | |||
| 1339 | return true; | |||
| 1340 | } | |||
| 1341 | ||||
| 1342 | if (NameKind == LookupLocalFriendName && !S->isClassScope()) { | |||
| 1343 | // C++11 [class.friend]p11: | |||
| 1344 | // If a friend declaration appears in a local class and the name | |||
| 1345 | // specified is an unqualified name, a prior declaration is | |||
| 1346 | // looked up without considering scopes that are outside the | |||
| 1347 | // innermost enclosing non-class scope. | |||
| 1348 | return false; | |||
| 1349 | } | |||
| 1350 | ||||
| 1351 | if (DeclContext *Ctx = S->getLookupEntity()) { | |||
| 1352 | DeclContext *OuterCtx = findOuterContext(S); | |||
| 1353 | for (; Ctx && !Ctx->Equals(OuterCtx); Ctx = Ctx->getLookupParent()) { | |||
| 1354 | // We do not directly look into transparent contexts, since | |||
| 1355 | // those entities will be found in the nearest enclosing | |||
| 1356 | // non-transparent context. | |||
| 1357 | if (Ctx->isTransparentContext()) | |||
| 1358 | continue; | |||
| 1359 | ||||
| 1360 | // We do not look directly into function or method contexts, | |||
| 1361 | // since all of the local variables and parameters of the | |||
| 1362 | // function/method are present within the Scope. | |||
| 1363 | if (Ctx->isFunctionOrMethod()) { | |||
| 1364 | // If we have an Objective-C instance method, look for ivars | |||
| 1365 | // in the corresponding interface. | |||
| 1366 | if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(Ctx)) { | |||
| 1367 | if (Method->isInstanceMethod() && Name.getAsIdentifierInfo()) | |||
| 1368 | if (ObjCInterfaceDecl *Class = Method->getClassInterface()) { | |||
| 1369 | ObjCInterfaceDecl *ClassDeclared; | |||
| 1370 | if (ObjCIvarDecl *Ivar = Class->lookupInstanceVariable( | |||
| 1371 | Name.getAsIdentifierInfo(), | |||
| 1372 | ClassDeclared)) { | |||
| 1373 | if (NamedDecl *ND = R.getAcceptableDecl(Ivar)) { | |||
| 1374 | R.addDecl(ND); | |||
| 1375 | R.resolveKind(); | |||
| 1376 | return true; | |||
| 1377 | } | |||
| 1378 | } | |||
| 1379 | } | |||
| 1380 | } | |||
| 1381 | ||||
| 1382 | continue; | |||
| 1383 | } | |||
| 1384 | ||||
| 1385 | // If this is a file context, we need to perform unqualified name | |||
| 1386 | // lookup considering using directives. | |||
| 1387 | if (Ctx->isFileContext()) { | |||
| 1388 | // If we haven't handled using directives yet, do so now. | |||
| 1389 | if (!VisitedUsingDirectives) { | |||
| 1390 | // Add using directives from this context up to the top level. | |||
| 1391 | for (DeclContext *UCtx = Ctx; UCtx; UCtx = UCtx->getParent()) { | |||
| 1392 | if (UCtx->isTransparentContext()) | |||
| 1393 | continue; | |||
| 1394 | ||||
| 1395 | UDirs.visit(UCtx, UCtx); | |||
| 1396 | } | |||
| 1397 | ||||
| 1398 | // Find the innermost file scope, so we can add using directives | |||
| 1399 | // from local scopes. | |||
| 1400 | Scope *InnermostFileScope = S; | |||
| 1401 | while (InnermostFileScope && | |||
| 1402 | !isNamespaceOrTranslationUnitScope(InnermostFileScope)) | |||
| 1403 | InnermostFileScope = InnermostFileScope->getParent(); | |||
| 1404 | UDirs.visitScopeChain(Initial, InnermostFileScope); | |||
| 1405 | ||||
| 1406 | UDirs.done(); | |||
| 1407 | ||||
| 1408 | VisitedUsingDirectives = true; | |||
| 1409 | } | |||
| 1410 | ||||
| 1411 | if (CppNamespaceLookup(*this, R, Context, Ctx, UDirs)) { | |||
| 1412 | R.resolveKind(); | |||
| 1413 | return true; | |||
| 1414 | } | |||
| 1415 | ||||
| 1416 | continue; | |||
| 1417 | } | |||
| 1418 | ||||
| 1419 | // Perform qualified name lookup into this context. | |||
| 1420 | // FIXME: In some cases, we know that every name that could be found by | |||
| 1421 | // this qualified name lookup will also be on the identifier chain. For | |||
| 1422 | // example, inside a class without any base classes, we never need to | |||
| 1423 | // perform qualified lookup because all of the members are on top of the | |||
| 1424 | // identifier chain. | |||
| 1425 | if (LookupQualifiedName(R, Ctx, /*InUnqualifiedLookup=*/true)) | |||
| 1426 | return true; | |||
| 1427 | } | |||
| 1428 | } | |||
| 1429 | } | |||
| 1430 | ||||
| 1431 | // Stop if we ran out of scopes. | |||
| 1432 | // FIXME: This really, really shouldn't be happening. | |||
| 1433 | if (!S) return false; | |||
| 1434 | ||||
| 1435 | // If we are looking for members, no need to look into global/namespace scope. | |||
| 1436 | if (NameKind == LookupMemberName) | |||
| 1437 | return false; | |||
| 1438 | ||||
| 1439 | // Collect UsingDirectiveDecls in all scopes, and recursively all | |||
| 1440 | // nominated namespaces by those using-directives. | |||
| 1441 | // | |||
| 1442 | // FIXME: Cache this sorted list in Scope structure, and DeclContext, so we | |||
| 1443 | // don't build it for each lookup! | |||
| 1444 | if (!VisitedUsingDirectives) { | |||
| 1445 | UDirs.visitScopeChain(Initial, S); | |||
| 1446 | UDirs.done(); | |||
| 1447 | } | |||
| 1448 | ||||
| 1449 | // If we're not performing redeclaration lookup, do not look for local | |||
| 1450 | // extern declarations outside of a function scope. | |||
| 1451 | if (!R.isForRedeclaration()) | |||
| 1452 | FindLocals.restore(); | |||
| 1453 | ||||
| 1454 | // Lookup namespace scope, and global scope. | |||
| 1455 | // Unqualified name lookup in C++ requires looking into scopes | |||
| 1456 | // that aren't strictly lexical, and therefore we walk through the | |||
| 1457 | // context as well as walking through the scopes. | |||
| 1458 | for (; S; S = S->getParent()) { | |||
| 1459 | // Check whether the IdResolver has anything in this scope. | |||
| 1460 | bool Found = false; | |||
| 1461 | for (; I != IEnd && S->isDeclScope(*I); ++I) { | |||
| 1462 | if (NamedDecl *ND = R.getAcceptableDecl(*I)) { | |||
| 1463 | // We found something. Look for anything else in our scope | |||
| 1464 | // with this same name and in an acceptable identifier | |||
| 1465 | // namespace, so that we can construct an overload set if we | |||
| 1466 | // need to. | |||
| 1467 | Found = true; | |||
| 1468 | R.addDecl(ND); | |||
| 1469 | } | |||
| 1470 | } | |||
| 1471 | ||||
| 1472 | if (Found && S->isTemplateParamScope()) { | |||
| 1473 | R.resolveKind(); | |||
| 1474 | return true; | |||
| 1475 | } | |||
| 1476 | ||||
| 1477 | DeclContext *Ctx = S->getLookupEntity(); | |||
| 1478 | if (Ctx) { | |||
| 1479 | DeclContext *OuterCtx = findOuterContext(S); | |||
| 1480 | for (; Ctx && !Ctx->Equals(OuterCtx); Ctx = Ctx->getLookupParent()) { | |||
| 1481 | // We do not directly look into transparent contexts, since | |||
| 1482 | // those entities will be found in the nearest enclosing | |||
| 1483 | // non-transparent context. | |||
| 1484 | if (Ctx->isTransparentContext()) | |||
| 1485 | continue; | |||
| 1486 | ||||
| 1487 | // If we have a context, and it's not a context stashed in the | |||
| 1488 | // template parameter scope for an out-of-line definition, also | |||
| 1489 | // look into that context. | |||
| 1490 | if (!(Found && S->isTemplateParamScope())) { | |||
| 1491 | assert(Ctx->isFileContext() &&(static_cast <bool> (Ctx->isFileContext() && "We should have been looking only at file context here already." ) ? void (0) : __assert_fail ("Ctx->isFileContext() && \"We should have been looking only at file context here already.\"" , "clang/lib/Sema/SemaLookup.cpp", 1492, __extension__ __PRETTY_FUNCTION__ )) | |||
| 1492 | "We should have been looking only at file context here already.")(static_cast <bool> (Ctx->isFileContext() && "We should have been looking only at file context here already." ) ? void (0) : __assert_fail ("Ctx->isFileContext() && \"We should have been looking only at file context here already.\"" , "clang/lib/Sema/SemaLookup.cpp", 1492, __extension__ __PRETTY_FUNCTION__ )); | |||
| 1493 | ||||
| 1494 | // Look into context considering using-directives. | |||
| 1495 | if (CppNamespaceLookup(*this, R, Context, Ctx, UDirs)) | |||
| 1496 | Found = true; | |||
| 1497 | } | |||
| 1498 | ||||
| 1499 | if (Found) { | |||
| 1500 | R.resolveKind(); | |||
| 1501 | return true; | |||
| 1502 | } | |||
| 1503 | ||||
| 1504 | if (R.isForRedeclaration() && !Ctx->isTransparentContext()) | |||
| 1505 | return false; | |||
| 1506 | } | |||
| 1507 | } | |||
| 1508 | ||||
| 1509 | if (R.isForRedeclaration() && Ctx && !Ctx->isTransparentContext()) | |||
| 1510 | return false; | |||
| 1511 | } | |||
| 1512 | ||||
| 1513 | return !R.empty(); | |||
| 1514 | } | |||
| 1515 | ||||
| 1516 | void Sema::makeMergedDefinitionVisible(NamedDecl *ND) { | |||
| 1517 | if (auto *M = getCurrentModule()) | |||
| 1518 | Context.mergeDefinitionIntoModule(ND, M); | |||
| 1519 | else | |||
| 1520 | // We're not building a module; just make the definition visible. | |||
| 1521 | ND->setVisibleDespiteOwningModule(); | |||
| 1522 | ||||
| 1523 | // If ND is a template declaration, make the template parameters | |||
| 1524 | // visible too. They're not (necessarily) within a mergeable DeclContext. | |||
| 1525 | if (auto *TD = dyn_cast<TemplateDecl>(ND)) | |||
| 1526 | for (auto *Param : *TD->getTemplateParameters()) | |||
| 1527 | makeMergedDefinitionVisible(Param); | |||
| 1528 | } | |||
| 1529 | ||||
| 1530 | /// Find the module in which the given declaration was defined. | |||
| 1531 | static Module *getDefiningModule(Sema &S, Decl *Entity) { | |||
| 1532 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Entity)) { | |||
| 1533 | // If this function was instantiated from a template, the defining module is | |||
| 1534 | // the module containing the pattern. | |||
| 1535 | if (FunctionDecl *Pattern = FD->getTemplateInstantiationPattern()) | |||
| 1536 | Entity = Pattern; | |||
| 1537 | } else if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Entity)) { | |||
| 1538 | if (CXXRecordDecl *Pattern = RD->getTemplateInstantiationPattern()) | |||
| 1539 | Entity = Pattern; | |||
| 1540 | } else if (EnumDecl *ED = dyn_cast<EnumDecl>(Entity)) { | |||
| 1541 | if (auto *Pattern = ED->getTemplateInstantiationPattern()) | |||
| 1542 | Entity = Pattern; | |||
| 1543 | } else if (VarDecl *VD = dyn_cast<VarDecl>(Entity)) { | |||
| 1544 | if (VarDecl *Pattern = VD->getTemplateInstantiationPattern()) | |||
| 1545 | Entity = Pattern; | |||
| 1546 | } | |||
| 1547 | ||||
| 1548 | // Walk up to the containing context. That might also have been instantiated | |||
| 1549 | // from a template. | |||
| 1550 | DeclContext *Context = Entity->getLexicalDeclContext(); | |||
| 1551 | if (Context->isFileContext()) | |||
| 1552 | return S.getOwningModule(Entity); | |||
| 1553 | return getDefiningModule(S, cast<Decl>(Context)); | |||
| 1554 | } | |||
| 1555 | ||||
| 1556 | llvm::DenseSet<Module*> &Sema::getLookupModules() { | |||
| 1557 | unsigned N = CodeSynthesisContexts.size(); | |||
| 1558 | for (unsigned I = CodeSynthesisContextLookupModules.size(); | |||
| 1559 | I != N; ++I) { | |||
| 1560 | Module *M = CodeSynthesisContexts[I].Entity ? | |||
| 1561 | getDefiningModule(*this, CodeSynthesisContexts[I].Entity) : | |||
| 1562 | nullptr; | |||
| 1563 | if (M && !LookupModulesCache.insert(M).second) | |||
| 1564 | M = nullptr; | |||
| 1565 | CodeSynthesisContextLookupModules.push_back(M); | |||
| 1566 | } | |||
| 1567 | return LookupModulesCache; | |||
| 1568 | } | |||
| 1569 | ||||
| 1570 | /// Determine if we could use all the declarations in the module. | |||
| 1571 | bool Sema::isUsableModule(const Module *M) { | |||
| 1572 | assert(M && "We shouldn't check nullness for module here")(static_cast <bool> (M && "We shouldn't check nullness for module here" ) ? void (0) : __assert_fail ("M && \"We shouldn't check nullness for module here\"" , "clang/lib/Sema/SemaLookup.cpp", 1572, __extension__ __PRETTY_FUNCTION__ )); | |||
| 1573 | // Return quickly if we cached the result. | |||
| 1574 | if (UsableModuleUnitsCache.count(M)) | |||
| 1575 | return true; | |||
| 1576 | ||||
| 1577 | // If M is the global module fragment of the current translation unit. So it | |||
| 1578 | // should be usable. | |||
| 1579 | // [module.global.frag]p1: | |||
| 1580 | // The global module fragment can be used to provide declarations that are | |||
| 1581 | // attached to the global module and usable within the module unit. | |||
| 1582 | if (M == TheGlobalModuleFragment || M == TheImplicitGlobalModuleFragment || | |||
| 1583 | M == TheExportedImplicitGlobalModuleFragment || | |||
| 1584 | // If M is the module we're parsing, it should be usable. This covers the | |||
| 1585 | // private module fragment. The private module fragment is usable only if | |||
| 1586 | // it is within the current module unit. And it must be the current | |||
| 1587 | // parsing module unit if it is within the current module unit according | |||
| 1588 | // to the grammar of the private module fragment. NOTE: This is covered by | |||
| 1589 | // the following condition. The intention of the check is to avoid string | |||
| 1590 | // comparison as much as possible. | |||
| 1591 | M == getCurrentModule() || | |||
| 1592 | // The module unit which is in the same module with the current module | |||
| 1593 | // unit is usable. | |||
| 1594 | // | |||
| 1595 | // FIXME: Here we judge if they are in the same module by comparing the | |||
| 1596 | // string. Is there any better solution? | |||
| 1597 | M->getPrimaryModuleInterfaceName() == | |||
| 1598 | llvm::StringRef(getLangOpts().CurrentModule).split(':').first) { | |||
| 1599 | UsableModuleUnitsCache.insert(M); | |||
| 1600 | return true; | |||
| 1601 | } | |||
| 1602 | ||||
| 1603 | return false; | |||
| 1604 | } | |||
| 1605 | ||||
| 1606 | bool Sema::hasVisibleMergedDefinition(const NamedDecl *Def) { | |||
| 1607 | for (const Module *Merged : Context.getModulesWithMergedDefinition(Def)) | |||
| 1608 | if (isModuleVisible(Merged)) | |||
| 1609 | return true; | |||
| 1610 | return false; | |||
| 1611 | } | |||
| 1612 | ||||
| 1613 | bool Sema::hasMergedDefinitionInCurrentModule(const NamedDecl *Def) { | |||
| 1614 | for (const Module *Merged : Context.getModulesWithMergedDefinition(Def)) | |||
| 1615 | if (isUsableModule(Merged)) | |||
| 1616 | return true; | |||
| 1617 | return false; | |||
| 1618 | } | |||
| 1619 | ||||
| 1620 | template <typename ParmDecl> | |||
| 1621 | static bool | |||
| 1622 | hasAcceptableDefaultArgument(Sema &S, const ParmDecl *D, | |||
| 1623 | llvm::SmallVectorImpl<Module *> *Modules, | |||
| 1624 | Sema::AcceptableKind Kind) { | |||
| 1625 | if (!D->hasDefaultArgument()) | |||
| 1626 | return false; | |||
| 1627 | ||||
| 1628 | llvm::SmallPtrSet<const ParmDecl *, 4> Visited; | |||
| 1629 | while (D && Visited.insert(D).second) { | |||
| 1630 | auto &DefaultArg = D->getDefaultArgStorage(); | |||
| 1631 | if (!DefaultArg.isInherited() && S.isAcceptable(D, Kind)) | |||
| 1632 | return true; | |||
| 1633 | ||||
| 1634 | if (!DefaultArg.isInherited() && Modules) { | |||
| 1635 | auto *NonConstD = const_cast<ParmDecl*>(D); | |||
| 1636 | Modules->push_back(S.getOwningModule(NonConstD)); | |||
| 1637 | } | |||
| 1638 | ||||
| 1639 | // If there was a previous default argument, maybe its parameter is | |||
| 1640 | // acceptable. | |||
| 1641 | D = DefaultArg.getInheritedFrom(); | |||
| 1642 | } | |||
| 1643 | return false; | |||
| 1644 | } | |||
| 1645 | ||||
| 1646 | bool Sema::hasAcceptableDefaultArgument( | |||
| 1647 | const NamedDecl *D, llvm::SmallVectorImpl<Module *> *Modules, | |||
| 1648 | Sema::AcceptableKind Kind) { | |||
| 1649 | if (auto *P = dyn_cast<TemplateTypeParmDecl>(D)) | |||
| 1650 | return ::hasAcceptableDefaultArgument(*this, P, Modules, Kind); | |||
| 1651 | ||||
| 1652 | if (auto *P = dyn_cast<NonTypeTemplateParmDecl>(D)) | |||
| 1653 | return ::hasAcceptableDefaultArgument(*this, P, Modules, Kind); | |||
| 1654 | ||||
| 1655 | return ::hasAcceptableDefaultArgument( | |||
| 1656 | *this, cast<TemplateTemplateParmDecl>(D), Modules, Kind); | |||
| 1657 | } | |||
| 1658 | ||||
| 1659 | bool Sema::hasVisibleDefaultArgument(const NamedDecl *D, | |||
| 1660 | llvm::SmallVectorImpl<Module *> *Modules) { | |||
| 1661 | return hasAcceptableDefaultArgument(D, Modules, | |||
| 1662 | Sema::AcceptableKind::Visible); | |||
| 1663 | } | |||
| 1664 | ||||
| 1665 | bool Sema::hasReachableDefaultArgument( | |||
| 1666 | const NamedDecl *D, llvm::SmallVectorImpl<Module *> *Modules) { | |||
| 1667 | return hasAcceptableDefaultArgument(D, Modules, | |||
| 1668 | Sema::AcceptableKind::Reachable); | |||
| 1669 | } | |||
| 1670 | ||||
| 1671 | template <typename Filter> | |||
| 1672 | static bool | |||
| 1673 | hasAcceptableDeclarationImpl(Sema &S, const NamedDecl *D, | |||
| 1674 | llvm::SmallVectorImpl<Module *> *Modules, Filter F, | |||
| 1675 | Sema::AcceptableKind Kind) { | |||
| 1676 | bool HasFilteredRedecls = false; | |||
| 1677 | ||||
| 1678 | for (auto *Redecl : D->redecls()) { | |||
| 1679 | auto *R = cast<NamedDecl>(Redecl); | |||
| 1680 | if (!F(R)) | |||
| 1681 | continue; | |||
| 1682 | ||||
| 1683 | if (S.isAcceptable(R, Kind)) | |||
| 1684 | return true; | |||
| 1685 | ||||
| 1686 | HasFilteredRedecls = true; | |||
| 1687 | ||||
| 1688 | if (Modules) | |||
| 1689 | Modules->push_back(R->getOwningModule()); | |||
| 1690 | } | |||
| 1691 | ||||
| 1692 | // Only return false if there is at least one redecl that is not filtered out. | |||
| 1693 | if (HasFilteredRedecls) | |||
| 1694 | return false; | |||
| 1695 | ||||
| 1696 | return true; | |||
| 1697 | } | |||
| 1698 | ||||
| 1699 | static bool | |||
| 1700 | hasAcceptableExplicitSpecialization(Sema &S, const NamedDecl *D, | |||
| 1701 | llvm::SmallVectorImpl<Module *> *Modules, | |||
| 1702 | Sema::AcceptableKind Kind) { | |||
| 1703 | return hasAcceptableDeclarationImpl( | |||
| 1704 | S, D, Modules, | |||
| 1705 | [](const NamedDecl *D) { | |||
| 1706 | if (auto *RD = dyn_cast<CXXRecordDecl>(D)) | |||
| 1707 | return RD->getTemplateSpecializationKind() == | |||
| 1708 | TSK_ExplicitSpecialization; | |||
| 1709 | if (auto *FD = dyn_cast<FunctionDecl>(D)) | |||
| 1710 | return FD->getTemplateSpecializationKind() == | |||
| 1711 | TSK_ExplicitSpecialization; | |||
| 1712 | if (auto *VD = dyn_cast<VarDecl>(D)) | |||
| 1713 | return VD->getTemplateSpecializationKind() == | |||
| 1714 | TSK_ExplicitSpecialization; | |||
| 1715 | llvm_unreachable("unknown explicit specialization kind")::llvm::llvm_unreachable_internal("unknown explicit specialization kind" , "clang/lib/Sema/SemaLookup.cpp", 1715); | |||
| 1716 | }, | |||
| 1717 | Kind); | |||
| 1718 | } | |||
| 1719 | ||||
| 1720 | bool Sema::hasVisibleExplicitSpecialization( | |||
| 1721 | const NamedDecl *D, llvm::SmallVectorImpl<Module *> *Modules) { | |||
| 1722 | return ::hasAcceptableExplicitSpecialization(*this, D, Modules, | |||
| 1723 | Sema::AcceptableKind::Visible); | |||
| 1724 | } | |||
| 1725 | ||||
| 1726 | bool Sema::hasReachableExplicitSpecialization( | |||
| 1727 | const NamedDecl *D, llvm::SmallVectorImpl<Module *> *Modules) { | |||
| 1728 | return ::hasAcceptableExplicitSpecialization(*this, D, Modules, | |||
| 1729 | Sema::AcceptableKind::Reachable); | |||
| 1730 | } | |||
| 1731 | ||||
| 1732 | static bool | |||
| 1733 | hasAcceptableMemberSpecialization(Sema &S, const NamedDecl *D, | |||
| 1734 | llvm::SmallVectorImpl<Module *> *Modules, | |||
| 1735 | Sema::AcceptableKind Kind) { | |||
| 1736 | assert(isa<CXXRecordDecl>(D->getDeclContext()) &&(static_cast <bool> (isa<CXXRecordDecl>(D->getDeclContext ()) && "not a member specialization") ? void (0) : __assert_fail ("isa<CXXRecordDecl>(D->getDeclContext()) && \"not a member specialization\"" , "clang/lib/Sema/SemaLookup.cpp", 1737, __extension__ __PRETTY_FUNCTION__ )) | |||
| 1737 | "not a member specialization")(static_cast <bool> (isa<CXXRecordDecl>(D->getDeclContext ()) && "not a member specialization") ? void (0) : __assert_fail ("isa<CXXRecordDecl>(D->getDeclContext()) && \"not a member specialization\"" , "clang/lib/Sema/SemaLookup.cpp", 1737, __extension__ __PRETTY_FUNCTION__ )); | |||
| 1738 | return hasAcceptableDeclarationImpl( | |||
| 1739 | S, D, Modules, | |||
| 1740 | [](const NamedDecl *D) { | |||
| 1741 | // If the specialization is declared at namespace scope, then it's a | |||
| 1742 | // member specialization declaration. If it's lexically inside the class | |||
| 1743 | // definition then it was instantiated. | |||
| 1744 | // | |||
| 1745 | // FIXME: This is a hack. There should be a better way to determine | |||
| 1746 | // this. | |||
| 1747 | // FIXME: What about MS-style explicit specializations declared within a | |||
| 1748 | // class definition? | |||
| 1749 | return D->getLexicalDeclContext()->isFileContext(); | |||
| 1750 | }, | |||
| 1751 | Kind); | |||
| 1752 | } | |||
| 1753 | ||||
| 1754 | bool Sema::hasVisibleMemberSpecialization( | |||
| 1755 | const NamedDecl *D, llvm::SmallVectorImpl<Module *> *Modules) { | |||
| 1756 | return hasAcceptableMemberSpecialization(*this, D, Modules, | |||
| 1757 | Sema::AcceptableKind::Visible); | |||
| 1758 | } | |||
| 1759 | ||||
| 1760 | bool Sema::hasReachableMemberSpecialization( | |||
| 1761 | const NamedDecl *D, llvm::SmallVectorImpl<Module *> *Modules) { | |||
| 1762 | return hasAcceptableMemberSpecialization(*this, D, Modules, | |||
| 1763 | Sema::AcceptableKind::Reachable); | |||
| 1764 | } | |||
| 1765 | ||||
| 1766 | /// Determine whether a declaration is acceptable to name lookup. | |||
| 1767 | /// | |||
| 1768 | /// This routine determines whether the declaration D is acceptable in the | |||
| 1769 | /// current lookup context, taking into account the current template | |||
| 1770 | /// instantiation stack. During template instantiation, a declaration is | |||
| 1771 | /// acceptable if it is acceptable from a module containing any entity on the | |||
| 1772 | /// template instantiation path (by instantiating a template, you allow it to | |||
| 1773 | /// see the declarations that your module can see, including those later on in | |||
| 1774 | /// your module). | |||
| 1775 | bool LookupResult::isAcceptableSlow(Sema &SemaRef, NamedDecl *D, | |||
| 1776 | Sema::AcceptableKind Kind) { | |||
| 1777 | assert(!D->isUnconditionallyVisible() &&(static_cast <bool> (!D->isUnconditionallyVisible() && "should not call this: not in slow case") ? void (0) : __assert_fail ("!D->isUnconditionallyVisible() && \"should not call this: not in slow case\"" , "clang/lib/Sema/SemaLookup.cpp", 1778, __extension__ __PRETTY_FUNCTION__ )) | |||
| 1778 | "should not call this: not in slow case")(static_cast <bool> (!D->isUnconditionallyVisible() && "should not call this: not in slow case") ? void (0) : __assert_fail ("!D->isUnconditionallyVisible() && \"should not call this: not in slow case\"" , "clang/lib/Sema/SemaLookup.cpp", 1778, __extension__ __PRETTY_FUNCTION__ )); | |||
| 1779 | ||||
| 1780 | Module *DeclModule = SemaRef.getOwningModule(D); | |||
| 1781 | assert(DeclModule && "hidden decl has no owning module")(static_cast <bool> (DeclModule && "hidden decl has no owning module" ) ? void (0) : __assert_fail ("DeclModule && \"hidden decl has no owning module\"" , "clang/lib/Sema/SemaLookup.cpp", 1781, __extension__ __PRETTY_FUNCTION__ )); | |||
| 1782 | ||||
| 1783 | // If the owning module is visible, the decl is acceptable. | |||
| 1784 | if (SemaRef.isModuleVisible(DeclModule, | |||
| 1785 | D->isInvisibleOutsideTheOwningModule())) | |||
| 1786 | return true; | |||
| 1787 | ||||
| 1788 | // Determine whether a decl context is a file context for the purpose of | |||
| 1789 | // visibility/reachability. This looks through some (export and linkage spec) | |||
| 1790 | // transparent contexts, but not others (enums). | |||
| 1791 | auto IsEffectivelyFileContext = [](const DeclContext *DC) { | |||
| 1792 | return DC->isFileContext() || isa<LinkageSpecDecl>(DC) || | |||
| 1793 | isa<ExportDecl>(DC); | |||
| 1794 | }; | |||
| 1795 | ||||
| 1796 | // If this declaration is not at namespace scope | |||
| 1797 | // then it is acceptable if its lexical parent has a acceptable definition. | |||
| 1798 | DeclContext *DC = D->getLexicalDeclContext(); | |||
| 1799 | if (DC && !IsEffectivelyFileContext(DC)) { | |||
| 1800 | // For a parameter, check whether our current template declaration's | |||
| 1801 | // lexical context is acceptable, not whether there's some other acceptable | |||
| 1802 | // definition of it, because parameters aren't "within" the definition. | |||
| 1803 | // | |||
| 1804 | // In C++ we need to check for a acceptable definition due to ODR merging, | |||
| 1805 | // and in C we must not because each declaration of a function gets its own | |||
| 1806 | // set of declarations for tags in prototype scope. | |||
| 1807 | bool AcceptableWithinParent; | |||
| 1808 | if (D->isTemplateParameter()) { | |||
| 1809 | bool SearchDefinitions = true; | |||
| 1810 | if (const auto *DCD = dyn_cast<Decl>(DC)) { | |||
| 1811 | if (const auto *TD = DCD->getDescribedTemplate()) { | |||
| 1812 | TemplateParameterList *TPL = TD->getTemplateParameters(); | |||
| 1813 | auto Index = getDepthAndIndex(D).second; | |||
| 1814 | SearchDefinitions = Index >= TPL->size() || TPL->getParam(Index) != D; | |||
| 1815 | } | |||
| 1816 | } | |||
| 1817 | if (SearchDefinitions) | |||
| 1818 | AcceptableWithinParent = | |||
| 1819 | SemaRef.hasAcceptableDefinition(cast<NamedDecl>(DC), Kind); | |||
| 1820 | else | |||
| 1821 | AcceptableWithinParent = | |||
| 1822 | isAcceptable(SemaRef, cast<NamedDecl>(DC), Kind); | |||
| 1823 | } else if (isa<ParmVarDecl>(D) || | |||
| 1824 | (isa<FunctionDecl>(DC) && !SemaRef.getLangOpts().CPlusPlus)) | |||
| 1825 | AcceptableWithinParent = isAcceptable(SemaRef, cast<NamedDecl>(DC), Kind); | |||
| 1826 | else if (D->isModulePrivate()) { | |||
| 1827 | // A module-private declaration is only acceptable if an enclosing lexical | |||
| 1828 | // parent was merged with another definition in the current module. | |||
| 1829 | AcceptableWithinParent = false; | |||
| 1830 | do { | |||
| 1831 | if (SemaRef.hasMergedDefinitionInCurrentModule(cast<NamedDecl>(DC))) { | |||
| 1832 | AcceptableWithinParent = true; | |||
| 1833 | break; | |||
| 1834 | } | |||
| 1835 | DC = DC->getLexicalParent(); | |||
| 1836 | } while (!IsEffectivelyFileContext(DC)); | |||
| 1837 | } else { | |||
| 1838 | AcceptableWithinParent = | |||
| 1839 | SemaRef.hasAcceptableDefinition(cast<NamedDecl>(DC), Kind); | |||
| 1840 | } | |||
| 1841 | ||||
| 1842 | if (AcceptableWithinParent && SemaRef.CodeSynthesisContexts.empty() && | |||
| 1843 | Kind == Sema::AcceptableKind::Visible && | |||
| 1844 | // FIXME: Do something better in this case. | |||
| 1845 | !SemaRef.getLangOpts().ModulesLocalVisibility) { | |||
| 1846 | // Cache the fact that this declaration is implicitly visible because | |||
| 1847 | // its parent has a visible definition. | |||
| 1848 | D->setVisibleDespiteOwningModule(); | |||
| 1849 | } | |||
| 1850 | return AcceptableWithinParent; | |||
| 1851 | } | |||
| 1852 | ||||
| 1853 | if (Kind == Sema::AcceptableKind::Visible) | |||
| 1854 | return false; | |||
| 1855 | ||||
| 1856 | assert(Kind == Sema::AcceptableKind::Reachable &&(static_cast <bool> (Kind == Sema::AcceptableKind::Reachable && "Additional Sema::AcceptableKind?") ? void (0) : __assert_fail ("Kind == Sema::AcceptableKind::Reachable && \"Additional Sema::AcceptableKind?\"" , "clang/lib/Sema/SemaLookup.cpp", 1857, __extension__ __PRETTY_FUNCTION__ )) | |||
| 1857 | "Additional Sema::AcceptableKind?")(static_cast <bool> (Kind == Sema::AcceptableKind::Reachable && "Additional Sema::AcceptableKind?") ? void (0) : __assert_fail ("Kind == Sema::AcceptableKind::Reachable && \"Additional Sema::AcceptableKind?\"" , "clang/lib/Sema/SemaLookup.cpp", 1857, __extension__ __PRETTY_FUNCTION__ )); | |||
| 1858 | return isReachableSlow(SemaRef, D); | |||
| 1859 | } | |||
| 1860 | ||||
| 1861 | bool Sema::isModuleVisible(const Module *M, bool ModulePrivate) { | |||
| 1862 | // The module might be ordinarily visible. For a module-private query, that | |||
| 1863 | // means it is part of the current module. | |||
| 1864 | if (ModulePrivate && isUsableModule(M)) | |||
| 1865 | return true; | |||
| 1866 | ||||
| 1867 | // For a query which is not module-private, that means it is in our visible | |||
| 1868 | // module set. | |||
| 1869 | if (!ModulePrivate && VisibleModules.isVisible(M)) | |||
| 1870 | return true; | |||
| 1871 | ||||
| 1872 | // Otherwise, it might be visible by virtue of the query being within a | |||
| 1873 | // template instantiation or similar that is permitted to look inside M. | |||
| 1874 | ||||
| 1875 | // Find the extra places where we need to look. | |||
| 1876 | const auto &LookupModules = getLookupModules(); | |||
| 1877 | if (LookupModules.empty()) | |||
| 1878 | return false; | |||
| 1879 | ||||
| 1880 | // If our lookup set contains the module, it's visible. | |||
| 1881 | if (LookupModules.count(M)) | |||
| 1882 | return true; | |||
| 1883 | ||||
| 1884 | // The global module fragments are visible to its corresponding module unit. | |||
| 1885 | // So the global module fragment should be visible if the its corresponding | |||
| 1886 | // module unit is visible. | |||
| 1887 | if (M->isGlobalModule() && LookupModules.count(M->getTopLevelModule())) | |||
| 1888 | return true; | |||
| 1889 | ||||
| 1890 | // For a module-private query, that's everywhere we get to look. | |||
| 1891 | if (ModulePrivate) | |||
| 1892 | return false; | |||
| 1893 | ||||
| 1894 | // Check whether M is transitively exported to an import of the lookup set. | |||
| 1895 | return llvm::any_of(LookupModules, [&](const Module *LookupM) { | |||
| 1896 | return LookupM->isModuleVisible(M); | |||
| 1897 | }); | |||
| 1898 | } | |||
| 1899 | ||||
| 1900 | // FIXME: Return false directly if we don't have an interface dependency on the | |||
| 1901 | // translation unit containing D. | |||
| 1902 | bool LookupResult::isReachableSlow(Sema &SemaRef, NamedDecl *D) { | |||
| 1903 | assert(!isVisible(SemaRef, D) && "Shouldn't call the slow case.\n")(static_cast <bool> (!isVisible(SemaRef, D) && "Shouldn't call the slow case.\n" ) ? void (0) : __assert_fail ("!isVisible(SemaRef, D) && \"Shouldn't call the slow case.\\n\"" , "clang/lib/Sema/SemaLookup.cpp", 1903, __extension__ __PRETTY_FUNCTION__ )); | |||
| 1904 | ||||
| 1905 | Module *DeclModule = SemaRef.getOwningModule(D); | |||
| 1906 | assert(DeclModule && "hidden decl has no owning module")(static_cast <bool> (DeclModule && "hidden decl has no owning module" ) ? void (0) : __assert_fail ("DeclModule && \"hidden decl has no owning module\"" , "clang/lib/Sema/SemaLookup.cpp", 1906, __extension__ __PRETTY_FUNCTION__ )); | |||
| 1907 | ||||
| 1908 | // Entities in header like modules are reachable only if they're visible. | |||
| 1909 | if (DeclModule->isHeaderLikeModule()) | |||
| 1910 | return false; | |||
| 1911 | ||||
| 1912 | if (!D->isInAnotherModuleUnit()) | |||
| 1913 | return true; | |||
| 1914 | ||||
| 1915 | // [module.reach]/p3: | |||
| 1916 | // A declaration D is reachable from a point P if: | |||
| 1917 | // ... | |||
| 1918 | // - D is not discarded ([module.global.frag]), appears in a translation unit | |||
| 1919 | // that is reachable from P, and does not appear within a private module | |||
| 1920 | // fragment. | |||
| 1921 | // | |||
| 1922 | // A declaration that's discarded in the GMF should be module-private. | |||
| 1923 | if (D->isModulePrivate()) | |||
| 1924 | return false; | |||
| 1925 | ||||
| 1926 | // [module.reach]/p1 | |||
| 1927 | // A translation unit U is necessarily reachable from a point P if U is a | |||
| 1928 | // module interface unit on which the translation unit containing P has an | |||
| 1929 | // interface dependency, or the translation unit containing P imports U, in | |||
| 1930 | // either case prior to P ([module.import]). | |||
| 1931 | // | |||
| 1932 | // [module.import]/p10 | |||
| 1933 | // A translation unit has an interface dependency on a translation unit U if | |||
| 1934 | // it contains a declaration (possibly a module-declaration) that imports U | |||
| 1935 | // or if it has an interface dependency on a translation unit that has an | |||
| 1936 | // interface dependency on U. | |||
| 1937 | // | |||
| 1938 | // So we could conclude the module unit U is necessarily reachable if: | |||
| 1939 | // (1) The module unit U is module interface unit. | |||
| 1940 | // (2) The current unit has an interface dependency on the module unit U. | |||
| 1941 | // | |||
| 1942 | // Here we only check for the first condition. Since we couldn't see | |||
| 1943 | // DeclModule if it isn't (transitively) imported. | |||
| 1944 | if (DeclModule->getTopLevelModule()->isModuleInterfaceUnit()) | |||
| 1945 | return true; | |||
| 1946 | ||||
| 1947 | // [module.reach]/p2 | |||
| 1948 | // Additional translation units on | |||
| 1949 | // which the point within the program has an interface dependency may be | |||
| 1950 | // considered reachable, but it is unspecified which are and under what | |||
| 1951 | // circumstances. | |||
| 1952 | // | |||
| 1953 | // The decision here is to treat all additional tranditional units as | |||
| 1954 | // unreachable. | |||
| 1955 | return false; | |||
| 1956 | } | |||
| 1957 | ||||
| 1958 | bool Sema::isAcceptableSlow(const NamedDecl *D, Sema::AcceptableKind Kind) { | |||
| 1959 | return LookupResult::isAcceptable(*this, const_cast<NamedDecl *>(D), Kind); | |||
| 1960 | } | |||
| 1961 | ||||
| 1962 | bool Sema::shouldLinkPossiblyHiddenDecl(LookupResult &R, const NamedDecl *New) { | |||
| 1963 | // FIXME: If there are both visible and hidden declarations, we need to take | |||
| 1964 | // into account whether redeclaration is possible. Example: | |||
| 1965 | // | |||
| 1966 | // Non-imported module: | |||
| 1967 | // int f(T); // #1 | |||
| 1968 | // Some TU: | |||
| 1969 | // static int f(U); // #2, not a redeclaration of #1 | |||
| 1970 | // int f(T); // #3, finds both, should link with #1 if T != U, but | |||
| 1971 | // // with #2 if T == U; neither should be ambiguous. | |||
| 1972 | for (auto *D : R) { | |||
| 1973 | if (isVisible(D)) | |||
| 1974 | return true; | |||
| 1975 | assert(D->isExternallyDeclarable() &&(static_cast <bool> (D->isExternallyDeclarable() && "should not have hidden, non-externally-declarable result here" ) ? void (0) : __assert_fail ("D->isExternallyDeclarable() && \"should not have hidden, non-externally-declarable result here\"" , "clang/lib/Sema/SemaLookup.cpp", 1976, __extension__ __PRETTY_FUNCTION__ )) | |||
| 1976 | "should not have hidden, non-externally-declarable result here")(static_cast <bool> (D->isExternallyDeclarable() && "should not have hidden, non-externally-declarable result here" ) ? void (0) : __assert_fail ("D->isExternallyDeclarable() && \"should not have hidden, non-externally-declarable result here\"" , "clang/lib/Sema/SemaLookup.cpp", 1976, __extension__ __PRETTY_FUNCTION__ )); | |||
| 1977 | } | |||
| 1978 | ||||
| 1979 | // This function is called once "New" is essentially complete, but before a | |||
| 1980 | // previous declaration is attached. We can't query the linkage of "New" in | |||
| 1981 | // general, because attaching the previous declaration can change the | |||
| 1982 | // linkage of New to match the previous declaration. | |||
| 1983 | // | |||
| 1984 | // However, because we've just determined that there is no *visible* prior | |||
| 1985 | // declaration, we can compute the linkage here. There are two possibilities: | |||
| 1986 | // | |||
| 1987 | // * This is not a redeclaration; it's safe to compute the linkage now. | |||
| 1988 | // | |||
| 1989 | // * This is a redeclaration of a prior declaration that is externally | |||
| 1990 | // redeclarable. In that case, the linkage of the declaration is not | |||
| 1991 | // changed by attaching the prior declaration, because both are externally | |||
| 1992 | // declarable (and thus ExternalLinkage or VisibleNoLinkage). | |||
| 1993 | // | |||
| 1994 | // FIXME: This is subtle and fragile. | |||
| 1995 | return New->isExternallyDeclarable(); | |||
| 1996 | } | |||
| 1997 | ||||
| 1998 | /// Retrieve the visible declaration corresponding to D, if any. | |||
| 1999 | /// | |||
| 2000 | /// This routine determines whether the declaration D is visible in the current | |||
| 2001 | /// module, with the current imports. If not, it checks whether any | |||
| 2002 | /// redeclaration of D is visible, and if so, returns that declaration. | |||
| 2003 | /// | |||
| 2004 | /// \returns D, or a visible previous declaration of D, whichever is more recent | |||
| 2005 | /// and visible. If no declaration of D is visible, returns null. | |||
| 2006 | static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D, | |||
| 2007 | unsigned IDNS) { | |||
| 2008 | assert(!LookupResult::isAvailableForLookup(SemaRef, D) && "not in slow case")(static_cast <bool> (!LookupResult::isAvailableForLookup (SemaRef, D) && "not in slow case") ? void (0) : __assert_fail ("!LookupResult::isAvailableForLookup(SemaRef, D) && \"not in slow case\"" , "clang/lib/Sema/SemaLookup.cpp", 2008, __extension__ __PRETTY_FUNCTION__ )); | |||
| 2009 | ||||
| 2010 | for (auto *RD : D->redecls()) { | |||
| 2011 | // Don't bother with extra checks if we already know this one isn't visible. | |||
| 2012 | if (RD == D) | |||
| 2013 | continue; | |||
| 2014 | ||||
| 2015 | auto ND = cast<NamedDecl>(RD); | |||
| 2016 | // FIXME: This is wrong in the case where the previous declaration is not | |||
| 2017 | // visible in the same scope as D. This needs to be done much more | |||
| 2018 | // carefully. | |||
| 2019 | if (ND->isInIdentifierNamespace(IDNS) && | |||
| 2020 | LookupResult::isAvailableForLookup(SemaRef, ND)) | |||
| 2021 | return ND; | |||
| 2022 | } | |||
| 2023 | ||||
| 2024 | return nullptr; | |||
| 2025 | } | |||
| 2026 | ||||
| 2027 | bool Sema::hasVisibleDeclarationSlow(const NamedDecl *D, | |||
| 2028 | llvm::SmallVectorImpl<Module *> *Modules) { | |||
| 2029 | assert(!isVisible(D) && "not in slow case")(static_cast <bool> (!isVisible(D) && "not in slow case" ) ? void (0) : __assert_fail ("!isVisible(D) && \"not in slow case\"" , "clang/lib/Sema/SemaLookup.cpp", 2029, __extension__ __PRETTY_FUNCTION__ )); | |||
| 2030 | return hasAcceptableDeclarationImpl( | |||
| 2031 | *this, D, Modules, [](const NamedDecl *) { return true; }, | |||
| 2032 | Sema::AcceptableKind::Visible); | |||
| 2033 | } | |||
| 2034 | ||||
| 2035 | bool Sema::hasReachableDeclarationSlow( | |||
| 2036 | const NamedDecl *D, llvm::SmallVectorImpl<Module *> *Modules) { | |||
| 2037 | assert(!isReachable(D) && "not in slow case")(static_cast <bool> (!isReachable(D) && "not in slow case" ) ? void (0) : __assert_fail ("!isReachable(D) && \"not in slow case\"" , "clang/lib/Sema/SemaLookup.cpp", 2037, __extension__ __PRETTY_FUNCTION__ )); | |||
| 2038 | return hasAcceptableDeclarationImpl( | |||
| 2039 | *this, D, Modules, [](const NamedDecl *) { return true; }, | |||
| 2040 | Sema::AcceptableKind::Reachable); | |||
| 2041 | } | |||
| 2042 | ||||
| 2043 | NamedDecl *LookupResult::getAcceptableDeclSlow(NamedDecl *D) const { | |||
| 2044 | if (auto *ND = dyn_cast<NamespaceDecl>(D)) { | |||
| 2045 | // Namespaces are a bit of a special case: we expect there to be a lot of | |||
| 2046 | // redeclarations of some namespaces, all declarations of a namespace are | |||
| 2047 | // essentially interchangeable, all declarations are found by name lookup | |||
| 2048 | // if any is, and namespaces are never looked up during template | |||
| 2049 | // instantiation. So we benefit from caching the check in this case, and | |||
| 2050 | // it is correct to do so. | |||
| 2051 | auto *Key = ND->getCanonicalDecl(); | |||
| 2052 | if (auto *Acceptable = getSema().VisibleNamespaceCache.lookup(Key)) | |||
| 2053 | return Acceptable; | |||
| 2054 | auto *Acceptable = isVisible(getSema(), Key) | |||
| 2055 | ? Key | |||
| 2056 | : findAcceptableDecl(getSema(), Key, IDNS); | |||
| 2057 | if (Acceptable) | |||
| 2058 | getSema().VisibleNamespaceCache.insert(std::make_pair(Key, Acceptable)); | |||
| 2059 | return Acceptable; | |||
| 2060 | } | |||
| 2061 | ||||
| 2062 | return findAcceptableDecl(getSema(), D, IDNS); | |||
| 2063 | } | |||
| 2064 | ||||
| 2065 | bool LookupResult::isVisible(Sema &SemaRef, NamedDecl *D) { | |||
| 2066 | // If this declaration is already visible, return it directly. | |||
| 2067 | if (D->isUnconditionallyVisible()) | |||
| 2068 | return true; | |||
| 2069 | ||||
| 2070 | // During template instantiation, we can refer to hidden declarations, if | |||
| 2071 | // they were visible in any module along the path of instantiation. | |||
| 2072 | return isAcceptableSlow(SemaRef, D, Sema::AcceptableKind::Visible); | |||
| 2073 | } | |||
| 2074 | ||||
| 2075 | bool LookupResult::isReachable(Sema &SemaRef, NamedDecl *D) { | |||
| 2076 | if (D->isUnconditionallyVisible()) | |||
| 2077 | return true; | |||
| 2078 | ||||
| 2079 | return isAcceptableSlow(SemaRef, D, Sema::AcceptableKind::Reachable); | |||
| 2080 | } | |||
| 2081 | ||||
| 2082 | bool LookupResult::isAvailableForLookup(Sema &SemaRef, NamedDecl *ND) { | |||
| 2083 | // We should check the visibility at the callsite already. | |||
| 2084 | if (isVisible(SemaRef, ND)) | |||
| 2085 | return true; | |||
| 2086 | ||||
| 2087 | // Deduction guide lives in namespace scope generally, but it is just a | |||
| 2088 | // hint to the compilers. What we actually lookup for is the generated member | |||
| 2089 | // of the corresponding template. So it is sufficient to check the | |||
| 2090 | // reachability of the template decl. | |||
| 2091 | if (auto *DeductionGuide = ND->getDeclName().getCXXDeductionGuideTemplate()) | |||
| 2092 | return SemaRef.hasReachableDefinition(DeductionGuide); | |||
| 2093 | ||||
| 2094 | // FIXME: The lookup for allocation function is a standalone process. | |||
| 2095 | // (We can find the logics in Sema::FindAllocationFunctions) | |||
| 2096 | // | |||
| 2097 | // Such structure makes it a problem when we instantiate a template | |||
| 2098 | // declaration using placement allocation function if the placement | |||
| 2099 | // allocation function is invisible. | |||
| 2100 | // (See https://github.com/llvm/llvm-project/issues/59601) | |||
| 2101 | // | |||
| 2102 | // Here we workaround it by making the placement allocation functions | |||
| 2103 | // always acceptable. The downside is that we can't diagnose the direct | |||
| 2104 | // use of the invisible placement allocation functions. (Although such uses | |||
| 2105 | // should be rare). | |||
| 2106 | if (auto *FD = dyn_cast<FunctionDecl>(ND); | |||
| 2107 | FD && FD->isReservedGlobalPlacementOperator()) | |||
| 2108 | return true; | |||
| 2109 | ||||
| 2110 | auto *DC = ND->getDeclContext(); | |||
| 2111 | // If ND is not visible and it is at namespace scope, it shouldn't be found | |||
| 2112 | // by name lookup. | |||
| 2113 | if (DC->isFileContext()) | |||
| 2114 | return false; | |||
| 2115 | ||||
| 2116 | // [module.interface]p7 | |||
| 2117 | // Class and enumeration member names can be found by name lookup in any | |||
| 2118 | // context in which a definition of the type is reachable. | |||
| 2119 | // | |||
| 2120 | // FIXME: The current implementation didn't consider about scope. For example, | |||
| 2121 | // ``` | |||
| 2122 | // // m.cppm | |||
| 2123 | // export module m; | |||
| 2124 | // enum E1 { e1 }; | |||
| 2125 | // // Use.cpp | |||
| 2126 | // import m; | |||
| 2127 | // void test() { | |||
| 2128 | // auto a = E1::e1; // Error as expected. | |||
| 2129 | // auto b = e1; // Should be error. namespace-scope name e1 is not visible | |||
| 2130 | // } | |||
| 2131 | // ``` | |||
| 2132 | // For the above example, the current implementation would emit error for `a` | |||
| 2133 | // correctly. However, the implementation wouldn't diagnose about `b` now. | |||
| 2134 | // Since we only check the reachability for the parent only. | |||
| 2135 | // See clang/test/CXX/module/module.interface/p7.cpp for example. | |||
| 2136 | if (auto *TD = dyn_cast<TagDecl>(DC)) | |||
| 2137 | return SemaRef.hasReachableDefinition(TD); | |||
| 2138 | ||||
| 2139 | return false; | |||
| 2140 | } | |||
| 2141 | ||||
| 2142 | /// Perform unqualified name lookup starting from a given | |||
| 2143 | /// scope. | |||
| 2144 | /// | |||
| 2145 | /// Unqualified name lookup (C++ [basic.lookup.unqual], C99 6.2.1) is | |||
| 2146 | /// used to find names within the current scope. For example, 'x' in | |||
| 2147 | /// @code | |||
| 2148 | /// int x; | |||
| 2149 | /// int f() { | |||
| 2150 | /// return x; // unqualified name look finds 'x' in the global scope | |||
| 2151 | /// } | |||
| 2152 | /// @endcode | |||
| 2153 | /// | |||
| 2154 | /// Different lookup criteria can find different names. For example, a | |||
| 2155 | /// particular scope can have both a struct and a function of the same | |||
| 2156 | /// name, and each can be found by certain lookup criteria. For more | |||
| 2157 | /// information about lookup criteria, see the documentation for the | |||
| 2158 | /// class LookupCriteria. | |||
| 2159 | /// | |||
| 2160 | /// @param S The scope from which unqualified name lookup will | |||
| 2161 | /// begin. If the lookup criteria permits, name lookup may also search | |||
| 2162 | /// in the parent scopes. | |||
| 2163 | /// | |||
| 2164 | /// @param [in,out] R Specifies the lookup to perform (e.g., the name to | |||
| 2165 | /// look up and the lookup kind), and is updated with the results of lookup | |||
| 2166 | /// including zero or more declarations and possibly additional information | |||
| 2167 | /// used to diagnose ambiguities. | |||
| 2168 | /// | |||
| 2169 | /// @returns \c true if lookup succeeded and false otherwise. | |||
| 2170 | bool Sema::LookupName(LookupResult &R, Scope *S, bool AllowBuiltinCreation, | |||
| 2171 | bool ForceNoCPlusPlus) { | |||
| 2172 | DeclarationName Name = R.getLookupName(); | |||
| 2173 | if (!Name) return false; | |||
| 2174 | ||||
| 2175 | LookupNameKind NameKind = R.getLookupKind(); | |||
| 2176 | ||||
| 2177 | if (!getLangOpts().CPlusPlus || ForceNoCPlusPlus) { | |||
| 2178 | // Unqualified name lookup in C/Objective-C is purely lexical, so | |||
| 2179 | // search in the declarations attached to the name. | |||
| 2180 | if (NameKind == Sema::LookupRedeclarationWithLinkage) { | |||
| 2181 | // Find the nearest non-transparent declaration scope. | |||
| 2182 | while (!(S->getFlags() & Scope::DeclScope) || | |||
| 2183 | (S->getEntity() && S->getEntity()->isTransparentContext())) | |||
| 2184 | S = S->getParent(); | |||
| 2185 | } | |||
| 2186 | ||||
| 2187 | // When performing a scope lookup, we want to find local extern decls. | |||
| 2188 | FindLocalExternScope FindLocals(R); | |||
| 2189 | ||||
| 2190 | // Scan up the scope chain looking for a decl that matches this | |||
| 2191 | // identifier that is in the appropriate namespace. This search | |||
| 2192 | // should not take long, as shadowing of names is uncommon, and | |||
| 2193 | // deep shadowing is extremely uncommon. | |||
| 2194 | bool LeftStartingScope = false; | |||
| 2195 | ||||
| 2196 | for (IdentifierResolver::iterator I = IdResolver.begin(Name), | |||
| 2197 | IEnd = IdResolver.end(); | |||
| 2198 | I != IEnd; ++I) | |||
| 2199 | if (NamedDecl *D = R.getAcceptableDecl(*I)) { | |||
| 2200 | if (NameKind == LookupRedeclarationWithLinkage) { | |||
| 2201 | // Determine whether this (or a previous) declaration is | |||
| 2202 | // out-of-scope. | |||
| 2203 | if (!LeftStartingScope && !S->isDeclScope(*I)) | |||
| 2204 | LeftStartingScope = true; | |||
| 2205 | ||||
| 2206 | // If we found something outside of our starting scope that | |||
| 2207 | // does not have linkage, skip it. | |||
| 2208 | if (LeftStartingScope && !((*I)->hasLinkage())) { | |||
| 2209 | R.setShadowed(); | |||
| 2210 | continue; | |||
| 2211 | } | |||
| 2212 | } | |||
| 2213 | else if (NameKind == LookupObjCImplicitSelfParam && | |||
| 2214 | !isa<ImplicitParamDecl>(*I)) | |||
| 2215 | continue; | |||
| 2216 | ||||
| 2217 | R.addDecl(D); | |||
| 2218 | ||||
| 2219 | // Check whether there are any other declarations with the same name | |||
| 2220 | // and in the same scope. | |||
| 2221 | if (I != IEnd) { | |||
| 2222 | // Find the scope in which this declaration was declared (if it | |||
| 2223 | // actually exists in a Scope). | |||
| 2224 | while (S && !S->isDeclScope(D)) | |||
| 2225 | S = S->getParent(); | |||
| 2226 | ||||
| 2227 | // If the scope containing the declaration is the translation unit, | |||
| 2228 | // then we'll need to perform our checks based on the matching | |||
| 2229 | // DeclContexts rather than matching scopes. | |||
| 2230 | if (S && isNamespaceOrTranslationUnitScope(S)) | |||
| 2231 | S = nullptr; | |||
| 2232 | ||||
| 2233 | // Compute the DeclContext, if we need it. | |||
| 2234 | DeclContext *DC = nullptr; | |||
| 2235 | if (!S) | |||
| 2236 | DC = (*I)->getDeclContext()->getRedeclContext(); | |||
| 2237 | ||||
| 2238 | IdentifierResolver::iterator LastI = I; | |||
| 2239 | for (++LastI; LastI != IEnd; ++LastI) { | |||
| 2240 | if (S) { | |||
| 2241 | // Match based on scope. | |||
| 2242 | if (!S->isDeclScope(*LastI)) | |||
| 2243 | break; | |||
| 2244 | } else { | |||
| 2245 | // Match based on DeclContext. | |||
| 2246 | DeclContext *LastDC | |||
| 2247 | = (*LastI)->getDeclContext()->getRedeclContext(); | |||
| 2248 | if (!LastDC->Equals(DC)) | |||
| 2249 | break; | |||
| 2250 | } | |||
| 2251 | ||||
| 2252 | // If the declaration is in the right namespace and visible, add it. | |||
| 2253 | if (NamedDecl *LastD = R.getAcceptableDecl(*LastI)) | |||
| 2254 | R.addDecl(LastD); | |||
| 2255 | } | |||
| 2256 | ||||
| 2257 | R.resolveKind(); | |||
| 2258 | } | |||
| 2259 | ||||
| 2260 | return true; | |||
| 2261 | } | |||
| 2262 | } else { | |||
| 2263 | // Perform C++ unqualified name lookup. | |||
| 2264 | if (CppLookupName(R, S)) | |||
| 2265 | return true; | |||
| 2266 | } | |||
| 2267 | ||||
| 2268 | // If we didn't find a use of this identifier, and if the identifier | |||
| 2269 | // corresponds to a compiler builtin, create the decl object for the builtin | |||
| 2270 | // now, injecting it into translation unit scope, and return it. | |||
| 2271 | if (AllowBuiltinCreation && LookupBuiltin(R)) | |||
| 2272 | return true; | |||
| 2273 | ||||
| 2274 | // If we didn't find a use of this identifier, the ExternalSource | |||
| 2275 | // may be able to handle the situation. | |||
| 2276 | // Note: some lookup failures are expected! | |||
| 2277 | // See e.g. R.isForRedeclaration(). | |||
| 2278 | return (ExternalSource && ExternalSource->LookupUnqualified(R, S)); | |||
| 2279 | } | |||
| 2280 | ||||
| 2281 | /// Perform qualified name lookup in the namespaces nominated by | |||
| 2282 | /// using directives by the given context. | |||
| 2283 | /// | |||
| 2284 | /// C++98 [namespace.qual]p2: | |||
| 2285 | /// Given X::m (where X is a user-declared namespace), or given \::m | |||
| 2286 | /// (where X is the global namespace), let S be the set of all | |||
| 2287 | /// declarations of m in X and in the transitive closure of all | |||
| 2288 | /// namespaces nominated by using-directives in X and its used | |||
| 2289 | /// namespaces, except that using-directives are ignored in any | |||
| 2290 | /// namespace, including X, directly containing one or more | |||
| 2291 | /// declarations of m. No namespace is searched more than once in | |||
| 2292 | /// the lookup of a name. If S is the empty set, the program is | |||
| 2293 | /// ill-formed. Otherwise, if S has exactly one member, or if the | |||
| 2294 | /// context of the reference is a using-declaration | |||
| 2295 | /// (namespace.udecl), S is the required set of declarations of | |||
| 2296 | /// m. Otherwise if the use of m is not one that allows a unique | |||
| 2297 | /// declaration to be chosen from S, the program is ill-formed. | |||
| 2298 | /// | |||
| 2299 | /// C++98 [namespace.qual]p5: | |||
| 2300 | /// During the lookup of a qualified namespace member name, if the | |||
| 2301 | /// lookup finds more than one declaration of the member, and if one | |||
| 2302 | /// declaration introduces a class name or enumeration name and the | |||
| 2303 | /// other declarations either introduce the same object, the same | |||
| 2304 | /// enumerator or a set of functions, the non-type name hides the | |||
| 2305 | /// class or enumeration name if and only if the declarations are | |||
| 2306 | /// from the same namespace; otherwise (the declarations are from | |||
| 2307 | /// different namespaces), the program is ill-formed. | |||
| 2308 | static bool LookupQualifiedNameInUsingDirectives(Sema &S, LookupResult &R, | |||
| 2309 | DeclContext *StartDC) { | |||
| 2310 | assert(StartDC->isFileContext() && "start context is not a file context")(static_cast <bool> (StartDC->isFileContext() && "start context is not a file context") ? void (0) : __assert_fail ("StartDC->isFileContext() && \"start context is not a file context\"" , "clang/lib/Sema/SemaLookup.cpp", 2310, __extension__ __PRETTY_FUNCTION__ )); | |||
| 2311 | ||||
| 2312 | // We have not yet looked into these namespaces, much less added | |||
| 2313 | // their "using-children" to the queue. | |||
| 2314 | SmallVector<NamespaceDecl*, 8> Queue; | |||
| 2315 | ||||
| 2316 | // We have at least added all these contexts to the queue. | |||
| 2317 | llvm::SmallPtrSet<DeclContext*, 8> Visited; | |||
| 2318 | Visited.insert(StartDC); | |||
| 2319 | ||||
| 2320 | // We have already looked into the initial namespace; seed the queue | |||
| 2321 | // with its using-children. | |||
| 2322 | for (auto *I : StartDC->using_directives()) { | |||
| 2323 | NamespaceDecl *ND = I->getNominatedNamespace()->getOriginalNamespace(); | |||
| 2324 | if (S.isVisible(I) && Visited.insert(ND).second) | |||
| 2325 | Queue.push_back(ND); | |||
| 2326 | } | |||
| 2327 | ||||
| 2328 | // The easiest way to implement the restriction in [namespace.qual]p5 | |||
| 2329 | // is to check whether any of the individual results found a tag | |||
| 2330 | // and, if so, to declare an ambiguity if the final result is not | |||
| 2331 | // a tag. | |||
| 2332 | bool FoundTag = false; | |||
| 2333 | bool FoundNonTag = false; | |||
| 2334 | ||||
| 2335 | LookupResult LocalR(LookupResult::Temporary, R); | |||
| 2336 | ||||
| 2337 | bool Found = false; | |||
| 2338 | while (!Queue.empty()) { | |||
| 2339 | NamespaceDecl *ND = Queue.pop_back_val(); | |||
| 2340 | ||||
| 2341 | // We go through some convolutions here to avoid copying results | |||
| 2342 | // between LookupResults. | |||
| 2343 | bool UseLocal = !R.empty(); | |||
| 2344 | LookupResult &DirectR = UseLocal ? LocalR : R; | |||
| 2345 | bool FoundDirect = LookupDirect(S, DirectR, ND); | |||
| 2346 | ||||
| 2347 | if (FoundDirect) { | |||
| 2348 | // First do any local hiding. | |||
| 2349 | DirectR.resolveKind(); | |||
| 2350 | ||||
| 2351 | // If the local result is a tag, remember that. | |||
| 2352 | if (DirectR.isSingleTagDecl()) | |||
| 2353 | FoundTag = true; | |||
| 2354 | else | |||
| 2355 | FoundNonTag = true; | |||
| 2356 | ||||
| 2357 | // Append the local results to the total results if necessary. | |||
| 2358 | if (UseLocal) { | |||
| 2359 | R.addAllDecls(LocalR); | |||
| 2360 | LocalR.clear(); | |||
| 2361 | } | |||
| 2362 | } | |||
| 2363 | ||||
| 2364 | // If we find names in this namespace, ignore its using directives. | |||
| 2365 | if (FoundDirect) { | |||
| 2366 | Found = true; | |||
| 2367 | continue; | |||
| 2368 | } | |||
| 2369 | ||||
| 2370 | for (auto *I : ND->using_directives()) { | |||
| 2371 | NamespaceDecl *Nom = I->getNominatedNamespace(); | |||
| 2372 | if (S.isVisible(I) && Visited.insert(Nom).second) | |||
| 2373 | Queue.push_back(Nom); | |||
| 2374 | } | |||
| 2375 | } | |||
| 2376 | ||||
| 2377 | if (Found) { | |||
| 2378 | if (FoundTag && FoundNonTag) | |||
| 2379 | R.setAmbiguousQualifiedTagHiding(); | |||
| 2380 | else | |||
| 2381 | R.resolveKind(); | |||
| 2382 | } | |||
| 2383 | ||||
| 2384 | return Found; | |||
| 2385 | } | |||
| 2386 | ||||
| 2387 | /// Perform qualified name lookup into a given context. | |||
| 2388 | /// | |||
| 2389 | /// Qualified name lookup (C++ [basic.lookup.qual]) is used to find | |||
| 2390 | /// names when the context of those names is explicit specified, e.g., | |||
| 2391 | /// "std::vector" or "x->member", or as part of unqualified name lookup. | |||
| 2392 | /// | |||
| 2393 | /// Different lookup criteria can find different names. For example, a | |||
| 2394 | /// particular scope can have both a struct and a function of the same | |||
| 2395 | /// name, and each can be found by certain lookup criteria. For more | |||
| 2396 | /// information about lookup criteria, see the documentation for the | |||
| 2397 | /// class LookupCriteria. | |||
| 2398 | /// | |||
| 2399 | /// \param R captures both the lookup criteria and any lookup results found. | |||
| 2400 | /// | |||
| 2401 | /// \param LookupCtx The context in which qualified name lookup will | |||
| 2402 | /// search. If the lookup criteria permits, name lookup may also search | |||
| 2403 | /// in the parent contexts or (for C++ classes) base classes. | |||
| 2404 | /// | |||
| 2405 | /// \param InUnqualifiedLookup true if this is qualified name lookup that | |||
| 2406 | /// occurs as part of unqualified name lookup. | |||
| 2407 | /// | |||
| 2408 | /// \returns true if lookup succeeded, false if it failed. | |||
| 2409 | bool Sema::LookupQualifiedName(LookupResult &R, DeclContext *LookupCtx, | |||
| 2410 | bool InUnqualifiedLookup) { | |||
| 2411 | assert(LookupCtx && "Sema::LookupQualifiedName requires a lookup context")(static_cast <bool> (LookupCtx && "Sema::LookupQualifiedName requires a lookup context" ) ? void (0) : __assert_fail ("LookupCtx && \"Sema::LookupQualifiedName requires a lookup context\"" , "clang/lib/Sema/SemaLookup.cpp", 2411, __extension__ __PRETTY_FUNCTION__ )); | |||
| 2412 | ||||
| 2413 | if (!R.getLookupName()) | |||
| 2414 | return false; | |||
| 2415 | ||||
| 2416 | // Make sure that the declaration context is complete. | |||
| 2417 | assert((!isa<TagDecl>(LookupCtx) ||(static_cast <bool> ((!isa<TagDecl>(LookupCtx) || LookupCtx->isDependentContext() || cast<TagDecl>(LookupCtx )->isCompleteDefinition() || cast<TagDecl>(LookupCtx )->isBeingDefined()) && "Declaration context must already be complete!" ) ? void (0) : __assert_fail ("(!isa<TagDecl>(LookupCtx) || LookupCtx->isDependentContext() || cast<TagDecl>(LookupCtx)->isCompleteDefinition() || cast<TagDecl>(LookupCtx)->isBeingDefined()) && \"Declaration context must already be complete!\"" , "clang/lib/Sema/SemaLookup.cpp", 2421, __extension__ __PRETTY_FUNCTION__ )) | |||
| 2418 | LookupCtx->isDependentContext() ||(static_cast <bool> ((!isa<TagDecl>(LookupCtx) || LookupCtx->isDependentContext() || cast<TagDecl>(LookupCtx )->isCompleteDefinition() || cast<TagDecl>(LookupCtx )->isBeingDefined()) && "Declaration context must already be complete!" ) ? void (0) : __assert_fail ("(!isa<TagDecl>(LookupCtx) || LookupCtx->isDependentContext() || cast<TagDecl>(LookupCtx)->isCompleteDefinition() || cast<TagDecl>(LookupCtx)->isBeingDefined()) && \"Declaration context must already be complete!\"" , "clang/lib/Sema/SemaLookup.cpp", 2421, __extension__ __PRETTY_FUNCTION__ )) | |||
| 2419 | cast<TagDecl>(LookupCtx)->isCompleteDefinition() ||(static_cast <bool> ((!isa<TagDecl>(LookupCtx) || LookupCtx->isDependentContext() || cast<TagDecl>(LookupCtx )->isCompleteDefinition() || cast<TagDecl>(LookupCtx )->isBeingDefined()) && "Declaration context must already be complete!" ) ? void (0) : __assert_fail ("(!isa<TagDecl>(LookupCtx) || LookupCtx->isDependentContext() || cast<TagDecl>(LookupCtx)->isCompleteDefinition() || cast<TagDecl>(LookupCtx)->isBeingDefined()) && \"Declaration context must already be complete!\"" , "clang/lib/Sema/SemaLookup.cpp", 2421, __extension__ __PRETTY_FUNCTION__ )) | |||
| 2420 | cast<TagDecl>(LookupCtx)->isBeingDefined()) &&(static_cast <bool> ((!isa<TagDecl>(LookupCtx) || LookupCtx->isDependentContext() || cast<TagDecl>(LookupCtx )->isCompleteDefinition() || cast<TagDecl>(LookupCtx )->isBeingDefined()) && "Declaration context must already be complete!" ) ? void (0) : __assert_fail ("(!isa<TagDecl>(LookupCtx) || LookupCtx->isDependentContext() || cast<TagDecl>(LookupCtx)->isCompleteDefinition() || cast<TagDecl>(LookupCtx)->isBeingDefined()) && \"Declaration context must already be complete!\"" , "clang/lib/Sema/SemaLookup.cpp", 2421, __extension__ __PRETTY_FUNCTION__ )) | |||
| 2421 | "Declaration context must already be complete!")(static_cast <bool> ((!isa<TagDecl>(LookupCtx) || LookupCtx->isDependentContext() || cast<TagDecl>(LookupCtx )->isCompleteDefinition() || cast<TagDecl>(LookupCtx )->isBeingDefined()) && "Declaration context must already be complete!" ) ? void (0) : __assert_fail ("(!isa<TagDecl>(LookupCtx) || LookupCtx->isDependentContext() || cast<TagDecl>(LookupCtx)->isCompleteDefinition() || cast<TagDecl>(LookupCtx)->isBeingDefined()) && \"Declaration context must already be complete!\"" , "clang/lib/Sema/SemaLookup.cpp", 2421, __extension__ __PRETTY_FUNCTION__ )); | |||
| 2422 | ||||
| 2423 | struct QualifiedLookupInScope { | |||
| 2424 | bool oldVal; | |||
| 2425 | DeclContext *Context; | |||
| 2426 | // Set flag in DeclContext informing debugger that we're looking for qualified name | |||
| 2427 | QualifiedLookupInScope(DeclContext *ctx) : Context(ctx) { | |||
| 2428 | oldVal = ctx->setUseQualifiedLookup(); | |||
| 2429 | } | |||
| 2430 | ~QualifiedLookupInScope() { | |||
| 2431 | Context->setUseQualifiedLookup(oldVal); | |||
| 2432 | } | |||
| 2433 | } QL(LookupCtx); | |||
| 2434 | ||||
| 2435 | if (LookupDirect(*this, R, LookupCtx)) { | |||
| 2436 | R.resolveKind(); | |||
| 2437 | if (isa<CXXRecordDecl>(LookupCtx)) | |||
| 2438 | R.setNamingClass(cast<CXXRecordDecl>(LookupCtx)); | |||
| 2439 | return true; | |||
| 2440 | } | |||
| 2441 | ||||
| 2442 | // Don't descend into implied contexts for redeclarations. | |||
| 2443 | // C++98 [namespace.qual]p6: | |||
| 2444 | // In a declaration for a namespace member in which the | |||
| 2445 | // declarator-id is a qualified-id, given that the qualified-id | |||
| 2446 | // for the namespace member has the form | |||
| 2447 | // nested-name-specifier unqualified-id | |||
| 2448 | // the unqualified-id shall name a member of the namespace | |||
| 2449 | // designated by the nested-name-specifier. | |||
| 2450 | // See also [class.mfct]p5 and [class.static.data]p2. | |||
| 2451 | if (R.isForRedeclaration()) | |||
| 2452 | return false; | |||
| 2453 | ||||
| 2454 | // If this is a namespace, look it up in the implied namespaces. | |||
| 2455 | if (LookupCtx->isFileContext()) | |||
| 2456 | return LookupQualifiedNameInUsingDirectives(*this, R, LookupCtx); | |||
| 2457 | ||||
| 2458 | // If this isn't a C++ class, we aren't allowed to look into base | |||
| 2459 | // classes, we're done. | |||
| 2460 | CXXRecordDecl *LookupRec = dyn_cast<CXXRecordDecl>(LookupCtx); | |||
| 2461 | if (!LookupRec || !LookupRec->getDefinition()) | |||
| 2462 | return false; | |||
| 2463 | ||||
| 2464 | // We're done for lookups that can never succeed for C++ classes. | |||
| 2465 | if (R.getLookupKind() == LookupOperatorName || | |||
| 2466 | R.getLookupKind() == LookupNamespaceName || | |||
| 2467 | R.getLookupKind() == LookupObjCProtocolName || | |||
| 2468 | R.getLookupKind() == LookupLabel) | |||
| 2469 | return false; | |||
| 2470 | ||||
| 2471 | // If we're performing qualified name lookup into a dependent class, | |||
| 2472 | // then we are actually looking into a current instantiation. If we have any | |||
| 2473 | // dependent base classes, then we either have to delay lookup until | |||
| 2474 | // template instantiation time (at which point all bases will be available) | |||
| 2475 | // or we have to fail. | |||
| 2476 | if (!InUnqualifiedLookup && LookupRec->isDependentContext() && | |||
| 2477 | LookupRec->hasAnyDependentBases()) { | |||
| 2478 | R.setNotFoundInCurrentInstantiation(); | |||
| 2479 | return false; | |||
| 2480 | } | |||
| 2481 | ||||
| 2482 | // Perform lookup into our base classes. | |||
| 2483 | ||||
| 2484 | DeclarationName Name = R.getLookupName(); | |||
| 2485 | unsigned IDNS = R.getIdentifierNamespace(); | |||
| 2486 | ||||
| 2487 | // Look for this member in our base classes. | |||
| 2488 | auto BaseCallback = [Name, IDNS](const CXXBaseSpecifier *Specifier, | |||
| 2489 | CXXBasePath &Path) -> bool { | |||
| 2490 | CXXRecordDecl *BaseRecord = Specifier->getType()->getAsCXXRecordDecl(); | |||
| 2491 | // Drop leading non-matching lookup results from the declaration list so | |||
| 2492 | // we don't need to consider them again below. | |||
| 2493 | for (Path.Decls = BaseRecord->lookup(Name).begin(); | |||
| 2494 | Path.Decls != Path.Decls.end(); ++Path.Decls) { | |||
| 2495 | if ((*Path.Decls)->isInIdentifierNamespace(IDNS)) | |||
| 2496 | return true; | |||
| 2497 | } | |||
| 2498 | return false; | |||
| 2499 | }; | |||
| 2500 | ||||
| 2501 | CXXBasePaths Paths; | |||
| 2502 | Paths.setOrigin(LookupRec); | |||
| 2503 | if (!LookupRec->lookupInBases(BaseCallback, Paths)) | |||
| 2504 | return false; | |||
| 2505 | ||||
| 2506 | R.setNamingClass(LookupRec); | |||
| 2507 | ||||
| 2508 | // C++ [class.member.lookup]p2: | |||
| 2509 | // [...] If the resulting set of declarations are not all from | |||
| 2510 | // sub-objects of the same type, or the set has a nonstatic member | |||
| 2511 | // and includes members from distinct sub-objects, there is an | |||
| 2512 | // ambiguity and the program is ill-formed. Otherwise that set is | |||
| 2513 | // the result of the lookup. | |||
| 2514 | QualType SubobjectType; | |||
| 2515 | int SubobjectNumber = 0; | |||
| 2516 | AccessSpecifier SubobjectAccess = AS_none; | |||
| 2517 | ||||
| 2518 | // Check whether the given lookup result contains only static members. | |||
| 2519 | auto HasOnlyStaticMembers = [&](DeclContext::lookup_iterator Result) { | |||
| 2520 | for (DeclContext::lookup_iterator I = Result, E = I.end(); I != E; ++I) | |||
| 2521 | if ((*I)->isInIdentifierNamespace(IDNS) && (*I)->isCXXInstanceMember()) | |||
| 2522 | return false; | |||
| 2523 | return true; | |||
| 2524 | }; | |||
| 2525 | ||||
| 2526 | bool TemplateNameLookup = R.isTemplateNameLookup(); | |||
| 2527 | ||||
| 2528 | // Determine whether two sets of members contain the same members, as | |||
| 2529 | // required by C++ [class.member.lookup]p6. | |||
| 2530 | auto HasSameDeclarations = [&](DeclContext::lookup_iterator A, | |||
| 2531 | DeclContext::lookup_iterator B) { | |||
| 2532 | using Iterator = DeclContextLookupResult::iterator; | |||
| 2533 | using Result = const void *; | |||
| 2534 | ||||
| 2535 | auto Next = [&](Iterator &It, Iterator End) -> Result { | |||
| 2536 | while (It != End) { | |||
| 2537 | NamedDecl *ND = *It++; | |||
| 2538 | if (!ND->isInIdentifierNamespace(IDNS)) | |||
| 2539 | continue; | |||
| 2540 | ||||
| 2541 | // C++ [temp.local]p3: | |||
| 2542 | // A lookup that finds an injected-class-name (10.2) can result in | |||
| 2543 | // an ambiguity in certain cases (for example, if it is found in | |||
| 2544 | // more than one base class). If all of the injected-class-names | |||
| 2545 | // that are found refer to specializations of the same class | |||
| 2546 | // template, and if the name is used as a template-name, the | |||
| 2547 | // reference refers to the class template itself and not a | |||
| 2548 | // specialization thereof, and is not ambiguous. | |||
| 2549 | if (TemplateNameLookup) | |||
| 2550 | if (auto *TD = getAsTemplateNameDecl(ND)) | |||
| 2551 | ND = TD; | |||
| 2552 | ||||
| 2553 | // C++ [class.member.lookup]p3: | |||
| 2554 | // type declarations (including injected-class-names) are replaced by | |||
| 2555 | // the types they designate | |||
| 2556 | if (const TypeDecl *TD = dyn_cast<TypeDecl>(ND->getUnderlyingDecl())) { | |||
| 2557 | QualType T = Context.getTypeDeclType(TD); | |||
| 2558 | return T.getCanonicalType().getAsOpaquePtr(); | |||
| 2559 | } | |||
| 2560 | ||||
| 2561 | return ND->getUnderlyingDecl()->getCanonicalDecl(); | |||
| 2562 | } | |||
| 2563 | return nullptr; | |||
| 2564 | }; | |||
| 2565 | ||||
| 2566 | // We'll often find the declarations are in the same order. Handle this | |||
| 2567 | // case (and the special case of only one declaration) efficiently. | |||
| 2568 | Iterator AIt = A, BIt = B, AEnd, BEnd; | |||
| 2569 | while (true) { | |||
| 2570 | Result AResult = Next(AIt, AEnd); | |||
| 2571 | Result BResult = Next(BIt, BEnd); | |||
| 2572 | if (!AResult && !BResult) | |||
| 2573 | return true; | |||
| 2574 | if (!AResult || !BResult) | |||
| 2575 | return false; | |||
| 2576 | if (AResult != BResult) { | |||
| 2577 | // Found a mismatch; carefully check both lists, accounting for the | |||
| 2578 | // possibility of declarations appearing more than once. | |||
| 2579 | llvm::SmallDenseMap<Result, bool, 32> AResults; | |||
| 2580 | for (; AResult; AResult = Next(AIt, AEnd)) | |||
| 2581 | AResults.insert({AResult, /*FoundInB*/false}); | |||
| 2582 | unsigned Found = 0; | |||
| 2583 | for (; BResult; BResult = Next(BIt, BEnd)) { | |||
| 2584 | auto It = AResults.find(BResult); | |||
| 2585 | if (It == AResults.end()) | |||
| 2586 | return false; | |||
| 2587 | if (!It->second) { | |||
| 2588 | It->second = true; | |||
| 2589 | ++Found; | |||
| 2590 | } | |||
| 2591 | } | |||
| 2592 | return AResults.size() == Found; | |||
| 2593 | } | |||
| 2594 | } | |||
| 2595 | }; | |||
| 2596 | ||||
| 2597 | for (CXXBasePaths::paths_iterator Path = Paths.begin(), PathEnd = Paths.end(); | |||
| 2598 | Path != PathEnd; ++Path) { | |||
| 2599 | const CXXBasePathElement &PathElement = Path->back(); | |||
| 2600 | ||||
| 2601 | // Pick the best (i.e. most permissive i.e. numerically lowest) access | |||
| 2602 | // across all paths. | |||
| 2603 | SubobjectAccess = std::min(SubobjectAccess, Path->Access); | |||
| 2604 | ||||
| 2605 | // Determine whether we're looking at a distinct sub-object or not. | |||
| 2606 | if (SubobjectType.isNull()) { | |||
| 2607 | // This is the first subobject we've looked at. Record its type. | |||
| 2608 | SubobjectType = Context.getCanonicalType(PathElement.Base->getType()); | |||
| 2609 | SubobjectNumber = PathElement.SubobjectNumber; | |||
| 2610 | continue; | |||
| 2611 | } | |||
| 2612 | ||||
| 2613 | if (SubobjectType != | |||
| 2614 | Context.getCanonicalType(PathElement.Base->getType())) { | |||
| 2615 | // We found members of the given name in two subobjects of | |||
| 2616 | // different types. If the declaration sets aren't the same, this | |||
| 2617 | // lookup is ambiguous. | |||
| 2618 | // | |||
| 2619 | // FIXME: The language rule says that this applies irrespective of | |||
| 2620 | // whether the sets contain only static members. | |||
| 2621 | if (HasOnlyStaticMembers(Path->Decls) && | |||
| 2622 | HasSameDeclarations(Paths.begin()->Decls, Path->Decls)) | |||
| 2623 | continue; | |||
| 2624 | ||||
| 2625 | R.setAmbiguousBaseSubobjectTypes(Paths); | |||
| 2626 | return true; | |||
| 2627 | } | |||
| 2628 | ||||
| 2629 | // FIXME: This language rule no longer exists. Checking for ambiguous base | |||
| 2630 | // subobjects should be done as part of formation of a class member access | |||
| 2631 | // expression (when converting the object parameter to the member's type). | |||
| 2632 | if (SubobjectNumber != PathElement.SubobjectNumber) { | |||
| 2633 | // We have a different subobject of the same type. | |||
| 2634 | ||||
| 2635 | // C++ [class.member.lookup]p5: | |||
| 2636 | // A static member, a nested type or an enumerator defined in | |||
| 2637 | // a base class T can unambiguously be found even if an object | |||
| 2638 | // has more than one base class subobject of type T. | |||
| 2639 | if (HasOnlyStaticMembers(Path->Decls)) | |||
| 2640 | continue; | |||
| 2641 | ||||
| 2642 | // We have found a nonstatic member name in multiple, distinct | |||
| 2643 | // subobjects. Name lookup is ambiguous. | |||
| 2644 | R.setAmbiguousBaseSubobjects(Paths); | |||
| 2645 | return true; | |||
| 2646 | } | |||
| 2647 | } | |||
| 2648 | ||||
| 2649 | // Lookup in a base class succeeded; return these results. | |||
| 2650 | ||||
| 2651 | for (DeclContext::lookup_iterator I = Paths.front().Decls, E = I.end(); | |||
| 2652 | I != E; ++I) { | |||
| 2653 | AccessSpecifier AS = CXXRecordDecl::MergeAccess(SubobjectAccess, | |||
| 2654 | (*I)->getAccess()); | |||
| 2655 | if (NamedDecl *ND = R.getAcceptableDecl(*I)) | |||
| 2656 | R.addDecl(ND, AS); | |||
| 2657 | } | |||
| 2658 | R.resolveKind(); | |||
| 2659 | return true; | |||
| 2660 | } | |||
| 2661 | ||||
| 2662 | /// Performs qualified name lookup or special type of lookup for | |||
| 2663 | /// "__super::" scope specifier. | |||
| 2664 | /// | |||
| 2665 | /// This routine is a convenience overload meant to be called from contexts | |||
| 2666 | /// that need to perform a qualified name lookup with an optional C++ scope | |||
| 2667 | /// specifier that might require special kind of lookup. | |||
| 2668 | /// | |||
| 2669 | /// \param R captures both the lookup criteria and any lookup results found. | |||
| 2670 | /// | |||
| 2671 | /// \param LookupCtx The context in which qualified name lookup will | |||
| 2672 | /// search. | |||
| 2673 | /// | |||
| 2674 | /// \param SS An optional C++ scope-specifier. | |||
| 2675 | /// | |||
| 2676 | /// \returns true if lookup succeeded, false if it failed. | |||
| 2677 | bool Sema::LookupQualifiedName(LookupResult &R, DeclContext *LookupCtx, | |||
| 2678 | CXXScopeSpec &SS) { | |||
| 2679 | auto *NNS = SS.getScopeRep(); | |||
| 2680 | if (NNS && NNS->getKind() == NestedNameSpecifier::Super) | |||
| 2681 | return LookupInSuper(R, NNS->getAsRecordDecl()); | |||
| 2682 | else | |||
| 2683 | ||||
| 2684 | return LookupQualifiedName(R, LookupCtx); | |||
| 2685 | } | |||
| 2686 | ||||
| 2687 | /// Performs name lookup for a name that was parsed in the | |||
| 2688 | /// source code, and may contain a C++ scope specifier. | |||
| 2689 | /// | |||
| 2690 | /// This routine is a convenience routine meant to be called from | |||
| 2691 | /// contexts that receive a name and an optional C++ scope specifier | |||
| 2692 | /// (e.g., "N::M::x"). It will then perform either qualified or | |||
| 2693 | /// unqualified name lookup (with LookupQualifiedName or LookupName, | |||
| 2694 | /// respectively) on the given name and return those results. It will | |||
| 2695 | /// perform a special type of lookup for "__super::" scope specifier. | |||
| 2696 | /// | |||
| 2697 | /// @param S The scope from which unqualified name lookup will | |||
| 2698 | /// begin. | |||
| 2699 | /// | |||
| 2700 | /// @param SS An optional C++ scope-specifier, e.g., "::N::M". | |||
| 2701 | /// | |||
| 2702 | /// @param EnteringContext Indicates whether we are going to enter the | |||
| 2703 | /// context of the scope-specifier SS (if present). | |||
| 2704 | /// | |||
| 2705 | /// @returns True if any decls were found (but possibly ambiguous) | |||
| 2706 | bool Sema::LookupParsedName(LookupResult &R, Scope *S, CXXScopeSpec *SS, | |||
| 2707 | bool AllowBuiltinCreation, bool EnteringContext) { | |||
| 2708 | if (SS && SS->isInvalid()) { | |||
| 2709 | // When the scope specifier is invalid, don't even look for | |||
| 2710 | // anything. | |||
| 2711 | return false; | |||
| 2712 | } | |||
| 2713 | ||||
| 2714 | if (SS && SS->isSet()) { | |||
| 2715 | NestedNameSpecifier *NNS = SS->getScopeRep(); | |||
| 2716 | if (NNS->getKind() == NestedNameSpecifier::Super) | |||
| 2717 | return LookupInSuper(R, NNS->getAsRecordDecl()); | |||
| 2718 | ||||
| 2719 | if (DeclContext *DC = computeDeclContext(*SS, EnteringContext)) { | |||
| 2720 | // We have resolved the scope specifier to a particular declaration | |||
| 2721 | // contex, and will perform name lookup in that context. | |||
| 2722 | if (!DC->isDependentContext() && RequireCompleteDeclContext(*SS, DC)) | |||
| 2723 | return false; | |||
| 2724 | ||||
| 2725 | R.setContextRange(SS->getRange()); | |||
| 2726 | return LookupQualifiedName(R, DC); | |||
| 2727 | } | |||
| 2728 | ||||
| 2729 | // We could not resolve the scope specified to a specific declaration | |||
| 2730 | // context, which means that SS refers to an unknown specialization. | |||
| 2731 | // Name lookup can't find anything in this case. | |||
| 2732 | R.setNotFoundInCurrentInstantiation(); | |||
| 2733 | R.setContextRange(SS->getRange()); | |||
| 2734 | return false; | |||
| 2735 | } | |||
| 2736 | ||||
| 2737 | // Perform unqualified name lookup starting in the given scope. | |||
| 2738 | return LookupName(R, S, AllowBuiltinCreation); | |||
| 2739 | } | |||
| 2740 | ||||
| 2741 | /// Perform qualified name lookup into all base classes of the given | |||
| 2742 | /// class. | |||
| 2743 | /// | |||
| 2744 | /// \param R captures both the lookup criteria and any lookup results found. | |||
| 2745 | /// | |||
| 2746 | /// \param Class The context in which qualified name lookup will | |||
| 2747 | /// search. Name lookup will search in all base classes merging the results. | |||
| 2748 | /// | |||
| 2749 | /// @returns True if any decls were found (but possibly ambiguous) | |||
| 2750 | bool Sema::LookupInSuper(LookupResult &R, CXXRecordDecl *Class) { | |||
| 2751 | // The access-control rules we use here are essentially the rules for | |||
| 2752 | // doing a lookup in Class that just magically skipped the direct | |||
| 2753 | // members of Class itself. That is, the naming class is Class, and the | |||
| 2754 | // access includes the access of the base. | |||
| 2755 | for (const auto &BaseSpec : Class->bases()) { | |||
| 2756 | CXXRecordDecl *RD = cast<CXXRecordDecl>( | |||
| 2757 | BaseSpec.getType()->castAs<RecordType>()->getDecl()); | |||
| 2758 | LookupResult Result(*this, R.getLookupNameInfo(), R.getLookupKind()); | |||
| 2759 | Result.setBaseObjectType(Context.getRecordType(Class)); | |||
| 2760 | LookupQualifiedName(Result, RD); | |||
| 2761 | ||||
| 2762 | // Copy the lookup results into the target, merging the base's access into | |||
| 2763 | // the path access. | |||
| 2764 | for (auto I = Result.begin(), E = Result.end(); I != E; ++I) { | |||
| 2765 | R.addDecl(I.getDecl(), | |||
| 2766 | CXXRecordDecl::MergeAccess(BaseSpec.getAccessSpecifier(), | |||
| 2767 | I.getAccess())); | |||
| 2768 | } | |||
| 2769 | ||||
| 2770 | Result.suppressDiagnostics(); | |||
| 2771 | } | |||
| 2772 | ||||
| 2773 | R.resolveKind(); | |||
| 2774 | R.setNamingClass(Class); | |||
| 2775 | ||||
| 2776 | return !R.empty(); | |||
| 2777 | } | |||
| 2778 | ||||
| 2779 | /// Produce a diagnostic describing the ambiguity that resulted | |||
| 2780 | /// from name lookup. | |||
| 2781 | /// | |||
| 2782 | /// \param Result The result of the ambiguous lookup to be diagnosed. | |||
| 2783 | void Sema::DiagnoseAmbiguousLookup(LookupResult &Result) { | |||
| 2784 | assert(Result.isAmbiguous() && "Lookup result must be ambiguous")(static_cast <bool> (Result.isAmbiguous() && "Lookup result must be ambiguous" ) ? void (0) : __assert_fail ("Result.isAmbiguous() && \"Lookup result must be ambiguous\"" , "clang/lib/Sema/SemaLookup.cpp", 2784, __extension__ __PRETTY_FUNCTION__ )); | |||
| 2785 | ||||
| 2786 | DeclarationName Name = Result.getLookupName(); | |||
| 2787 | SourceLocation NameLoc = Result.getNameLoc(); | |||
| 2788 | SourceRange LookupRange = Result.getContextRange(); | |||
| 2789 | ||||
| 2790 | switch (Result.getAmbiguityKind()) { | |||
| 2791 | case LookupResult::AmbiguousBaseSubobjects: { | |||
| 2792 | CXXBasePaths *Paths = Result.getBasePaths(); | |||
| 2793 | QualType SubobjectType = Paths->front().back().Base->getType(); | |||
| 2794 | Diag(NameLoc, diag::err_ambiguous_member_multiple_subobjects) | |||
| 2795 | << Name << SubobjectType << getAmbiguousPathsDisplayString(*Paths) | |||
| 2796 | << LookupRange; | |||
| 2797 | ||||
| 2798 | DeclContext::lookup_iterator Found = Paths->front().Decls; | |||
| 2799 | while (isa<CXXMethodDecl>(*Found) && | |||
| 2800 | cast<CXXMethodDecl>(*Found)->isStatic()) | |||
| 2801 | ++Found; | |||
| 2802 | ||||
| 2803 | Diag((*Found)->getLocation(), diag::note_ambiguous_member_found); | |||
| 2804 | break; | |||
| 2805 | } | |||
| 2806 | ||||
| 2807 | case LookupResult::AmbiguousBaseSubobjectTypes: { | |||
| 2808 | Diag(NameLoc, diag::err_ambiguous_member_multiple_subobject_types) | |||
| 2809 | << Name << LookupRange; | |||
| 2810 | ||||
| 2811 | CXXBasePaths *Paths = Result.getBasePaths(); | |||
| 2812 | std::set<const NamedDecl *> DeclsPrinted; | |||
| 2813 | for (CXXBasePaths::paths_iterator Path = Paths->begin(), | |||
| 2814 | PathEnd = Paths->end(); | |||
| 2815 | Path != PathEnd; ++Path) { | |||
| 2816 | const NamedDecl *D = *Path->Decls; | |||
| 2817 | if (!D->isInIdentifierNamespace(Result.getIdentifierNamespace())) | |||
| 2818 | continue; | |||
| 2819 | if (DeclsPrinted.insert(D).second) { | |||
| 2820 | if (const auto *TD = dyn_cast<TypedefNameDecl>(D->getUnderlyingDecl())) | |||
| 2821 | Diag(D->getLocation(), diag::note_ambiguous_member_type_found) | |||
| 2822 | << TD->getUnderlyingType(); | |||
| 2823 | else if (const auto *TD = dyn_cast<TypeDecl>(D->getUnderlyingDecl())) | |||
| 2824 | Diag(D->getLocation(), diag::note_ambiguous_member_type_found) | |||
| 2825 | << Context.getTypeDeclType(TD); | |||
| 2826 | else | |||
| 2827 | Diag(D->getLocation(), diag::note_ambiguous_member_found); | |||
| 2828 | } | |||
| 2829 | } | |||
| 2830 | break; | |||
| 2831 | } | |||
| 2832 | ||||
| 2833 | case LookupResult::AmbiguousTagHiding: { | |||
| 2834 | Diag(NameLoc, diag::err_ambiguous_tag_hiding) << Name << LookupRange; | |||
| 2835 | ||||
| 2836 | llvm::SmallPtrSet<NamedDecl*, 8> TagDecls; | |||
| 2837 | ||||
| 2838 | for (auto *D : Result) | |||
| 2839 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) { | |||
| 2840 | TagDecls.insert(TD); | |||
| 2841 | Diag(TD->getLocation(), diag::note_hidden_tag); | |||
| 2842 | } | |||
| 2843 | ||||
| 2844 | for (auto *D : Result) | |||
| 2845 | if (!isa<TagDecl>(D)) | |||
| 2846 | Diag(D->getLocation(), diag::note_hiding_object); | |||
| 2847 | ||||
| 2848 | // For recovery purposes, go ahead and implement the hiding. | |||
| 2849 | LookupResult::Filter F = Result.makeFilter(); | |||
| 2850 | while (F.hasNext()) { | |||
| 2851 | if (TagDecls.count(F.next())) | |||
| 2852 | F.erase(); | |||
| 2853 | } | |||
| 2854 | F.done(); | |||
| 2855 | break; | |||
| 2856 | } | |||
| 2857 | ||||
| 2858 | case LookupResult::AmbiguousReference: { | |||
| 2859 | Diag(NameLoc, diag::err_ambiguous_reference) << Name << LookupRange; | |||
| 2860 | ||||
| 2861 | for (auto *D : Result) | |||
| 2862 | Diag(D->getLocation(), diag::note_ambiguous_candidate) << D; | |||
| 2863 | break; | |||
| 2864 | } | |||
| 2865 | } | |||
| 2866 | } | |||
| 2867 | ||||
| 2868 | namespace { | |||
| 2869 | struct AssociatedLookup { | |||
| 2870 | AssociatedLookup(Sema &S, SourceLocation InstantiationLoc, | |||
| 2871 | Sema::AssociatedNamespaceSet &Namespaces, | |||
| 2872 | Sema::AssociatedClassSet &Classes) | |||
| 2873 | : S(S), Namespaces(Namespaces), Classes(Classes), | |||
| 2874 | InstantiationLoc(InstantiationLoc) { | |||
| 2875 | } | |||
| 2876 | ||||
| 2877 | bool addClassTransitive(CXXRecordDecl *RD) { | |||
| 2878 | Classes.insert(RD); | |||
| 2879 | return ClassesTransitive.insert(RD); | |||
| 2880 | } | |||
| 2881 | ||||
| 2882 | Sema &S; | |||
| 2883 | Sema::AssociatedNamespaceSet &Namespaces; | |||
| 2884 | Sema::AssociatedClassSet &Classes; | |||
| 2885 | SourceLocation InstantiationLoc; | |||
| 2886 | ||||
| 2887 | private: | |||
| 2888 | Sema::AssociatedClassSet ClassesTransitive; | |||
| 2889 | }; | |||
| 2890 | } // end anonymous namespace | |||
| 2891 | ||||
| 2892 | static void | |||
| 2893 | addAssociatedClassesAndNamespaces(AssociatedLookup &Result, QualType T); | |||
| 2894 | ||||
| 2895 | // Given the declaration context \param Ctx of a class, class template or | |||
| 2896 | // enumeration, add the associated namespaces to \param Namespaces as described | |||
| 2897 | // in [basic.lookup.argdep]p2. | |||
| 2898 | static void CollectEnclosingNamespace(Sema::AssociatedNamespaceSet &Namespaces, | |||
| 2899 | DeclContext *Ctx) { | |||
| 2900 | // The exact wording has been changed in C++14 as a result of | |||
| 2901 | // CWG 1691 (see also CWG 1690 and CWG 1692). We apply it unconditionally | |||
| 2902 | // to all language versions since it is possible to return a local type | |||
| 2903 | // from a lambda in C++11. | |||
| 2904 | // | |||
| 2905 | // C++14 [basic.lookup.argdep]p2: | |||
| 2906 | // If T is a class type [...]. Its associated namespaces are the innermost | |||
| 2907 | // enclosing namespaces of its associated classes. [...] | |||
| 2908 | // | |||
| 2909 | // If T is an enumeration type, its associated namespace is the innermost | |||
| 2910 | // enclosing namespace of its declaration. [...] | |||
| 2911 | ||||
| 2912 | // We additionally skip inline namespaces. The innermost non-inline namespace | |||
| 2913 | // contains all names of all its nested inline namespaces anyway, so we can | |||
| 2914 | // replace the entire inline namespace tree with its root. | |||
| 2915 | while (!Ctx->isFileContext() || Ctx->isInlineNamespace()) | |||
| 2916 | Ctx = Ctx->getParent(); | |||
| 2917 | ||||
| 2918 | Namespaces.insert(Ctx->getPrimaryContext()); | |||
| 2919 | } | |||
| 2920 | ||||
| 2921 | // Add the associated classes and namespaces for argument-dependent | |||
| 2922 | // lookup that involves a template argument (C++ [basic.lookup.argdep]p2). | |||
| 2923 | static void | |||
| 2924 | addAssociatedClassesAndNamespaces(AssociatedLookup &Result, | |||
| 2925 | const TemplateArgument &Arg) { | |||
| 2926 | // C++ [basic.lookup.argdep]p2, last bullet: | |||
| 2927 | // -- [...] ; | |||
| 2928 | switch (Arg.getKind()) { | |||
| 2929 | case TemplateArgument::Null: | |||
| 2930 | break; | |||
| 2931 | ||||
| 2932 | case TemplateArgument::Type: | |||
| 2933 | // [...] the namespaces and classes associated with the types of the | |||
| 2934 | // template arguments provided for template type parameters (excluding | |||
| 2935 | // template template parameters) | |||
| 2936 | addAssociatedClassesAndNamespaces(Result, Arg.getAsType()); | |||
| 2937 | break; | |||
| 2938 | ||||
| 2939 | case TemplateArgument::Template: | |||
| 2940 | case TemplateArgument::TemplateExpansion: { | |||
| 2941 | // [...] the namespaces in which any template template arguments are | |||
| 2942 | // defined; and the classes in which any member templates used as | |||
| 2943 | // template template arguments are defined. | |||
| 2944 | TemplateName Template = Arg.getAsTemplateOrTemplatePattern(); | |||
| 2945 | if (ClassTemplateDecl *ClassTemplate | |||
| 2946 | = dyn_cast<ClassTemplateDecl>(Template.getAsTemplateDecl())) { | |||
| 2947 | DeclContext *Ctx = ClassTemplate->getDeclContext(); | |||
| 2948 | if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) | |||
| 2949 | Result.Classes.insert(EnclosingClass); | |||
| 2950 | // Add the associated namespace for this class. | |||
| 2951 | CollectEnclosingNamespace(Result.Namespaces, Ctx); | |||
| 2952 | } | |||
| 2953 | break; | |||
| 2954 | } | |||
| 2955 | ||||
| 2956 | case TemplateArgument::Declaration: | |||
| 2957 | case TemplateArgument::Integral: | |||
| 2958 | case TemplateArgument::Expression: | |||
| 2959 | case TemplateArgument::NullPtr: | |||
| 2960 | // [Note: non-type template arguments do not contribute to the set of | |||
| 2961 | // associated namespaces. ] | |||
| 2962 | break; | |||
| 2963 | ||||
| 2964 | case TemplateArgument::Pack: | |||
| 2965 | for (const auto &P : Arg.pack_elements()) | |||
| 2966 | addAssociatedClassesAndNamespaces(Result, P); | |||
| 2967 | break; | |||
| 2968 | } | |||
| 2969 | } | |||
| 2970 | ||||
| 2971 | // Add the associated classes and namespaces for argument-dependent lookup | |||
| 2972 | // with an argument of class type (C++ [basic.lookup.argdep]p2). | |||
| 2973 | static void | |||
| 2974 | addAssociatedClassesAndNamespaces(AssociatedLookup &Result, | |||
| 2975 | CXXRecordDecl *Class) { | |||
| 2976 | ||||
| 2977 | // Just silently ignore anything whose name is __va_list_tag. | |||
| 2978 | if (Class->getDeclName() == Result.S.VAListTagName) | |||
| 2979 | return; | |||
| 2980 | ||||
| 2981 | // C++ [basic.lookup.argdep]p2: | |||
| 2982 | // [...] | |||
| 2983 | // -- If T is a class type (including unions), its associated | |||
| 2984 | // classes are: the class itself; the class of which it is a | |||
| 2985 | // member, if any; and its direct and indirect base classes. | |||
| 2986 | // Its associated namespaces are the innermost enclosing | |||
| 2987 | // namespaces of its associated classes. | |||
| 2988 | ||||
| 2989 | // Add the class of which it is a member, if any. | |||
| 2990 | DeclContext *Ctx = Class->getDeclContext(); | |||
| 2991 | if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) | |||
| 2992 | Result.Classes.insert(EnclosingClass); | |||
| 2993 | ||||
| 2994 | // Add the associated namespace for this class. | |||
| 2995 | CollectEnclosingNamespace(Result.Namespaces, Ctx); | |||
| 2996 | ||||
| 2997 | // -- If T is a template-id, its associated namespaces and classes are | |||
| 2998 | // the namespace in which the template is defined; for member | |||
| 2999 | // templates, the member template's class; the namespaces and classes | |||
| 3000 | // associated with the types of the template arguments provided for | |||
| 3001 | // template type parameters (excluding template template parameters); the | |||
| 3002 | // namespaces in which any template template arguments are defined; and | |||
| 3003 | // the classes in which any member templates used as template template | |||
| 3004 | // arguments are defined. [Note: non-type template arguments do not | |||
| 3005 | // contribute to the set of associated namespaces. ] | |||
| 3006 | if (ClassTemplateSpecializationDecl *Spec | |||
| 3007 | = dyn_cast<ClassTemplateSpecializationDecl>(Class)) { | |||
| 3008 | DeclContext *Ctx = Spec->getSpecializedTemplate()->getDeclContext(); | |||
| 3009 | if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) | |||
| 3010 | Result.Classes.insert(EnclosingClass); | |||
| 3011 | // Add the associated namespace for this class. | |||
| 3012 | CollectEnclosingNamespace(Result.Namespaces, Ctx); | |||
| 3013 | ||||
| 3014 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); | |||
| 3015 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) | |||
| 3016 | addAssociatedClassesAndNamespaces(Result, TemplateArgs[I]); | |||
| 3017 | } | |||
| 3018 | ||||
| 3019 | // Add the class itself. If we've already transitively visited this class, | |||
| 3020 | // we don't need to visit base classes. | |||
| 3021 | if (!Result.addClassTransitive(Class)) | |||
| 3022 | return; | |||
| 3023 | ||||
| 3024 | // Only recurse into base classes for complete types. | |||
| 3025 | if (!Result.S.isCompleteType(Result.InstantiationLoc, | |||
| 3026 | Result.S.Context.getRecordType(Class))) | |||
| 3027 | return; | |||
| 3028 | ||||
| 3029 | // Add direct and indirect base classes along with their associated | |||
| 3030 | // namespaces. | |||
| 3031 | SmallVector<CXXRecordDecl *, 32> Bases; | |||
| 3032 | Bases.push_back(Class); | |||
| 3033 | while (!Bases.empty()) { | |||
| 3034 | // Pop this class off the stack. | |||
| 3035 | Class = Bases.pop_back_val(); | |||
| 3036 | ||||
| 3037 | // Visit the base classes. | |||
| 3038 | for (const auto &Base : Class->bases()) { | |||
| 3039 | const RecordType *BaseType = Base.getType()->getAs<RecordType>(); | |||
| 3040 | // In dependent contexts, we do ADL twice, and the first time around, | |||
| 3041 | // the base type might be a dependent TemplateSpecializationType, or a | |||
| 3042 | // TemplateTypeParmType. If that happens, simply ignore it. | |||
| 3043 | // FIXME: If we want to support export, we probably need to add the | |||
| 3044 | // namespace of the template in a TemplateSpecializationType, or even | |||
| 3045 | // the classes and namespaces of known non-dependent arguments. | |||
| 3046 | if (!BaseType) | |||
| 3047 | continue; | |||
| 3048 | CXXRecordDecl *BaseDecl = cast<CXXRecordDecl>(BaseType->getDecl()); | |||
| 3049 | if (Result.addClassTransitive(BaseDecl)) { | |||
| 3050 | // Find the associated namespace for this base class. | |||
| 3051 | DeclContext *BaseCtx = BaseDecl->getDeclContext(); | |||
| 3052 | CollectEnclosingNamespace(Result.Namespaces, BaseCtx); | |||
| 3053 | ||||
| 3054 | // Make sure we visit the bases of this base class. | |||
| 3055 | if (BaseDecl->bases_begin() != BaseDecl->bases_end()) | |||
| 3056 | Bases.push_back(BaseDecl); | |||
| 3057 | } | |||
| 3058 | } | |||
| 3059 | } | |||
| 3060 | } | |||
| 3061 | ||||
| 3062 | // Add the associated classes and namespaces for | |||
| 3063 | // argument-dependent lookup with an argument of type T | |||
| 3064 | // (C++ [basic.lookup.koenig]p2). | |||
| 3065 | static void | |||
| 3066 | addAssociatedClassesAndNamespaces(AssociatedLookup &Result, QualType Ty) { | |||
| 3067 | // C++ [basic.lookup.koenig]p2: | |||
| 3068 | // | |||
| 3069 | // For each argument type T in the function call, there is a set | |||
| 3070 | // of zero or more associated namespaces and a set of zero or more | |||
| 3071 | // associated classes to be considered. The sets of namespaces and | |||
| 3072 | // classes is determined entirely by the types of the function | |||
| 3073 | // arguments (and the namespace of any template template | |||
| 3074 | // argument). Typedef names and using-declarations used to specify | |||
| 3075 | // the types do not contribute to this set. The sets of namespaces | |||
| 3076 | // and classes are determined in the following way: | |||
| 3077 | ||||
| 3078 | SmallVector<const Type *, 16> Queue; | |||
| 3079 | const Type *T = Ty->getCanonicalTypeInternal().getTypePtr(); | |||
| 3080 | ||||
| 3081 | while (true) { | |||
| 3082 | switch (T->getTypeClass()) { | |||
| 3083 | ||||
| 3084 | #define TYPE(Class, Base) | |||
| 3085 | #define DEPENDENT_TYPE(Class, Base) case Type::Class: | |||
| 3086 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: | |||
| 3087 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) case Type::Class: | |||
| 3088 | #define ABSTRACT_TYPE(Class, Base) | |||
| 3089 | #include "clang/AST/TypeNodes.inc" | |||
| 3090 | // T is canonical. We can also ignore dependent types because | |||
| 3091 | // we don't need to do ADL at the definition point, but if we | |||
| 3092 | // wanted to implement template export (or if we find some other | |||
| 3093 | // use for associated classes and namespaces...) this would be | |||
| 3094 | // wrong. | |||
| 3095 | break; | |||
| 3096 | ||||
| 3097 | // -- If T is a pointer to U or an array of U, its associated | |||
| 3098 | // namespaces and classes are those associated with U. | |||
| 3099 | case Type::Pointer: | |||
| 3100 | T = cast<PointerType>(T)->getPointeeType().getTypePtr(); | |||
| 3101 | continue; | |||
| 3102 | case Type::ConstantArray: | |||
| 3103 | case Type::IncompleteArray: | |||
| 3104 | case Type::VariableArray: | |||
| 3105 | T = cast<ArrayType>(T)->getElementType().getTypePtr(); | |||
| 3106 | continue; | |||
| 3107 | ||||
| 3108 | // -- If T is a fundamental type, its associated sets of | |||
| 3109 | // namespaces and classes are both empty. | |||
| 3110 | case Type::Builtin: | |||
| 3111 | break; | |||
| 3112 | ||||
| 3113 | // -- If T is a class type (including unions), its associated | |||
| 3114 | // classes are: the class itself; the class of which it is | |||
| 3115 | // a member, if any; and its direct and indirect base classes. | |||
| 3116 | // Its associated namespaces are the innermost enclosing | |||
| 3117 | // namespaces of its associated classes. | |||
| 3118 | case Type::Record: { | |||
| 3119 | CXXRecordDecl *Class = | |||
| 3120 | cast<CXXRecordDecl>(cast<RecordType>(T)->getDecl()); | |||
| 3121 | addAssociatedClassesAndNamespaces(Result, Class); | |||
| 3122 | break; | |||
| 3123 | } | |||
| 3124 | ||||
| 3125 | // -- If T is an enumeration type, its associated namespace | |||
| 3126 | // is the innermost enclosing namespace of its declaration. | |||
| 3127 | // If it is a class member, its associated class is the | |||
| 3128 | // member’s class; else it has no associated class. | |||
| 3129 | case Type::Enum: { | |||
| 3130 | EnumDecl *Enum = cast<EnumType>(T)->getDecl(); | |||
| 3131 | ||||
| 3132 | DeclContext *Ctx = Enum->getDeclContext(); | |||
| 3133 | if (CXXRecordDecl *EnclosingClass = dyn_cast<CXXRecordDecl>(Ctx)) | |||
| 3134 | Result.Classes.insert(EnclosingClass); | |||
| 3135 | ||||
| 3136 | // Add the associated namespace for this enumeration. | |||
| 3137 | CollectEnclosingNamespace(Result.Namespaces, Ctx); | |||
| 3138 | ||||
| 3139 | break; | |||
| 3140 | } | |||
| 3141 | ||||
| 3142 | // -- If T is a function type, its associated namespaces and | |||
| 3143 | // classes are those associated with the function parameter | |||
| 3144 | // types and those associated with the return type. | |||
| 3145 | case Type::FunctionProto: { | |||
| 3146 | const FunctionProtoType *Proto = cast<FunctionProtoType>(T); | |||
| 3147 | for (const auto &Arg : Proto->param_types()) | |||
| 3148 | Queue.push_back(Arg.getTypePtr()); | |||
| 3149 | // fallthrough | |||
| 3150 | [[fallthrough]]; | |||
| 3151 | } | |||
| 3152 | case Type::FunctionNoProto: { | |||
| 3153 | const FunctionType *FnType = cast<FunctionType>(T); | |||
| 3154 | T = FnType->getReturnType().getTypePtr(); | |||
| 3155 | continue; | |||
| 3156 | } | |||
| 3157 | ||||
| 3158 | // -- If T is a pointer to a member function of a class X, its | |||
| 3159 | // associated namespaces and classes are those associated | |||
| 3160 | // with the function parameter types and return type, | |||
| 3161 | // together with those associated with X. | |||
| 3162 | // | |||
| 3163 | // -- If T is a pointer to a data member of class X, its | |||
| 3164 | // associated namespaces and classes are those associated | |||
| 3165 | // with the member type together with those associated with | |||
| 3166 | // X. | |||
| 3167 | case Type::MemberPointer: { | |||
| 3168 | const MemberPointerType *MemberPtr = cast<MemberPointerType>(T); | |||
| 3169 | ||||
| 3170 | // Queue up the class type into which this points. | |||
| 3171 | Queue.push_back(MemberPtr->getClass()); | |||
| 3172 | ||||
| 3173 | // And directly continue with the pointee type. | |||
| 3174 | T = MemberPtr->getPointeeType().getTypePtr(); | |||
| 3175 | continue; | |||
| 3176 | } | |||
| 3177 | ||||
| 3178 | // As an extension, treat this like a normal pointer. | |||
| 3179 | case Type::BlockPointer: | |||
| 3180 | T = cast<BlockPointerType>(T)->getPointeeType().getTypePtr(); | |||
| 3181 | continue; | |||
| 3182 | ||||
| 3183 | // References aren't covered by the standard, but that's such an | |||
| 3184 | // obvious defect that we cover them anyway. | |||
| 3185 | case Type::LValueReference: | |||
| 3186 | case Type::RValueReference: | |||
| 3187 | T = cast<ReferenceType>(T)->getPointeeType().getTypePtr(); | |||
| 3188 | continue; | |||
| 3189 | ||||
| 3190 | // These are fundamental types. | |||
| 3191 | case Type::Vector: | |||
| 3192 | case Type::ExtVector: | |||
| 3193 | case Type::ConstantMatrix: | |||
| 3194 | case Type::Complex: | |||
| 3195 | case Type::BitInt: | |||
| 3196 | break; | |||
| 3197 | ||||
| 3198 | // Non-deduced auto types only get here for error cases. | |||
| 3199 | case Type::Auto: | |||
| 3200 | case Type::DeducedTemplateSpecialization: | |||
| 3201 | break; | |||
| 3202 | ||||
| 3203 | // If T is an Objective-C object or interface type, or a pointer to an | |||
| 3204 | // object or interface type, the associated namespace is the global | |||
| 3205 | // namespace. | |||
| 3206 | case Type::ObjCObject: | |||
| 3207 | case Type::ObjCInterface: | |||
| 3208 | case Type::ObjCObjectPointer: | |||
| 3209 | Result.Namespaces.insert(Result.S.Context.getTranslationUnitDecl()); | |||
| 3210 | break; | |||
| 3211 | ||||
| 3212 | // Atomic types are just wrappers; use the associations of the | |||
| 3213 | // contained type. | |||
| 3214 | case Type::Atomic: | |||
| 3215 | T = cast<AtomicType>(T)->getValueType().getTypePtr(); | |||
| 3216 | continue; | |||
| 3217 | case Type::Pipe: | |||
| 3218 | T = cast<PipeType>(T)->getElementType().getTypePtr(); | |||
| 3219 | continue; | |||
| 3220 | } | |||
| 3221 | ||||
| 3222 | if (Queue.empty()) | |||
| 3223 | break; | |||
| 3224 | T = Queue.pop_back_val(); | |||
| 3225 | } | |||
| 3226 | } | |||
| 3227 | ||||
| 3228 | /// Find the associated classes and namespaces for | |||
| 3229 | /// argument-dependent lookup for a call with the given set of | |||
| 3230 | /// arguments. | |||
| 3231 | /// | |||
| 3232 | /// This routine computes the sets of associated classes and associated | |||
| 3233 | /// namespaces searched by argument-dependent lookup | |||
| 3234 | /// (C++ [basic.lookup.argdep]) for a given set of arguments. | |||
| 3235 | void Sema::FindAssociatedClassesAndNamespaces( | |||
| 3236 | SourceLocation InstantiationLoc, ArrayRef<Expr *> Args, | |||
| 3237 | AssociatedNamespaceSet &AssociatedNamespaces, | |||
| 3238 | AssociatedClassSet &AssociatedClasses) { | |||
| 3239 | AssociatedNamespaces.clear(); | |||
| 3240 | AssociatedClasses.clear(); | |||
| 3241 | ||||
| 3242 | AssociatedLookup Result(*this, InstantiationLoc, | |||
| 3243 | AssociatedNamespaces, AssociatedClasses); | |||
| 3244 | ||||
| 3245 | // C++ [basic.lookup.koenig]p2: | |||
| 3246 | // For each argument type T in the function call, there is a set | |||
| 3247 | // of zero or more associated namespaces and a set of zero or more | |||
| 3248 | // associated classes to be considered. The sets of namespaces and | |||
| 3249 | // classes is determined entirely by the types of the function | |||
| 3250 | // arguments (and the namespace of any template template | |||
| 3251 | // argument). | |||
| 3252 | for (unsigned ArgIdx = 0; ArgIdx != Args.size(); ++ArgIdx) { | |||
| 3253 | Expr *Arg = Args[ArgIdx]; | |||
| 3254 | ||||
| 3255 | if (Arg->getType() != Context.OverloadTy) { | |||
| 3256 | addAssociatedClassesAndNamespaces(Result, Arg->getType()); | |||
| 3257 | continue; | |||
| 3258 | } | |||
| 3259 | ||||
| 3260 | // [...] In addition, if the argument is the name or address of a | |||
| 3261 | // set of overloaded functions and/or function templates, its | |||
| 3262 | // associated classes and namespaces are the union of those | |||
| 3263 | // associated with each of the members of the set: the namespace | |||
| 3264 | // in which the function or function template is defined and the | |||
| 3265 | // classes and namespaces associated with its (non-dependent) | |||
| 3266 | // parameter types and return type. | |||
| 3267 | OverloadExpr *OE = OverloadExpr::find(Arg).Expression; | |||
| 3268 | ||||
| 3269 | for (const NamedDecl *D : OE->decls()) { | |||
| 3270 | // Look through any using declarations to find the underlying function. | |||
| 3271 | const FunctionDecl *FDecl = D->getUnderlyingDecl()->getAsFunction(); | |||
| 3272 | ||||
| 3273 | // Add the classes and namespaces associated with the parameter | |||
| 3274 | // types and return type of this function. | |||
| 3275 | addAssociatedClassesAndNamespaces(Result, FDecl->getType()); | |||
| 3276 | } | |||
| 3277 | } | |||
| 3278 | } | |||
| 3279 | ||||
| 3280 | NamedDecl *Sema::LookupSingleName(Scope *S, DeclarationName Name, | |||
| 3281 | SourceLocation Loc, | |||
| 3282 | LookupNameKind NameKind, | |||
| 3283 | RedeclarationKind Redecl) { | |||
| 3284 | LookupResult R(*this, Name, Loc, NameKind, Redecl); | |||
| 3285 | LookupName(R, S); | |||
| 3286 | return R.getAsSingle<NamedDecl>(); | |||
| 3287 | } | |||
| 3288 | ||||
| 3289 | /// Find the protocol with the given name, if any. | |||
| 3290 | ObjCProtocolDecl *Sema::LookupProtocol(IdentifierInfo *II, | |||
| 3291 | SourceLocation IdLoc, | |||
| 3292 | RedeclarationKind Redecl) { | |||
| 3293 | Decl *D = LookupSingleName(TUScope, II, IdLoc, | |||
| 3294 | LookupObjCProtocolName, Redecl); | |||
| 3295 | return cast_or_null<ObjCProtocolDecl>(D); | |||
| 3296 | } | |||
| 3297 | ||||
| 3298 | void Sema::LookupOverloadedOperatorName(OverloadedOperatorKind Op, Scope *S, | |||
| 3299 | UnresolvedSetImpl &Functions) { | |||
| 3300 | // C++ [over.match.oper]p3: | |||
| 3301 | // -- The set of non-member candidates is the result of the | |||
| 3302 | // unqualified lookup of operator@ in the context of the | |||
| 3303 | // expression according to the usual rules for name lookup in | |||
| 3304 | // unqualified function calls (3.4.2) except that all member | |||
| 3305 | // functions are ignored. | |||
| 3306 | DeclarationName OpName = Context.DeclarationNames.getCXXOperatorName(Op); | |||
| 3307 | LookupResult Operators(*this, OpName, SourceLocation(), LookupOperatorName); | |||
| 3308 | LookupName(Operators, S); | |||
| 3309 | ||||
| 3310 | assert(!Operators.isAmbiguous() && "Operator lookup cannot be ambiguous")(static_cast <bool> (!Operators.isAmbiguous() && "Operator lookup cannot be ambiguous") ? void (0) : __assert_fail ("!Operators.isAmbiguous() && \"Operator lookup cannot be ambiguous\"" , "clang/lib/Sema/SemaLookup.cpp", 3310, __extension__ __PRETTY_FUNCTION__ )); | |||
| 3311 | Functions.append(Operators.begin(), Operators.end()); | |||
| 3312 | } | |||
| 3313 | ||||
| 3314 | Sema::SpecialMemberOverloadResult Sema::LookupSpecialMember(CXXRecordDecl *RD, | |||
| 3315 | CXXSpecialMember SM, | |||
| 3316 | bool ConstArg, | |||
| 3317 | bool VolatileArg, | |||
| 3318 | bool RValueThis, | |||
| 3319 | bool ConstThis, | |||
| 3320 | bool VolatileThis) { | |||
| 3321 | assert(CanDeclareSpecialMemberFunction(RD) &&(static_cast <bool> (CanDeclareSpecialMemberFunction(RD ) && "doing special member lookup into record that isn't fully complete" ) ? void (0) : __assert_fail ("CanDeclareSpecialMemberFunction(RD) && \"doing special member lookup into record that isn't fully complete\"" , "clang/lib/Sema/SemaLookup.cpp", 3322, __extension__ __PRETTY_FUNCTION__ )) | |||
| 3322 | "doing special member lookup into record that isn't fully complete")(static_cast <bool> (CanDeclareSpecialMemberFunction(RD ) && "doing special member lookup into record that isn't fully complete" ) ? void (0) : __assert_fail ("CanDeclareSpecialMemberFunction(RD) && \"doing special member lookup into record that isn't fully complete\"" , "clang/lib/Sema/SemaLookup.cpp", 3322, __extension__ __PRETTY_FUNCTION__ )); | |||
| 3323 | RD = RD->getDefinition(); | |||
| 3324 | if (RValueThis || ConstThis || VolatileThis) | |||
| 3325 | assert((SM == CXXCopyAssignment || SM == CXXMoveAssignment) &&(static_cast <bool> ((SM == CXXCopyAssignment || SM == CXXMoveAssignment ) && "constructors and destructors always have unqualified lvalue this" ) ? void (0) : __assert_fail ("(SM == CXXCopyAssignment || SM == CXXMoveAssignment) && \"constructors and destructors always have unqualified lvalue this\"" , "clang/lib/Sema/SemaLookup.cpp", 3326, __extension__ __PRETTY_FUNCTION__ )) | |||
| 3326 | "constructors and destructors always have unqualified lvalue this")(static_cast <bool> ((SM == CXXCopyAssignment || SM == CXXMoveAssignment ) && "constructors and destructors always have unqualified lvalue this" ) ? void (0) : __assert_fail ("(SM == CXXCopyAssignment || SM == CXXMoveAssignment) && \"constructors and destructors always have unqualified lvalue this\"" , "clang/lib/Sema/SemaLookup.cpp", 3326, __extension__ __PRETTY_FUNCTION__ )); | |||
| 3327 | if (ConstArg || VolatileArg) | |||
| 3328 | assert((SM != CXXDefaultConstructor && SM != CXXDestructor) &&(static_cast <bool> ((SM != CXXDefaultConstructor && SM != CXXDestructor) && "parameter-less special members can't have qualified arguments" ) ? void (0) : __assert_fail ("(SM != CXXDefaultConstructor && SM != CXXDestructor) && \"parameter-less special members can't have qualified arguments\"" , "clang/lib/Sema/SemaLookup.cpp", 3329, __extension__ __PRETTY_FUNCTION__ )) | |||
| 3329 | "parameter-less special members can't have qualified arguments")(static_cast <bool> ((SM != CXXDefaultConstructor && SM != CXXDestructor) && "parameter-less special members can't have qualified arguments" ) ? void (0) : __assert_fail ("(SM != CXXDefaultConstructor && SM != CXXDestructor) && \"parameter-less special members can't have qualified arguments\"" , "clang/lib/Sema/SemaLookup.cpp", 3329, __extension__ __PRETTY_FUNCTION__ )); | |||
| 3330 | ||||
| 3331 | // FIXME: Get the caller to pass in a location for the lookup. | |||
| 3332 | SourceLocation LookupLoc = RD->getLocation(); | |||
| 3333 | ||||
| 3334 | llvm::FoldingSetNodeID ID; | |||
| 3335 | ID.AddPointer(RD); | |||
| 3336 | ID.AddInteger(SM); | |||
| 3337 | ID.AddInteger(ConstArg); | |||
| 3338 | ID.AddInteger(VolatileArg); | |||
| 3339 | ID.AddInteger(RValueThis); | |||
| 3340 | ID.AddInteger(ConstThis); | |||
| 3341 | ID.AddInteger(VolatileThis); | |||
| 3342 | ||||
| 3343 | void *InsertPoint; | |||
| 3344 | SpecialMemberOverloadResultEntry *Result = | |||
| 3345 | SpecialMemberCache.FindNodeOrInsertPos(ID, InsertPoint); | |||
| 3346 | ||||
| 3347 | // This was already cached | |||
| 3348 | if (Result) | |||
| 3349 | return *Result; | |||
| 3350 | ||||
| 3351 | Result = BumpAlloc.Allocate<SpecialMemberOverloadResultEntry>(); | |||
| 3352 | Result = new (Result) SpecialMemberOverloadResultEntry(ID); | |||
| 3353 | SpecialMemberCache.InsertNode(Result, InsertPoint); | |||
| 3354 | ||||
| 3355 | if (SM == CXXDestructor) { | |||
| 3356 | if (RD->needsImplicitDestructor()) { | |||
| 3357 | runWithSufficientStackSpace(RD->getLocation(), [&] { | |||
| 3358 | DeclareImplicitDestructor(RD); | |||
| 3359 | }); | |||
| 3360 | } | |||
| 3361 | CXXDestructorDecl *DD = RD->getDestructor(); | |||
| 3362 | Result->setMethod(DD); | |||
| 3363 | Result->setKind(DD && !DD->isDeleted() | |||
| 3364 | ? SpecialMemberOverloadResult::Success | |||
| 3365 | : SpecialMemberOverloadResult::NoMemberOrDeleted); | |||
| 3366 | return *Result; | |||
| 3367 | } | |||
| 3368 | ||||
| 3369 | // Prepare for overload resolution. Here we construct a synthetic argument | |||
| 3370 | // if necessary and make sure that implicit functions are declared. | |||
| 3371 | CanQualType CanTy = Context.getCanonicalType(Context.getTagDeclType(RD)); | |||
| 3372 | DeclarationName Name; | |||
| 3373 | Expr *Arg = nullptr; | |||
| 3374 | unsigned NumArgs; | |||
| 3375 | ||||
| 3376 | QualType ArgType = CanTy; | |||
| 3377 | ExprValueKind VK = VK_LValue; | |||
| 3378 | ||||
| 3379 | if (SM == CXXDefaultConstructor) { | |||
| 3380 | Name = Context.DeclarationNames.getCXXConstructorName(CanTy); | |||
| 3381 | NumArgs = 0; | |||
| 3382 | if (RD->needsImplicitDefaultConstructor()) { | |||
| 3383 | runWithSufficientStackSpace(RD->getLocation(), [&] { | |||
| 3384 | DeclareImplicitDefaultConstructor(RD); | |||
| 3385 | }); | |||
| 3386 | } | |||
| 3387 | } else { | |||
| 3388 | if (SM == CXXCopyConstructor || SM == CXXMoveConstructor) { | |||
| 3389 | Name = Context.DeclarationNames.getCXXConstructorName(CanTy); | |||
| 3390 | if (RD->needsImplicitCopyConstructor()) { | |||
| 3391 | runWithSufficientStackSpace(RD->getLocation(), [&] { | |||
| 3392 | DeclareImplicitCopyConstructor(RD); | |||
| 3393 | }); | |||
| 3394 | } | |||
| 3395 | if (getLangOpts().CPlusPlus11 && RD->needsImplicitMoveConstructor()) { | |||
| 3396 | runWithSufficientStackSpace(RD->getLocation(), [&] { | |||
| 3397 | DeclareImplicitMoveConstructor(RD); | |||
| 3398 | }); | |||
| 3399 | } | |||
| 3400 | } else { | |||
| 3401 | Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal); | |||
| 3402 | if (RD->needsImplicitCopyAssignment()) { | |||
| 3403 | runWithSufficientStackSpace(RD->getLocation(), [&] { | |||
| 3404 | DeclareImplicitCopyAssignment(RD); | |||
| 3405 | }); | |||
| 3406 | } | |||
| 3407 | if (getLangOpts().CPlusPlus11 && RD->needsImplicitMoveAssignment()) { | |||
| 3408 | runWithSufficientStackSpace(RD->getLocation(), [&] { | |||
| 3409 | DeclareImplicitMoveAssignment(RD); | |||
| 3410 | }); | |||
| 3411 | } | |||
| 3412 | } | |||
| 3413 | ||||
| 3414 | if (ConstArg) | |||
| 3415 | ArgType.addConst(); | |||
| 3416 | if (VolatileArg) | |||
| 3417 | ArgType.addVolatile(); | |||
| 3418 | ||||
| 3419 | // This isn't /really/ specified by the standard, but it's implied | |||
| 3420 | // we should be working from a PRValue in the case of move to ensure | |||
| 3421 | // that we prefer to bind to rvalue references, and an LValue in the | |||
| 3422 | // case of copy to ensure we don't bind to rvalue references. | |||
| 3423 | // Possibly an XValue is actually correct in the case of move, but | |||
| 3424 | // there is no semantic difference for class types in this restricted | |||
| 3425 | // case. | |||
| 3426 | if (SM == CXXCopyConstructor || SM == CXXCopyAssignment) | |||
| 3427 | VK = VK_LValue; | |||
| 3428 | else | |||
| 3429 | VK = VK_PRValue; | |||
| 3430 | } | |||
| 3431 | ||||
| 3432 | OpaqueValueExpr FakeArg(LookupLoc, ArgType, VK); | |||
| 3433 | ||||
| 3434 | if (SM != CXXDefaultConstructor) { | |||
| 3435 | NumArgs = 1; | |||
| 3436 | Arg = &FakeArg; | |||
| 3437 | } | |||
| 3438 | ||||
| 3439 | // Create the object argument | |||
| 3440 | QualType ThisTy = CanTy; | |||
| 3441 | if (ConstThis) | |||
| 3442 | ThisTy.addConst(); | |||
| 3443 | if (VolatileThis) | |||
| 3444 | ThisTy.addVolatile(); | |||
| 3445 | Expr::Classification Classification = | |||
| 3446 | OpaqueValueExpr(LookupLoc, ThisTy, RValueThis ? VK_PRValue : VK_LValue) | |||
| 3447 | .Classify(Context); | |||
| 3448 | ||||
| 3449 | // Now we perform lookup on the name we computed earlier and do overload | |||
| 3450 | // resolution. Lookup is only performed directly into the class since there | |||
| 3451 | // will always be a (possibly implicit) declaration to shadow any others. | |||
| 3452 | OverloadCandidateSet OCS(LookupLoc, OverloadCandidateSet::CSK_Normal); | |||
| 3453 | DeclContext::lookup_result R = RD->lookup(Name); | |||
| 3454 | ||||
| 3455 | if (R.empty()) { | |||
| 3456 | // We might have no default constructor because we have a lambda's closure | |||
| 3457 | // type, rather than because there's some other declared constructor. | |||
| 3458 | // Every class has a copy/move constructor, copy/move assignment, and | |||
| 3459 | // destructor. | |||
| 3460 | assert(SM == CXXDefaultConstructor &&(static_cast <bool> (SM == CXXDefaultConstructor && "lookup for a constructor or assignment operator was empty") ? void (0) : __assert_fail ("SM == CXXDefaultConstructor && \"lookup for a constructor or assignment operator was empty\"" , "clang/lib/Sema/SemaLookup.cpp", 3461, __extension__ __PRETTY_FUNCTION__ )) | |||
| 3461 | "lookup for a constructor or assignment operator was empty")(static_cast <bool> (SM == CXXDefaultConstructor && "lookup for a constructor or assignment operator was empty") ? void (0) : __assert_fail ("SM == CXXDefaultConstructor && \"lookup for a constructor or assignment operator was empty\"" , "clang/lib/Sema/SemaLookup.cpp", 3461, __extension__ __PRETTY_FUNCTION__ )); | |||
| 3462 | Result->setMethod(nullptr); | |||
| 3463 | Result->setKind(SpecialMemberOverloadResult::NoMemberOrDeleted); | |||
| 3464 | return *Result; | |||
| 3465 | } | |||
| 3466 | ||||
| 3467 | // Copy the candidates as our processing of them may load new declarations | |||
| 3468 | // from an external source and invalidate lookup_result. | |||
| 3469 | SmallVector<NamedDecl *, 8> Candidates(R.begin(), R.end()); | |||
| 3470 | ||||
| 3471 | for (NamedDecl *CandDecl : Candidates) { | |||
| 3472 | if (CandDecl->isInvalidDecl()) | |||
| 3473 | continue; | |||
| 3474 | ||||
| 3475 | DeclAccessPair Cand = DeclAccessPair::make(CandDecl, AS_public); | |||
| 3476 | auto CtorInfo = getConstructorInfo(Cand); | |||
| 3477 | if (CXXMethodDecl *M = dyn_cast<CXXMethodDecl>(Cand->getUnderlyingDecl())) { | |||
| 3478 | if (SM == CXXCopyAssignment || SM == CXXMoveAssignment) | |||
| 3479 | AddMethodCandidate(M, Cand, RD, ThisTy, Classification, | |||
| 3480 | llvm::ArrayRef(&Arg, NumArgs), OCS, true); | |||
| 3481 | else if (CtorInfo) | |||
| 3482 | AddOverloadCandidate(CtorInfo.Constructor, CtorInfo.FoundDecl, | |||
| 3483 | llvm::ArrayRef(&Arg, NumArgs), OCS, | |||
| 3484 | /*SuppressUserConversions*/ true); | |||
| 3485 | else | |||
| 3486 | AddOverloadCandidate(M, Cand, llvm::ArrayRef(&Arg, NumArgs), OCS, | |||
| 3487 | /*SuppressUserConversions*/ true); | |||
| 3488 | } else if (FunctionTemplateDecl *Tmpl = | |||
| 3489 | dyn_cast<FunctionTemplateDecl>(Cand->getUnderlyingDecl())) { | |||
| 3490 | if (SM == CXXCopyAssignment || SM == CXXMoveAssignment) | |||
| 3491 | AddMethodTemplateCandidate(Tmpl, Cand, RD, nullptr, ThisTy, | |||
| 3492 | Classification, | |||
| 3493 | llvm::ArrayRef(&Arg, NumArgs), OCS, true); | |||
| 3494 | else if (CtorInfo) | |||
| 3495 | AddTemplateOverloadCandidate(CtorInfo.ConstructorTmpl, | |||
| 3496 | CtorInfo.FoundDecl, nullptr, | |||
| 3497 | llvm::ArrayRef(&Arg, NumArgs), OCS, true); | |||
| 3498 | else | |||
| 3499 | AddTemplateOverloadCandidate(Tmpl, Cand, nullptr, | |||
| 3500 | llvm::ArrayRef(&Arg, NumArgs), OCS, true); | |||
| 3501 | } else { | |||
| 3502 | assert(isa<UsingDecl>(Cand.getDecl()) &&(static_cast <bool> (isa<UsingDecl>(Cand.getDecl( )) && "illegal Kind of operator = Decl") ? void (0) : __assert_fail ("isa<UsingDecl>(Cand.getDecl()) && \"illegal Kind of operator = Decl\"" , "clang/lib/Sema/SemaLookup.cpp", 3503, __extension__ __PRETTY_FUNCTION__ )) | |||
| 3503 | "illegal Kind of operator = Decl")(static_cast <bool> (isa<UsingDecl>(Cand.getDecl( )) && "illegal Kind of operator = Decl") ? void (0) : __assert_fail ("isa<UsingDecl>(Cand.getDecl()) && \"illegal Kind of operator = Decl\"" , "clang/lib/Sema/SemaLookup.cpp", 3503, __extension__ __PRETTY_FUNCTION__ )); | |||
| 3504 | } | |||
| 3505 | } | |||
| 3506 | ||||
| 3507 | OverloadCandidateSet::iterator Best; | |||
| 3508 | switch (OCS.BestViableFunction(*this, LookupLoc, Best)) { | |||
| 3509 | case OR_Success: | |||
| 3510 | Result->setMethod(cast<CXXMethodDecl>(Best->Function)); | |||
| 3511 | Result->setKind(SpecialMemberOverloadResult::Success); | |||
| 3512 | break; | |||
| 3513 | ||||
| 3514 | case OR_Deleted: | |||
| 3515 | Result->setMethod(cast<CXXMethodDecl>(Best->Function)); | |||
| 3516 | Result->setKind(SpecialMemberOverloadResult::NoMemberOrDeleted); | |||
| 3517 | break; | |||
| 3518 | ||||
| 3519 | case OR_Ambiguous: | |||
| 3520 | Result->setMethod(nullptr); | |||
| 3521 | Result->setKind(SpecialMemberOverloadResult::Ambiguous); | |||
| 3522 | break; | |||
| 3523 | ||||
| 3524 | case OR_No_Viable_Function: | |||
| 3525 | Result->setMethod(nullptr); | |||
| 3526 | Result->setKind(SpecialMemberOverloadResult::NoMemberOrDeleted); | |||
| 3527 | break; | |||
| 3528 | } | |||
| 3529 | ||||
| 3530 | return *Result; | |||
| 3531 | } | |||
| 3532 | ||||
| 3533 | /// Look up the default constructor for the given class. | |||
| 3534 | CXXConstructorDecl *Sema::LookupDefaultConstructor(CXXRecordDecl *Class) { | |||
| 3535 | SpecialMemberOverloadResult Result = | |||
| 3536 | LookupSpecialMember(Class, CXXDefaultConstructor, false, false, false, | |||
| 3537 | false, false); | |||
| 3538 | ||||
| 3539 | return cast_or_null<CXXConstructorDecl>(Result.getMethod()); | |||
| 3540 | } | |||
| 3541 | ||||
| 3542 | /// Look up the copying constructor for the given class. | |||
| 3543 | CXXConstructorDecl *Sema::LookupCopyingConstructor(CXXRecordDecl *Class, | |||
| 3544 | unsigned Quals) { | |||
| 3545 | assert(!(Quals & ~(Qualifiers::Const | Qualifiers::Volatile)) &&(static_cast <bool> (!(Quals & ~(Qualifiers::Const | Qualifiers::Volatile)) && "non-const, non-volatile qualifiers for copy ctor arg" ) ? void (0) : __assert_fail ("!(Quals & ~(Qualifiers::Const | Qualifiers::Volatile)) && \"non-const, non-volatile qualifiers for copy ctor arg\"" , "clang/lib/Sema/SemaLookup.cpp", 3546, __extension__ __PRETTY_FUNCTION__ )) | |||
| 3546 | "non-const, non-volatile qualifiers for copy ctor arg")(static_cast <bool> (!(Quals & ~(Qualifiers::Const | Qualifiers::Volatile)) && "non-const, non-volatile qualifiers for copy ctor arg" ) ? void (0) : __assert_fail ("!(Quals & ~(Qualifiers::Const | Qualifiers::Volatile)) && \"non-const, non-volatile qualifiers for copy ctor arg\"" , "clang/lib/Sema/SemaLookup.cpp", 3546, __extension__ __PRETTY_FUNCTION__ )); | |||
| 3547 | SpecialMemberOverloadResult Result = | |||
| 3548 | LookupSpecialMember(Class, CXXCopyConstructor, Quals & Qualifiers::Const, | |||
| 3549 | Quals & Qualifiers::Volatile, false, false, false); | |||
| 3550 | ||||
| 3551 | return cast_or_null<CXXConstructorDecl>(Result.getMethod()); | |||
| 3552 | } | |||
| 3553 | ||||
| 3554 | /// Look up the moving constructor for the given class. | |||
| 3555 | CXXConstructorDecl *Sema::LookupMovingConstructor(CXXRecordDecl *Class, | |||
| 3556 | unsigned Quals) { | |||
| 3557 | SpecialMemberOverloadResult Result = | |||
| 3558 | LookupSpecialMember(Class, CXXMoveConstructor, Quals & Qualifiers::Const, | |||
| 3559 | Quals & Qualifiers::Volatile, false, false, false); | |||
| 3560 | ||||
| 3561 | return cast_or_null<CXXConstructorDecl>(Result.getMethod()); | |||
| 3562 | } | |||
| 3563 | ||||
| 3564 | /// Look up the constructors for the given class. | |||
| 3565 | DeclContext::lookup_result Sema::LookupConstructors(CXXRecordDecl *Class) { | |||
| 3566 | // If the implicit constructors have not yet been declared, do so now. | |||
| 3567 | if (CanDeclareSpecialMemberFunction(Class)) { | |||
| 3568 | runWithSufficientStackSpace(Class->getLocation(), [&] { | |||
| 3569 | if (Class->needsImplicitDefaultConstructor()) | |||
| 3570 | DeclareImplicitDefaultConstructor(Class); | |||
| 3571 | if (Class->needsImplicitCopyConstructor()) | |||
| 3572 | DeclareImplicitCopyConstructor(Class); | |||
| 3573 | if (getLangOpts().CPlusPlus11 && Class->needsImplicitMoveConstructor()) | |||
| 3574 | DeclareImplicitMoveConstructor(Class); | |||
| 3575 | }); | |||
| 3576 | } | |||
| 3577 | ||||
| 3578 | CanQualType T = Context.getCanonicalType(Context.getTypeDeclType(Class)); | |||
| 3579 | DeclarationName Name = Context.DeclarationNames.getCXXConstructorName(T); | |||
| 3580 | return Class->lookup(Name); | |||
| 3581 | } | |||
| 3582 | ||||
| 3583 | /// Look up the copying assignment operator for the given class. | |||
| 3584 | CXXMethodDecl *Sema::LookupCopyingAssignment(CXXRecordDecl *Class, | |||
| 3585 | unsigned Quals, bool RValueThis, | |||
| 3586 | unsigned ThisQuals) { | |||
| 3587 | assert(!(Quals & ~(Qualifiers::Const | Qualifiers::Volatile)) &&(static_cast <bool> (!(Quals & ~(Qualifiers::Const | Qualifiers::Volatile)) && "non-const, non-volatile qualifiers for copy assignment arg" ) ? void (0) : __assert_fail ("!(Quals & ~(Qualifiers::Const | Qualifiers::Volatile)) && \"non-const, non-volatile qualifiers for copy assignment arg\"" , "clang/lib/Sema/SemaLookup.cpp", 3588, __extension__ __PRETTY_FUNCTION__ )) | |||
| 3588 | "non-const, non-volatile qualifiers for copy assignment arg")(static_cast <bool> (!(Quals & ~(Qualifiers::Const | Qualifiers::Volatile)) && "non-const, non-volatile qualifiers for copy assignment arg" ) ? void (0) : __assert_fail ("!(Quals & ~(Qualifiers::Const | Qualifiers::Volatile)) && \"non-const, non-volatile qualifiers for copy assignment arg\"" , "clang/lib/Sema/SemaLookup.cpp", 3588, __extension__ __PRETTY_FUNCTION__ )); | |||
| 3589 | assert(!(ThisQuals & ~(Qualifiers::Const | Qualifiers::Volatile)) &&(static_cast <bool> (!(ThisQuals & ~(Qualifiers::Const | Qualifiers::Volatile)) && "non-const, non-volatile qualifiers for copy assignment this" ) ? void (0) : __assert_fail ("!(ThisQuals & ~(Qualifiers::Const | Qualifiers::Volatile)) && \"non-const, non-volatile qualifiers for copy assignment this\"" , "clang/lib/Sema/SemaLookup.cpp", 3590, __extension__ __PRETTY_FUNCTION__ )) | |||
| 3590 | "non-const, non-volatile qualifiers for copy assignment this")(static_cast <bool> (!(ThisQuals & ~(Qualifiers::Const | Qualifiers::Volatile)) && "non-const, non-volatile qualifiers for copy assignment this" ) ? void (0) : __assert_fail ("!(ThisQuals & ~(Qualifiers::Const | Qualifiers::Volatile)) && \"non-const, non-volatile qualifiers for copy assignment this\"" , "clang/lib/Sema/SemaLookup.cpp", 3590, __extension__ __PRETTY_FUNCTION__ )); | |||
| 3591 | SpecialMemberOverloadResult Result = | |||
| 3592 | LookupSpecialMember(Class, CXXCopyAssignment, Quals & Qualifiers::Const, | |||
| 3593 | Quals & Qualifiers::Volatile, RValueThis, | |||
| 3594 | ThisQuals & Qualifiers::Const, | |||
| 3595 | ThisQuals & Qualifiers::Volatile); | |||
| 3596 | ||||
| 3597 | return Result.getMethod(); | |||
| 3598 | } | |||
| 3599 | ||||
| 3600 | /// Look up the moving assignment operator for the given class. | |||
| 3601 | CXXMethodDecl *Sema::LookupMovingAssignment(CXXRecordDecl *Class, | |||
| 3602 | unsigned Quals, | |||
| 3603 | bool RValueThis, | |||
| 3604 | unsigned ThisQuals) { | |||
| 3605 | assert(!(ThisQuals & ~(Qualifiers::Const | Qualifiers::Volatile)) &&(static_cast <bool> (!(ThisQuals & ~(Qualifiers::Const | Qualifiers::Volatile)) && "non-const, non-volatile qualifiers for copy assignment this" ) ? void (0) : __assert_fail ("!(ThisQuals & ~(Qualifiers::Const | Qualifiers::Volatile)) && \"non-const, non-volatile qualifiers for copy assignment this\"" , "clang/lib/Sema/SemaLookup.cpp", 3606, __extension__ __PRETTY_FUNCTION__ )) | |||
| 3606 | "non-const, non-volatile qualifiers for copy assignment this")(static_cast <bool> (!(ThisQuals & ~(Qualifiers::Const | Qualifiers::Volatile)) && "non-const, non-volatile qualifiers for copy assignment this" ) ? void (0) : __assert_fail ("!(ThisQuals & ~(Qualifiers::Const | Qualifiers::Volatile)) && \"non-const, non-volatile qualifiers for copy assignment this\"" , "clang/lib/Sema/SemaLookup.cpp", 3606, __extension__ __PRETTY_FUNCTION__ )); | |||
| 3607 | SpecialMemberOverloadResult Result = | |||
| 3608 | LookupSpecialMember(Class, CXXMoveAssignment, Quals & Qualifiers::Const, | |||
| 3609 | Quals & Qualifiers::Volatile, RValueThis, | |||
| 3610 | ThisQuals & Qualifiers::Const, | |||
| 3611 | ThisQuals & Qualifiers::Volatile); | |||
| 3612 | ||||
| 3613 | return Result.getMethod(); | |||
| 3614 | } | |||
| 3615 | ||||
| 3616 | /// Look for the destructor of the given class. | |||
| 3617 | /// | |||
| 3618 | /// During semantic analysis, this routine should be used in lieu of | |||
| 3619 | /// CXXRecordDecl::getDestructor(). | |||
| 3620 | /// | |||
| 3621 | /// \returns The destructor for this class. | |||
| 3622 | CXXDestructorDecl *Sema::LookupDestructor(CXXRecordDecl *Class) { | |||
| 3623 | return cast_or_null<CXXDestructorDecl>( | |||
| 3624 | LookupSpecialMember(Class, CXXDestructor, false, false, false, false, | |||
| 3625 | false) | |||
| 3626 | .getMethod()); | |||
| 3627 | } | |||
| 3628 | ||||
| 3629 | /// LookupLiteralOperator - Determine which literal operator should be used for | |||
| 3630 | /// a user-defined literal, per C++11 [lex.ext]. | |||
| 3631 | /// | |||
| 3632 | /// Normal overload resolution is not used to select which literal operator to | |||
| 3633 | /// call for a user-defined literal. Look up the provided literal operator name, | |||
| 3634 | /// and filter the results to the appropriate set for the given argument types. | |||
| 3635 | Sema::LiteralOperatorLookupResult | |||
| 3636 | Sema::LookupLiteralOperator(Scope *S, LookupResult &R, | |||
| 3637 | ArrayRef<QualType> ArgTys, bool AllowRaw, | |||
| 3638 | bool AllowTemplate, bool AllowStringTemplatePack, | |||
| 3639 | bool DiagnoseMissing, StringLiteral *StringLit) { | |||
| 3640 | LookupName(R, S); | |||
| 3641 | assert(R.getResultKind() != LookupResult::Ambiguous &&(static_cast <bool> (R.getResultKind() != LookupResult:: Ambiguous && "literal operator lookup can't be ambiguous" ) ? void (0) : __assert_fail ("R.getResultKind() != LookupResult::Ambiguous && \"literal operator lookup can't be ambiguous\"" , "clang/lib/Sema/SemaLookup.cpp", 3642, __extension__ __PRETTY_FUNCTION__ )) | |||
| 3642 | "literal operator lookup can't be ambiguous")(static_cast <bool> (R.getResultKind() != LookupResult:: Ambiguous && "literal operator lookup can't be ambiguous" ) ? void (0) : __assert_fail ("R.getResultKind() != LookupResult::Ambiguous && \"literal operator lookup can't be ambiguous\"" , "clang/lib/Sema/SemaLookup.cpp", 3642, __extension__ __PRETTY_FUNCTION__ )); | |||
| 3643 | ||||
| 3644 | // Filter the lookup results appropriately. | |||
| 3645 | LookupResult::Filter F = R.makeFilter(); | |||
| 3646 | ||||
| 3647 | bool AllowCooked = true; | |||
| 3648 | bool FoundRaw = false; | |||
| 3649 | bool FoundTemplate = false; | |||
| 3650 | bool FoundStringTemplatePack = false; | |||
| 3651 | bool FoundCooked = false; | |||
| 3652 | ||||
| 3653 | while (F.hasNext()) { | |||
| 3654 | Decl *D = F.next(); | |||
| 3655 | if (UsingShadowDecl *USD = dyn_cast<UsingShadowDecl>(D)) | |||
| 3656 | D = USD->getTargetDecl(); | |||
| 3657 | ||||
| 3658 | // If the declaration we found is invalid, skip it. | |||
| 3659 | if (D->isInvalidDecl()) { | |||
| 3660 | F.erase(); | |||
| 3661 | continue; | |||
| 3662 | } | |||
| 3663 | ||||
| 3664 | bool IsRaw = false; | |||
| 3665 | bool IsTemplate = false; | |||
| 3666 | bool IsStringTemplatePack = false; | |||
| 3667 | bool IsCooked = false; | |||
| 3668 | ||||
| 3669 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { | |||
| 3670 | if (FD->getNumParams() == 1 && | |||
| 3671 | FD->getParamDecl(0)->getType()->getAs<PointerType>()) | |||
| 3672 | IsRaw = true; | |||
| 3673 | else if (FD->getNumParams() == ArgTys.size()) { | |||
| 3674 | IsCooked = true; | |||
| 3675 | for (unsigned ArgIdx = 0; ArgIdx != ArgTys.size(); ++ArgIdx) { | |||
| 3676 | QualType ParamTy = FD->getParamDecl(ArgIdx)->getType(); | |||
| 3677 | if (!Context.hasSameUnqualifiedType(ArgTys[ArgIdx], ParamTy)) { | |||
| 3678 | IsCooked = false; | |||
| 3679 | break; | |||
| 3680 | } | |||
| 3681 | } | |||
| 3682 | } | |||
| 3683 | } | |||
| 3684 | if (FunctionTemplateDecl *FD = dyn_cast<FunctionTemplateDecl>(D)) { | |||
| 3685 | TemplateParameterList *Params = FD->getTemplateParameters(); | |||
| 3686 | if (Params->size() == 1) { | |||
| 3687 | IsTemplate = true; | |||
| 3688 | if (!Params->getParam(0)->isTemplateParameterPack() && !StringLit) { | |||
| 3689 | // Implied but not stated: user-defined integer and floating literals | |||
| 3690 | // only ever use numeric literal operator templates, not templates | |||
| 3691 | // taking a parameter of class type. | |||
| 3692 | F.erase(); | |||
| 3693 | continue; | |||
| 3694 | } | |||
| 3695 | ||||
| 3696 | // A string literal template is only considered if the string literal | |||
| 3697 | // is a well-formed template argument for the template parameter. | |||
| 3698 | if (StringLit) { | |||
| 3699 | SFINAETrap Trap(*this); | |||
| 3700 | SmallVector<TemplateArgument, 1> SugaredChecked, CanonicalChecked; | |||
| 3701 | TemplateArgumentLoc Arg(TemplateArgument(StringLit), StringLit); | |||
| 3702 | if (CheckTemplateArgument( | |||
| 3703 | Params->getParam(0), Arg, FD, R.getNameLoc(), R.getNameLoc(), | |||
| 3704 | 0, SugaredChecked, CanonicalChecked, CTAK_Specified) || | |||
| 3705 | Trap.hasErrorOccurred()) | |||
| 3706 | IsTemplate = false; | |||
| 3707 | } | |||
| 3708 | } else { | |||
| 3709 | IsStringTemplatePack = true; | |||
| 3710 | } | |||
| 3711 | } | |||
| 3712 | ||||
| 3713 | if (AllowTemplate && StringLit && IsTemplate) { | |||
| 3714 | FoundTemplate = true; | |||
| 3715 | AllowRaw = false; | |||
| 3716 | AllowCooked = false; | |||
| 3717 | AllowStringTemplatePack = false; | |||
| 3718 | if (FoundRaw || FoundCooked || FoundStringTemplatePack) { | |||
| 3719 | F.restart(); | |||
| 3720 | FoundRaw = FoundCooked = FoundStringTemplatePack = false; | |||
| 3721 | } | |||
| 3722 | } else if (AllowCooked && IsCooked) { | |||
| 3723 | FoundCooked = true; | |||
| 3724 | AllowRaw = false; | |||
| 3725 | AllowTemplate = StringLit; | |||
| 3726 | AllowStringTemplatePack = false; | |||
| 3727 | if (FoundRaw || FoundTemplate || FoundStringTemplatePack) { | |||
| 3728 | // Go through again and remove the raw and template decls we've | |||
| 3729 | // already found. | |||
| 3730 | F.restart(); | |||
| 3731 | FoundRaw = FoundTemplate = FoundStringTemplatePack = false; | |||
| 3732 | } | |||
| 3733 | } else if (AllowRaw && IsRaw) { | |||
| 3734 | FoundRaw = true; | |||
| 3735 | } else if (AllowTemplate && IsTemplate) { | |||
| 3736 | FoundTemplate = true; | |||
| 3737 | } else if (AllowStringTemplatePack && IsStringTemplatePack) { | |||
| 3738 | FoundStringTemplatePack = true; | |||
| 3739 | } else { | |||
| 3740 | F.erase(); | |||
| 3741 | } | |||
| 3742 | } | |||
| 3743 | ||||
| 3744 | F.done(); | |||
| 3745 | ||||
| 3746 | // Per C++20 [lex.ext]p5, we prefer the template form over the non-template | |||
| 3747 | // form for string literal operator templates. | |||
| 3748 | if (StringLit && FoundTemplate) | |||
| 3749 | return LOLR_Template; | |||
| 3750 | ||||
| 3751 | // C++11 [lex.ext]p3, p4: If S contains a literal operator with a matching | |||
| 3752 | // parameter type, that is used in preference to a raw literal operator | |||
| 3753 | // or literal operator template. | |||
| 3754 | if (FoundCooked) | |||
| 3755 | return LOLR_Cooked; | |||
| 3756 | ||||
| 3757 | // C++11 [lex.ext]p3, p4: S shall contain a raw literal operator or a literal | |||
| 3758 | // operator template, but not both. | |||
| 3759 | if (FoundRaw && FoundTemplate) { | |||
| 3760 | Diag(R.getNameLoc(), diag::err_ovl_ambiguous_call) << R.getLookupName(); | |||
| 3761 | for (const NamedDecl *D : R) | |||
| 3762 | NoteOverloadCandidate(D, D->getUnderlyingDecl()->getAsFunction()); | |||
| 3763 | return LOLR_Error; | |||
| 3764 | } | |||
| 3765 | ||||
| 3766 | if (FoundRaw) | |||
| 3767 | return LOLR_Raw; | |||
| 3768 | ||||
| 3769 | if (FoundTemplate) | |||
| 3770 | return LOLR_Template; | |||
| 3771 | ||||
| 3772 | if (FoundStringTemplatePack) | |||
| 3773 | return LOLR_StringTemplatePack; | |||
| 3774 | ||||
| 3775 | // Didn't find anything we could use. | |||
| 3776 | if (DiagnoseMissing) { | |||
| 3777 | Diag(R.getNameLoc(), diag::err_ovl_no_viable_literal_operator) | |||
| 3778 | << R.getLookupName() << (int)ArgTys.size() << ArgTys[0] | |||
| 3779 | << (ArgTys.size() == 2 ? ArgTys[1] : QualType()) << AllowRaw | |||
| 3780 | << (AllowTemplate || AllowStringTemplatePack); | |||
| 3781 | return LOLR_Error; | |||
| 3782 | } | |||
| 3783 | ||||
| 3784 | return LOLR_ErrorNoDiagnostic; | |||
| 3785 | } | |||
| 3786 | ||||
| 3787 | void ADLResult::insert(NamedDecl *New) { | |||
| 3788 | NamedDecl *&Old = Decls[cast<NamedDecl>(New->getCanonicalDecl())]; | |||
| 3789 | ||||
| 3790 | // If we haven't yet seen a decl for this key, or the last decl | |||
| 3791 | // was exactly this one, we're done. | |||
| 3792 | if (Old == nullptr || Old == New) { | |||
| 3793 | Old = New; | |||
| 3794 | return; | |||
| 3795 | } | |||
| 3796 | ||||
| 3797 | // Otherwise, decide which is a more recent redeclaration. | |||
| 3798 | FunctionDecl *OldFD = Old->getAsFunction(); | |||
| 3799 | FunctionDecl *NewFD = New->getAsFunction(); | |||
| 3800 | ||||
| 3801 | FunctionDecl *Cursor = NewFD; | |||
| 3802 | while (true) { | |||
| 3803 | Cursor = Cursor->getPreviousDecl(); | |||
| 3804 | ||||
| 3805 | // If we got to the end without finding OldFD, OldFD is the newer | |||
| 3806 | // declaration; leave things as they are. | |||
| 3807 | if (!Cursor) return; | |||
| 3808 | ||||
| 3809 | // If we do find OldFD, then NewFD is newer. | |||
| 3810 | if (Cursor == OldFD) break; | |||
| 3811 | ||||
| 3812 | // Otherwise, keep looking. | |||
| 3813 | } | |||
| 3814 | ||||
| 3815 | Old = New; | |||
| 3816 | } | |||
| 3817 | ||||
| 3818 | void Sema::ArgumentDependentLookup(DeclarationName Name, SourceLocation Loc, | |||
| 3819 | ArrayRef<Expr *> Args, ADLResult &Result) { | |||
| 3820 | // Find all of the associated namespaces and classes based on the | |||
| 3821 | // arguments we have. | |||
| 3822 | AssociatedNamespaceSet AssociatedNamespaces; | |||
| 3823 | AssociatedClassSet AssociatedClasses; | |||
| 3824 | FindAssociatedClassesAndNamespaces(Loc, Args, | |||
| 3825 | AssociatedNamespaces, | |||
| 3826 | AssociatedClasses); | |||
| 3827 | ||||
| 3828 | // C++ [basic.lookup.argdep]p3: | |||
| 3829 | // Let X be the lookup set produced by unqualified lookup (3.4.1) | |||
| 3830 | // and let Y be the lookup set produced by argument dependent | |||
| 3831 | // lookup (defined as follows). If X contains [...] then Y is | |||
| 3832 | // empty. Otherwise Y is the set of declarations found in the | |||
| 3833 | // namespaces associated with the argument types as described | |||
| 3834 | // below. The set of declarations found by the lookup of the name | |||
| 3835 | // is the union of X and Y. | |||
| 3836 | // | |||
| 3837 | // Here, we compute Y and add its members to the overloaded | |||
| 3838 | // candidate set. | |||
| 3839 | for (auto *NS : AssociatedNamespaces) { | |||
| 3840 | // When considering an associated namespace, the lookup is the | |||
| 3841 | // same as the lookup performed when the associated namespace is | |||
| 3842 | // used as a qualifier (3.4.3.2) except that: | |||
| 3843 | // | |||
| 3844 | // -- Any using-directives in the associated namespace are | |||
| 3845 | // ignored. | |||
| 3846 | // | |||
| 3847 | // -- Any namespace-scope friend functions declared in | |||
| 3848 | // associated classes are visible within their respective | |||
| 3849 | // namespaces even if they are not visible during an ordinary | |||
| 3850 | // lookup (11.4). | |||
| 3851 | // | |||
| 3852 | // C++20 [basic.lookup.argdep] p4.3 | |||
| 3853 | // -- are exported, are attached to a named module M, do not appear | |||
| 3854 | // in the translation unit containing the point of the lookup, and | |||
| 3855 | // have the same innermost enclosing non-inline namespace scope as | |||
| 3856 | // a declaration of an associated entity attached to M. | |||
| 3857 | DeclContext::lookup_result R = NS->lookup(Name); | |||
| 3858 | for (auto *D : R) { | |||
| 3859 | auto *Underlying = D; | |||
| 3860 | if (auto *USD = dyn_cast<UsingShadowDecl>(D)) | |||
| 3861 | Underlying = USD->getTargetDecl(); | |||
| 3862 | ||||
| 3863 | if (!isa<FunctionDecl>(Underlying) && | |||
| 3864 | !isa<FunctionTemplateDecl>(Underlying)) | |||
| 3865 | continue; | |||
| 3866 | ||||
| 3867 | // The declaration is visible to argument-dependent lookup if either | |||
| 3868 | // it's ordinarily visible or declared as a friend in an associated | |||
| 3869 | // class. | |||
| 3870 | bool Visible = false; | |||
| 3871 | for (D = D->getMostRecentDecl(); D; | |||
| 3872 | D = cast_or_null<NamedDecl>(D->getPreviousDecl())) { | |||
| 3873 | if (D->getIdentifierNamespace() & Decl::IDNS_Ordinary) { | |||
| 3874 | if (isVisible(D)) { | |||
| 3875 | Visible = true; | |||
| 3876 | break; | |||
| 3877 | } | |||
| 3878 | ||||
| 3879 | if (!getLangOpts().CPlusPlusModules) | |||
| 3880 | continue; | |||
| 3881 | ||||
| 3882 | if (D->isInExportDeclContext()) { | |||
| 3883 | Module *FM = D->getOwningModule(); | |||
| 3884 | // C++20 [basic.lookup.argdep] p4.3 .. are exported ... | |||
| 3885 | // exports are only valid in module purview and outside of any | |||
| 3886 | // PMF (although a PMF should not even be present in a module | |||
| 3887 | // with an import). | |||
| 3888 | assert(FM && FM->isModulePurview() && !FM->isPrivateModule() &&(static_cast <bool> (FM && FM->isModulePurview () && !FM->isPrivateModule() && "bad export context" ) ? void (0) : __assert_fail ("FM && FM->isModulePurview() && !FM->isPrivateModule() && \"bad export context\"" , "clang/lib/Sema/SemaLookup.cpp", 3889, __extension__ __PRETTY_FUNCTION__ )) | |||
| 3889 | "bad export context")(static_cast <bool> (FM && FM->isModulePurview () && !FM->isPrivateModule() && "bad export context" ) ? void (0) : __assert_fail ("FM && FM->isModulePurview() && !FM->isPrivateModule() && \"bad export context\"" , "clang/lib/Sema/SemaLookup.cpp", 3889, __extension__ __PRETTY_FUNCTION__ )); | |||
| 3890 | // .. are attached to a named module M, do not appear in the | |||
| 3891 | // translation unit containing the point of the lookup.. | |||
| 3892 | if (D->isInAnotherModuleUnit() && | |||
| 3893 | llvm::any_of(AssociatedClasses, [&](auto *E) { | |||
| 3894 | // ... and have the same innermost enclosing non-inline | |||
| 3895 | // namespace scope as a declaration of an associated entity | |||
| 3896 | // attached to M | |||
| 3897 | if (E->getOwningModule() != FM) | |||
| 3898 | return false; | |||
| 3899 | // TODO: maybe this could be cached when generating the | |||
| 3900 | // associated namespaces / entities. | |||
| 3901 | DeclContext *Ctx = E->getDeclContext(); | |||
| 3902 | while (!Ctx->isFileContext() || Ctx->isInlineNamespace()) | |||
| 3903 | Ctx = Ctx->getParent(); | |||
| 3904 | return Ctx == NS; | |||
| 3905 | })) { | |||
| 3906 | Visible = true; | |||
| 3907 | break; | |||
| 3908 | } | |||
| 3909 | } | |||
| 3910 | } else if (D->getFriendObjectKind()) { | |||
| 3911 | auto *RD = cast<CXXRecordDecl>(D->getLexicalDeclContext()); | |||
| 3912 | // [basic.lookup.argdep]p4: | |||
| 3913 | // Argument-dependent lookup finds all declarations of functions and | |||
| 3914 | // function templates that | |||
| 3915 | // - ... | |||
| 3916 | // - are declared as a friend ([class.friend]) of any class with a | |||
| 3917 | // reachable definition in the set of associated entities, | |||
| 3918 | // | |||
| 3919 | // FIXME: If there's a merged definition of D that is reachable, then | |||
| 3920 | // the friend declaration should be considered. | |||
| 3921 | if (AssociatedClasses.count(RD) && isReachable(D)) { | |||
| 3922 | Visible = true; | |||
| 3923 | break; | |||
| 3924 | } | |||
| 3925 | } | |||
| 3926 | } | |||
| 3927 | ||||
| 3928 | // FIXME: Preserve D as the FoundDecl. | |||
| 3929 | if (Visible) | |||
| 3930 | Result.insert(Underlying); | |||
| 3931 | } | |||
| 3932 | } | |||
| 3933 | } | |||
| 3934 | ||||
| 3935 | //---------------------------------------------------------------------------- | |||
| 3936 | // Search for all visible declarations. | |||
| 3937 | //---------------------------------------------------------------------------- | |||
| 3938 | VisibleDeclConsumer::~VisibleDeclConsumer() { } | |||
| 3939 | ||||
| 3940 | bool VisibleDeclConsumer::includeHiddenDecls() const { return false; } | |||
| 3941 | ||||
| 3942 | namespace { | |||
| 3943 | ||||
| 3944 | class ShadowContextRAII; | |||
| 3945 | ||||
| 3946 | class VisibleDeclsRecord { | |||
| 3947 | public: | |||
| 3948 | /// An entry in the shadow map, which is optimized to store a | |||
| 3949 | /// single declaration (the common case) but can also store a list | |||
| 3950 | /// of declarations. | |||
| 3951 | typedef llvm::TinyPtrVector<NamedDecl*> ShadowMapEntry; | |||
| 3952 | ||||
| 3953 | private: | |||
| 3954 | /// A mapping from declaration names to the declarations that have | |||
| 3955 | /// this name within a particular scope. | |||
| 3956 | typedef llvm::DenseMap<DeclarationName, ShadowMapEntry> ShadowMap; | |||
| 3957 | ||||
| 3958 | /// A list of shadow maps, which is used to model name hiding. | |||
| 3959 | std::list<ShadowMap> ShadowMaps; | |||
| 3960 | ||||
| 3961 | /// The declaration contexts we have already visited. | |||
| 3962 | llvm::SmallPtrSet<DeclContext *, 8> VisitedContexts; | |||
| 3963 | ||||
| 3964 | friend class ShadowContextRAII; | |||
| 3965 | ||||
| 3966 | public: | |||
| 3967 | /// Determine whether we have already visited this context | |||
| 3968 | /// (and, if not, note that we are going to visit that context now). | |||
| 3969 | bool visitedContext(DeclContext *Ctx) { | |||
| 3970 | return !VisitedContexts.insert(Ctx).second; | |||
| 3971 | } | |||
| 3972 | ||||
| 3973 | bool alreadyVisitedContext(DeclContext *Ctx) { | |||
| 3974 | return VisitedContexts.count(Ctx); | |||
| 3975 | } | |||
| 3976 | ||||
| 3977 | /// Determine whether the given declaration is hidden in the | |||
| 3978 | /// current scope. | |||
| 3979 | /// | |||
| 3980 | /// \returns the declaration that hides the given declaration, or | |||
| 3981 | /// NULL if no such declaration exists. | |||
| 3982 | NamedDecl *checkHidden(NamedDecl *ND); | |||
| 3983 | ||||
| 3984 | /// Add a declaration to the current shadow map. | |||
| 3985 | void add(NamedDecl *ND) { | |||
| 3986 | ShadowMaps.back()[ND->getDeclName()].push_back(ND); | |||
| 3987 | } | |||
| 3988 | }; | |||
| 3989 | ||||
| 3990 | /// RAII object that records when we've entered a shadow context. | |||
| 3991 | class ShadowContextRAII { | |||
| 3992 | VisibleDeclsRecord &Visible; | |||
| 3993 | ||||
| 3994 | typedef VisibleDeclsRecord::ShadowMap ShadowMap; | |||
| 3995 | ||||
| 3996 | public: | |||
| 3997 | ShadowContextRAII(VisibleDeclsRecord &Visible) : Visible(Visible) { | |||
| 3998 | Visible.ShadowMaps.emplace_back(); | |||
| 3999 | } | |||
| 4000 | ||||
| 4001 | ~ShadowContextRAII() { | |||
| 4002 | Visible.ShadowMaps.pop_back(); | |||
| 4003 | } | |||
| 4004 | }; | |||
| 4005 | ||||
| 4006 | } // end anonymous namespace | |||
| 4007 | ||||
| 4008 | NamedDecl *VisibleDeclsRecord::checkHidden(NamedDecl *ND) { | |||
| 4009 | unsigned IDNS = ND->getIdentifierNamespace(); | |||
| 4010 | std::list<ShadowMap>::reverse_iterator SM = ShadowMaps.rbegin(); | |||
| 4011 | for (std::list<ShadowMap>::reverse_iterator SMEnd = ShadowMaps.rend(); | |||
| 4012 | SM != SMEnd; ++SM) { | |||
| 4013 | ShadowMap::iterator Pos = SM->find(ND->getDeclName()); | |||
| 4014 | if (Pos == SM->end()) | |||
| 4015 | continue; | |||
| 4016 | ||||
| 4017 | for (auto *D : Pos->second) { | |||
| 4018 | // A tag declaration does not hide a non-tag declaration. | |||
| 4019 | if (D->hasTagIdentifierNamespace() && | |||
| 4020 | (IDNS & (Decl::IDNS_Member | Decl::IDNS_Ordinary | | |||
| 4021 | Decl::IDNS_ObjCProtocol))) | |||
| 4022 | continue; | |||
| 4023 | ||||
| 4024 | // Protocols are in distinct namespaces from everything else. | |||
| 4025 | if (((D->getIdentifierNamespace() & Decl::IDNS_ObjCProtocol) | |||
| 4026 | || (IDNS & Decl::IDNS_ObjCProtocol)) && | |||
| 4027 | D->getIdentifierNamespace() != IDNS) | |||
| 4028 | continue; | |||
| 4029 | ||||
| 4030 | // Functions and function templates in the same scope overload | |||
| 4031 | // rather than hide. FIXME: Look for hiding based on function | |||
| 4032 | // signatures! | |||
| 4033 | if (D->getUnderlyingDecl()->isFunctionOrFunctionTemplate() && | |||
| 4034 | ND->getUnderlyingDecl()->isFunctionOrFunctionTemplate() && | |||
| 4035 | SM == ShadowMaps.rbegin()) | |||
| 4036 | continue; | |||
| 4037 | ||||
| 4038 | // A shadow declaration that's created by a resolved using declaration | |||
| 4039 | // is not hidden by the same using declaration. | |||
| 4040 | if (isa<UsingShadowDecl>(ND) && isa<UsingDecl>(D) && | |||
| 4041 | cast<UsingShadowDecl>(ND)->getIntroducer() == D) | |||
| 4042 | continue; | |||
| 4043 | ||||
| 4044 | // We've found a declaration that hides this one. | |||
| 4045 | return D; | |||
| 4046 | } | |||
| 4047 | } | |||
| 4048 | ||||
| 4049 | return nullptr; | |||
| 4050 | } | |||
| 4051 | ||||
| 4052 | namespace { | |||
| 4053 | class LookupVisibleHelper { | |||
| 4054 | public: | |||
| 4055 | LookupVisibleHelper(VisibleDeclConsumer &Consumer, bool IncludeDependentBases, | |||
| 4056 | bool LoadExternal) | |||
| 4057 | : Consumer(Consumer), IncludeDependentBases(IncludeDependentBases), | |||
| 4058 | LoadExternal(LoadExternal) {} | |||
| 4059 | ||||
| 4060 | void lookupVisibleDecls(Sema &SemaRef, Scope *S, Sema::LookupNameKind Kind, | |||
| 4061 | bool IncludeGlobalScope) { | |||
| 4062 | // Determine the set of using directives available during | |||
| 4063 | // unqualified name lookup. | |||
| 4064 | Scope *Initial = S; | |||
| 4065 | UnqualUsingDirectiveSet UDirs(SemaRef); | |||
| 4066 | if (SemaRef.getLangOpts().CPlusPlus) { | |||
| 4067 | // Find the first namespace or translation-unit scope. | |||
| 4068 | while (S && !isNamespaceOrTranslationUnitScope(S)) | |||
| 4069 | S = S->getParent(); | |||
| 4070 | ||||
| 4071 | UDirs.visitScopeChain(Initial, S); | |||
| 4072 | } | |||
| 4073 | UDirs.done(); | |||
| 4074 | ||||
| 4075 | // Look for visible declarations. | |||
| 4076 | LookupResult Result(SemaRef, DeclarationName(), SourceLocation(), Kind); | |||
| 4077 | Result.setAllowHidden(Consumer.includeHiddenDecls()); | |||
| 4078 | if (!IncludeGlobalScope) | |||
| 4079 | Visited.visitedContext(SemaRef.getASTContext().getTranslationUnitDecl()); | |||
| 4080 | ShadowContextRAII Shadow(Visited); | |||
| 4081 | lookupInScope(Initial, Result, UDirs); | |||
| 4082 | } | |||
| 4083 | ||||
| 4084 | void lookupVisibleDecls(Sema &SemaRef, DeclContext *Ctx, | |||
| 4085 | Sema::LookupNameKind Kind, bool IncludeGlobalScope) { | |||
| 4086 | LookupResult Result(SemaRef, DeclarationName(), SourceLocation(), Kind); | |||
| 4087 | Result.setAllowHidden(Consumer.includeHiddenDecls()); | |||
| 4088 | if (!IncludeGlobalScope) | |||
| 4089 | Visited.visitedContext(SemaRef.getASTContext().getTranslationUnitDecl()); | |||
| 4090 | ||||
| 4091 | ShadowContextRAII Shadow(Visited); | |||
| 4092 | lookupInDeclContext(Ctx, Result, /*QualifiedNameLookup=*/true, | |||
| 4093 | /*InBaseClass=*/false); | |||
| 4094 | } | |||
| 4095 | ||||
| 4096 | private: | |||
| 4097 | void lookupInDeclContext(DeclContext *Ctx, LookupResult &Result, | |||
| 4098 | bool QualifiedNameLookup, bool InBaseClass) { | |||
| 4099 | if (!Ctx) | |||
| 4100 | return; | |||
| 4101 | ||||
| 4102 | // Make sure we don't visit the same context twice. | |||
| 4103 | if (Visited.visitedContext(Ctx->getPrimaryContext())) | |||
| 4104 | return; | |||
| 4105 | ||||
| 4106 | Consumer.EnteredContext(Ctx); | |||
| 4107 | ||||
| 4108 | // Outside C++, lookup results for the TU live on identifiers. | |||
| 4109 | if (isa<TranslationUnitDecl>(Ctx) && | |||
| 4110 | !Result.getSema().getLangOpts().CPlusPlus) { | |||
| 4111 | auto &S = Result.getSema(); | |||
| 4112 | auto &Idents = S.Context.Idents; | |||
| 4113 | ||||
| 4114 | // Ensure all external identifiers are in the identifier table. | |||
| 4115 | if (LoadExternal) | |||
| 4116 | if (IdentifierInfoLookup *External = | |||
| 4117 | Idents.getExternalIdentifierLookup()) { | |||
| 4118 | std::unique_ptr<IdentifierIterator> Iter(External->getIdentifiers()); | |||
| 4119 | for (StringRef Name = Iter->Next(); !Name.empty(); | |||
| 4120 | Name = Iter->Next()) | |||
| 4121 | Idents.get(Name); | |||
| 4122 | } | |||
| 4123 | ||||
| 4124 | // Walk all lookup results in the TU for each identifier. | |||
| 4125 | for (const auto &Ident : Idents) { | |||
| 4126 | for (auto I = S.IdResolver.begin(Ident.getValue()), | |||
| 4127 | E = S.IdResolver.end(); | |||
| 4128 | I != E; ++I) { | |||
| 4129 | if (S.IdResolver.isDeclInScope(*I, Ctx)) { | |||
| 4130 | if (NamedDecl *ND = Result.getAcceptableDecl(*I)) { | |||
| 4131 | Consumer.FoundDecl(ND, Visited.checkHidden(ND), Ctx, InBaseClass); | |||
| 4132 | Visited.add(ND); | |||
| 4133 | } | |||
| 4134 | } | |||
| 4135 | } | |||
| 4136 | } | |||
| 4137 | ||||
| 4138 | return; | |||
| 4139 | } | |||
| 4140 | ||||
| 4141 | if (CXXRecordDecl *Class = dyn_cast<CXXRecordDecl>(Ctx)) | |||
| 4142 | Result.getSema().ForceDeclarationOfImplicitMembers(Class); | |||
| 4143 | ||||
| 4144 | llvm::SmallVector<NamedDecl *, 4> DeclsToVisit; | |||
| 4145 | // We sometimes skip loading namespace-level results (they tend to be huge). | |||
| 4146 | bool Load = LoadExternal || | |||
| 4147 | !(isa<TranslationUnitDecl>(Ctx) || isa<NamespaceDecl>(Ctx)); | |||
| 4148 | // Enumerate all of the results in this context. | |||
| 4149 | for (DeclContextLookupResult R : | |||
| 4150 | Load ? Ctx->lookups() | |||
| 4151 | : Ctx->noload_lookups(/*PreserveInternalState=*/false)) { | |||
| 4152 | for (auto *D : R) { | |||
| 4153 | if (auto *ND = Result.getAcceptableDecl(D)) { | |||
| 4154 | // Rather than visit immediately, we put ND into a vector and visit | |||
| 4155 | // all decls, in order, outside of this loop. The reason is that | |||
| 4156 | // Consumer.FoundDecl() may invalidate the iterators used in the two | |||
| 4157 | // loops above. | |||
| 4158 | DeclsToVisit.push_back(ND); | |||
| 4159 | } | |||
| 4160 | } | |||
| 4161 | } | |||
| 4162 | ||||
| 4163 | for (auto *ND : DeclsToVisit) { | |||
| 4164 | Consumer.FoundDecl(ND, Visited.checkHidden(ND), Ctx, InBaseClass); | |||
| 4165 | Visited.add(ND); | |||
| 4166 | } | |||
| 4167 | DeclsToVisit.clear(); | |||
| 4168 | ||||
| 4169 | // Traverse using directives for qualified name lookup. | |||
| 4170 | if (QualifiedNameLookup) { | |||
| 4171 | ShadowContextRAII Shadow(Visited); | |||
| 4172 | for (auto *I : Ctx->using_directives()) { | |||
| 4173 | if (!Result.getSema().isVisible(I)) | |||
| 4174 | continue; | |||
| 4175 | lookupInDeclContext(I->getNominatedNamespace(), Result, | |||
| 4176 | QualifiedNameLookup, InBaseClass); | |||
| 4177 | } | |||
| 4178 | } | |||
| 4179 | ||||
| 4180 | // Traverse the contexts of inherited C++ classes. | |||
| 4181 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx)) { | |||
| 4182 | if (!Record->hasDefinition()) | |||
| 4183 | return; | |||
| 4184 | ||||
| 4185 | for (const auto &B : Record->bases()) { | |||
| 4186 | QualType BaseType = B.getType(); | |||
| 4187 | ||||
| 4188 | RecordDecl *RD; | |||
| 4189 | if (BaseType->isDependentType()) { | |||
| 4190 | if (!IncludeDependentBases) { | |||
| 4191 | // Don't look into dependent bases, because name lookup can't look | |||
| 4192 | // there anyway. | |||
| 4193 | continue; | |||
| 4194 | } | |||
| 4195 | const auto *TST = BaseType->getAs<TemplateSpecializationType>(); | |||
| 4196 | if (!TST) | |||
| 4197 | continue; | |||
| 4198 | TemplateName TN = TST->getTemplateName(); | |||
| 4199 | const auto *TD = | |||
| 4200 | dyn_cast_or_null<ClassTemplateDecl>(TN.getAsTemplateDecl()); | |||
| 4201 | if (!TD) | |||
| 4202 | continue; | |||
| 4203 | RD = TD->getTemplatedDecl(); | |||
| 4204 | } else { | |||
| 4205 | const auto *Record = BaseType->getAs<RecordType>(); | |||
| 4206 | if (!Record) | |||
| 4207 | continue; | |||
| 4208 | RD = Record->getDecl(); | |||
| 4209 | } | |||
| 4210 | ||||
| 4211 | // FIXME: It would be nice to be able to determine whether referencing | |||
| 4212 | // a particular member would be ambiguous. For example, given | |||
| 4213 | // | |||
| 4214 | // struct A { int member; }; | |||
| 4215 | // struct B { int member; }; | |||
| 4216 | // struct C : A, B { }; | |||
| 4217 | // | |||
| 4218 | // void f(C *c) { c->### } | |||
| 4219 | // | |||
| 4220 | // accessing 'member' would result in an ambiguity. However, we | |||
| 4221 | // could be smart enough to qualify the member with the base | |||
| 4222 | // class, e.g., | |||
| 4223 | // | |||
| 4224 | // c->B::member | |||
| 4225 | // | |||
| 4226 | // or | |||
| 4227 | // | |||
| 4228 | // c->A::member | |||
| 4229 | ||||
| 4230 | // Find results in this base class (and its bases). | |||
| 4231 | ShadowContextRAII Shadow(Visited); | |||
| 4232 | lookupInDeclContext(RD, Result, QualifiedNameLookup, | |||
| 4233 | /*InBaseClass=*/true); | |||
| 4234 | } | |||
| 4235 | } | |||
| 4236 | ||||
| 4237 | // Traverse the contexts of Objective-C classes. | |||
| 4238 | if (ObjCInterfaceDecl *IFace = dyn_cast<ObjCInterfaceDecl>(Ctx)) { | |||
| 4239 | // Traverse categories. | |||
| 4240 | for (auto *Cat : IFace->visible_categories()) { | |||
| 4241 | ShadowContextRAII Shadow(Visited); | |||
| 4242 | lookupInDeclContext(Cat, Result, QualifiedNameLookup, | |||
| 4243 | /*InBaseClass=*/false); | |||
| 4244 | } | |||
| 4245 | ||||
| 4246 | // Traverse protocols. | |||
| 4247 | for (auto *I : IFace->all_referenced_protocols()) { | |||
| 4248 | ShadowContextRAII Shadow(Visited); | |||
| 4249 | lookupInDeclContext(I, Result, QualifiedNameLookup, | |||
| 4250 | /*InBaseClass=*/false); | |||
| 4251 | } | |||
| 4252 | ||||
| 4253 | // Traverse the superclass. | |||
| 4254 | if (IFace->getSuperClass()) { | |||
| 4255 | ShadowContextRAII Shadow(Visited); | |||
| 4256 | lookupInDeclContext(IFace->getSuperClass(), Result, QualifiedNameLookup, | |||
| 4257 | /*InBaseClass=*/true); | |||
| 4258 | } | |||
| 4259 | ||||
| 4260 | // If there is an implementation, traverse it. We do this to find | |||
| 4261 | // synthesized ivars. | |||
| 4262 | if (IFace->getImplementation()) { | |||
| 4263 | ShadowContextRAII Shadow(Visited); | |||
| 4264 | lookupInDeclContext(IFace->getImplementation(), Result, | |||
| 4265 | QualifiedNameLookup, InBaseClass); | |||
| 4266 | } | |||
| 4267 | } else if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Ctx)) { | |||
| 4268 | for (auto *I : Protocol->protocols()) { | |||
| 4269 | ShadowContextRAII Shadow(Visited); | |||
| 4270 | lookupInDeclContext(I, Result, QualifiedNameLookup, | |||
| 4271 | /*InBaseClass=*/false); | |||
| 4272 | } | |||
| 4273 | } else if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Ctx)) { | |||
| 4274 | for (auto *I : Category->protocols()) { | |||
| 4275 | ShadowContextRAII Shadow(Visited); | |||
| 4276 | lookupInDeclContext(I, Result, QualifiedNameLookup, | |||
| 4277 | /*InBaseClass=*/false); | |||
| 4278 | } | |||
| 4279 | ||||
| 4280 | // If there is an implementation, traverse it. | |||
| 4281 | if (Category->getImplementation()) { | |||
| 4282 | ShadowContextRAII Shadow(Visited); | |||
| 4283 | lookupInDeclContext(Category->getImplementation(), Result, | |||
| 4284 | QualifiedNameLookup, /*InBaseClass=*/true); | |||
| 4285 | } | |||
| 4286 | } | |||
| 4287 | } | |||
| 4288 | ||||
| 4289 | void lookupInScope(Scope *S, LookupResult &Result, | |||
| 4290 | UnqualUsingDirectiveSet &UDirs) { | |||
| 4291 | // No clients run in this mode and it's not supported. Please add tests and | |||
| 4292 | // remove the assertion if you start relying on it. | |||
| 4293 | assert(!IncludeDependentBases && "Unsupported flag for lookupInScope")(static_cast <bool> (!IncludeDependentBases && "Unsupported flag for lookupInScope" ) ? void (0) : __assert_fail ("!IncludeDependentBases && \"Unsupported flag for lookupInScope\"" , "clang/lib/Sema/SemaLookup.cpp", 4293, __extension__ __PRETTY_FUNCTION__ )); | |||
| 4294 | ||||
| 4295 | if (!S) | |||
| 4296 | return; | |||
| 4297 | ||||
| 4298 | if (!S->getEntity() || | |||
| 4299 | (!S->getParent() && !Visited.alreadyVisitedContext(S->getEntity())) || | |||
| 4300 | (S->getEntity())->isFunctionOrMethod()) { | |||
| 4301 | FindLocalExternScope FindLocals(Result); | |||
| 4302 | // Walk through the declarations in this Scope. The consumer might add new | |||
| 4303 | // decls to the scope as part of deserialization, so make a copy first. | |||
| 4304 | SmallVector<Decl *, 8> ScopeDecls(S->decls().begin(), S->decls().end()); | |||
| 4305 | for (Decl *D : ScopeDecls) { | |||
| 4306 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) | |||
| 4307 | if ((ND = Result.getAcceptableDecl(ND))) { | |||
| 4308 | Consumer.FoundDecl(ND, Visited.checkHidden(ND), nullptr, false); | |||
| 4309 | Visited.add(ND); | |||
| 4310 | } | |||
| 4311 | } | |||
| 4312 | } | |||
| 4313 | ||||
| 4314 | DeclContext *Entity = S->getLookupEntity(); | |||
| 4315 | if (Entity) { | |||
| 4316 | // Look into this scope's declaration context, along with any of its | |||
| 4317 | // parent lookup contexts (e.g., enclosing classes), up to the point | |||
| 4318 | // where we hit the context stored in the next outer scope. | |||
| 4319 | DeclContext *OuterCtx = findOuterContext(S); | |||
| 4320 | ||||
| 4321 | for (DeclContext *Ctx = Entity; Ctx && !Ctx->Equals(OuterCtx); | |||
| 4322 | Ctx = Ctx->getLookupParent()) { | |||
| 4323 | if (ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(Ctx)) { | |||
| 4324 | if (Method->isInstanceMethod()) { | |||
| 4325 | // For instance methods, look for ivars in the method's interface. | |||
| 4326 | LookupResult IvarResult(Result.getSema(), Result.getLookupName(), | |||
| 4327 | Result.getNameLoc(), | |||
| 4328 | Sema::LookupMemberName); | |||
| 4329 | if (ObjCInterfaceDecl *IFace = Method->getClassInterface()) { | |||
| 4330 | lookupInDeclContext(IFace, IvarResult, | |||
| 4331 | /*QualifiedNameLookup=*/false, | |||
| 4332 | /*InBaseClass=*/false); | |||
| 4333 | } | |||
| 4334 | } | |||
| 4335 | ||||
| 4336 | // We've already performed all of the name lookup that we need | |||
| 4337 | // to for Objective-C methods; the next context will be the | |||
| 4338 | // outer scope. | |||
| 4339 | break; | |||
| 4340 | } | |||
| 4341 | ||||
| 4342 | if (Ctx->isFunctionOrMethod()) | |||
| 4343 | continue; | |||
| 4344 | ||||
| 4345 | lookupInDeclContext(Ctx, Result, /*QualifiedNameLookup=*/false, | |||
| 4346 | /*InBaseClass=*/false); | |||
| 4347 | } | |||
| 4348 | } else if (!S->getParent()) { | |||
| 4349 | // Look into the translation unit scope. We walk through the translation | |||
| 4350 | // unit's declaration context, because the Scope itself won't have all of | |||
| 4351 | // the declarations if we loaded a precompiled header. | |||
| 4352 | // FIXME: We would like the translation unit's Scope object to point to | |||
| 4353 | // the translation unit, so we don't need this special "if" branch. | |||
| 4354 | // However, doing so would force the normal C++ name-lookup code to look | |||
| 4355 | // into the translation unit decl when the IdentifierInfo chains would | |||
| 4356 | // suffice. Once we fix that problem (which is part of a more general | |||
| 4357 | // "don't look in DeclContexts unless we have to" optimization), we can | |||
| 4358 | // eliminate this. | |||
| 4359 | Entity = Result.getSema().Context.getTranslationUnitDecl(); | |||
| 4360 | lookupInDeclContext(Entity, Result, /*QualifiedNameLookup=*/false, | |||
| 4361 | /*InBaseClass=*/false); | |||
| 4362 | } | |||
| 4363 | ||||
| 4364 | if (Entity) { | |||
| 4365 | // Lookup visible declarations in any namespaces found by using | |||
| 4366 | // directives. | |||
| 4367 | for (const UnqualUsingEntry &UUE : UDirs.getNamespacesFor(Entity)) | |||
| 4368 | lookupInDeclContext( | |||
| 4369 | const_cast<DeclContext *>(UUE.getNominatedNamespace()), Result, | |||
| 4370 | /*QualifiedNameLookup=*/false, | |||
| 4371 | /*InBaseClass=*/false); | |||
| 4372 | } | |||
| 4373 | ||||
| 4374 | // Lookup names in the parent scope. | |||
| 4375 | ShadowContextRAII Shadow(Visited); | |||
| 4376 | lookupInScope(S->getParent(), Result, UDirs); | |||
| 4377 | } | |||
| 4378 | ||||
| 4379 | private: | |||
| 4380 | VisibleDeclsRecord Visited; | |||
| 4381 | VisibleDeclConsumer &Consumer; | |||
| 4382 | bool IncludeDependentBases; | |||
| 4383 | bool LoadExternal; | |||
| 4384 | }; | |||
| 4385 | } // namespace | |||
| 4386 | ||||
| 4387 | void Sema::LookupVisibleDecls(Scope *S, LookupNameKind Kind, | |||
| 4388 | VisibleDeclConsumer &Consumer, | |||
| 4389 | bool IncludeGlobalScope, bool LoadExternal) { | |||
| 4390 | LookupVisibleHelper H(Consumer, /*IncludeDependentBases=*/false, | |||
| 4391 | LoadExternal); | |||
| 4392 | H.lookupVisibleDecls(*this, S, Kind, IncludeGlobalScope); | |||
| 4393 | } | |||
| 4394 | ||||
| 4395 | void Sema::LookupVisibleDecls(DeclContext *Ctx, LookupNameKind Kind, | |||
| 4396 | VisibleDeclConsumer &Consumer, | |||
| 4397 | bool IncludeGlobalScope, | |||
| 4398 | bool IncludeDependentBases, bool LoadExternal) { | |||
| 4399 | LookupVisibleHelper H(Consumer, IncludeDependentBases, LoadExternal); | |||
| 4400 | H.lookupVisibleDecls(*this, Ctx, Kind, IncludeGlobalScope); | |||
| 4401 | } | |||
| 4402 | ||||
| 4403 | /// LookupOrCreateLabel - Do a name lookup of a label with the specified name. | |||
| 4404 | /// If GnuLabelLoc is a valid source location, then this is a definition | |||
| 4405 | /// of an __label__ label name, otherwise it is a normal label definition | |||
| 4406 | /// or use. | |||
| 4407 | LabelDecl *Sema::LookupOrCreateLabel(IdentifierInfo *II, SourceLocation Loc, | |||
| 4408 | SourceLocation GnuLabelLoc) { | |||
| 4409 | // Do a lookup to see if we have a label with this name already. | |||
| 4410 | NamedDecl *Res = nullptr; | |||
| 4411 | ||||
| 4412 | if (GnuLabelLoc.isValid()) { | |||
| 4413 | // Local label definitions always shadow existing labels. | |||
| 4414 | Res = LabelDecl::Create(Context, CurContext, Loc, II, GnuLabelLoc); | |||
| 4415 | Scope *S = CurScope; | |||
| 4416 | PushOnScopeChains(Res, S, true); | |||
| 4417 | return cast<LabelDecl>(Res); | |||
| 4418 | } | |||
| 4419 | ||||
| 4420 | // Not a GNU local label. | |||
| 4421 | Res = LookupSingleName(CurScope, II, Loc, LookupLabel, NotForRedeclaration); | |||
| 4422 | // If we found a label, check to see if it is in the same context as us. | |||
| 4423 | // When in a Block, we don't want to reuse a label in an enclosing function. | |||
| 4424 | if (Res && Res->getDeclContext() != CurContext) | |||
| 4425 | Res = nullptr; | |||
| 4426 | if (!Res) { | |||
| 4427 | // If not forward referenced or defined already, create the backing decl. | |||
| 4428 | Res = LabelDecl::Create(Context, CurContext, Loc, II); | |||
| 4429 | Scope *S = CurScope->getFnParent(); | |||
| 4430 | assert(S && "Not in a function?")(static_cast <bool> (S && "Not in a function?") ? void (0) : __assert_fail ("S && \"Not in a function?\"" , "clang/lib/Sema/SemaLookup.cpp", 4430, __extension__ __PRETTY_FUNCTION__ )); | |||
| 4431 | PushOnScopeChains(Res, S, true); | |||
| 4432 | } | |||
| 4433 | return cast<LabelDecl>(Res); | |||
| 4434 | } | |||
| 4435 | ||||
| 4436 | //===----------------------------------------------------------------------===// | |||
| 4437 | // Typo correction | |||
| 4438 | //===----------------------------------------------------------------------===// | |||
| 4439 | ||||
| 4440 | static bool isCandidateViable(CorrectionCandidateCallback &CCC, | |||
| 4441 | TypoCorrection &Candidate) { | |||
| 4442 | Candidate.setCallbackDistance(CCC.RankCandidate(Candidate)); | |||
| 4443 | return Candidate.getEditDistance(false) != TypoCorrection::InvalidDistance; | |||
| 4444 | } | |||
| 4445 | ||||
| 4446 | static void LookupPotentialTypoResult(Sema &SemaRef, | |||
| 4447 | LookupResult &Res, | |||
| 4448 | IdentifierInfo *Name, | |||
| 4449 | Scope *S, CXXScopeSpec *SS, | |||
| 4450 | DeclContext *MemberContext, | |||
| 4451 | bool EnteringContext, | |||
| 4452 | bool isObjCIvarLookup, | |||
| 4453 | bool FindHidden); | |||
| 4454 | ||||
| 4455 | /// Check whether the declarations found for a typo correction are | |||
| 4456 | /// visible. Set the correction's RequiresImport flag to true if none of the | |||
| 4457 | /// declarations are visible, false otherwise. | |||
| 4458 | static void checkCorrectionVisibility(Sema &SemaRef, TypoCorrection &TC) { | |||
| 4459 | TypoCorrection::decl_iterator DI = TC.begin(), DE = TC.end(); | |||
| 4460 | ||||
| 4461 | for (/**/; DI != DE; ++DI) | |||
| 4462 | if (!LookupResult::isVisible(SemaRef, *DI)) | |||
| 4463 | break; | |||
| 4464 | // No filtering needed if all decls are visible. | |||
| 4465 | if (DI == DE) { | |||
| 4466 | TC.setRequiresImport(false); | |||
| 4467 | return; | |||
| 4468 | } | |||
| 4469 | ||||
| 4470 | llvm::SmallVector<NamedDecl*, 4> NewDecls(TC.begin(), DI); | |||
| 4471 | bool AnyVisibleDecls = !NewDecls.empty(); | |||
| 4472 | ||||
| 4473 | for (/**/; DI != DE; ++DI) { | |||
| 4474 | if (LookupResult::isVisible(SemaRef, *DI)) { | |||
| 4475 | if (!AnyVisibleDecls) { | |||
| 4476 | // Found a visible decl, discard all hidden ones. | |||
| 4477 | AnyVisibleDecls = true; | |||
| 4478 | NewDecls.clear(); | |||
| 4479 | } | |||
| 4480 | NewDecls.push_back(*DI); | |||
| 4481 | } else if (!AnyVisibleDecls && !(*DI)->isModulePrivate()) | |||
| 4482 | NewDecls.push_back(*DI); | |||
| 4483 | } | |||
| 4484 | ||||
| 4485 | if (NewDecls.empty()) | |||
| 4486 | TC = TypoCorrection(); | |||
| 4487 | else { | |||
| 4488 | TC.setCorrectionDecls(NewDecls); | |||
| 4489 | TC.setRequiresImport(!AnyVisibleDecls); | |||
| 4490 | } | |||
| 4491 | } | |||
| 4492 | ||||
| 4493 | // Fill the supplied vector with the IdentifierInfo pointers for each piece of | |||
| 4494 | // the given NestedNameSpecifier (i.e. given a NestedNameSpecifier "foo::bar::", | |||
| 4495 | // fill the vector with the IdentifierInfo pointers for "foo" and "bar"). | |||
| 4496 | static void getNestedNameSpecifierIdentifiers( | |||
| 4497 | NestedNameSpecifier *NNS, | |||
| 4498 | SmallVectorImpl<const IdentifierInfo*> &Identifiers) { | |||
| 4499 | if (NestedNameSpecifier *Prefix = NNS->getPrefix()) | |||
| 4500 | getNestedNameSpecifierIdentifiers(Prefix, Identifiers); | |||
| 4501 | else | |||
| 4502 | Identifiers.clear(); | |||
| 4503 | ||||
| 4504 | const IdentifierInfo *II = nullptr; | |||
| 4505 | ||||
| 4506 | switch (NNS->getKind()) { | |||
| 4507 | case NestedNameSpecifier::Identifier: | |||
| 4508 | II = NNS->getAsIdentifier(); | |||
| 4509 | break; | |||
| 4510 | ||||
| 4511 | case NestedNameSpecifier::Namespace: | |||
| 4512 | if (NNS->getAsNamespace()->isAnonymousNamespace()) | |||
| 4513 | return; | |||
| 4514 | II = NNS->getAsNamespace()->getIdentifier(); | |||
| 4515 | break; | |||
| 4516 | ||||
| 4517 | case NestedNameSpecifier::NamespaceAlias: | |||
| 4518 | II = NNS->getAsNamespaceAlias()->getIdentifier(); | |||
| 4519 | break; | |||
| 4520 | ||||
| 4521 | case NestedNameSpecifier::TypeSpecWithTemplate: | |||
| 4522 | case NestedNameSpecifier::TypeSpec: | |||
| 4523 | II = QualType(NNS->getAsType(), 0).getBaseTypeIdentifier(); | |||
| 4524 | break; | |||
| 4525 | ||||
| 4526 | case NestedNameSpecifier::Global: | |||
| 4527 | case NestedNameSpecifier::Super: | |||
| 4528 | return; | |||
| 4529 | } | |||
| 4530 | ||||
| 4531 | if (II) | |||
| 4532 | Identifiers.push_back(II); | |||
| 4533 | } | |||
| 4534 | ||||
| 4535 | void TypoCorrectionConsumer::FoundDecl(NamedDecl *ND, NamedDecl *Hiding, | |||
| 4536 | DeclContext *Ctx, bool InBaseClass) { | |||
| 4537 | // Don't consider hidden names for typo correction. | |||
| 4538 | if (Hiding) | |||
| 4539 | return; | |||
| 4540 | ||||
| 4541 | // Only consider entities with identifiers for names, ignoring | |||
| 4542 | // special names (constructors, overloaded operators, selectors, | |||
| 4543 | // etc.). | |||
| 4544 | IdentifierInfo *Name = ND->getIdentifier(); | |||
| 4545 | if (!Name) | |||
| 4546 | return; | |||
| 4547 | ||||
| 4548 | // Only consider visible declarations and declarations from modules with | |||
| 4549 | // names that exactly match. | |||
| 4550 | if (!LookupResult::isVisible(SemaRef, ND) && Name != Typo) | |||
| 4551 | return; | |||
| 4552 | ||||
| 4553 | FoundName(Name->getName()); | |||
| 4554 | } | |||
| 4555 | ||||
| 4556 | void TypoCorrectionConsumer::FoundName(StringRef Name) { | |||
| 4557 | // Compute the edit distance between the typo and the name of this | |||
| 4558 | // entity, and add the identifier to the list of results. | |||
| 4559 | addName(Name, nullptr); | |||
| 4560 | } | |||
| 4561 | ||||
| 4562 | void TypoCorrectionConsumer::addKeywordResult(StringRef Keyword) { | |||
| 4563 | // Compute the edit distance between the typo and this keyword, | |||
| 4564 | // and add the keyword to the list of results. | |||
| 4565 | addName(Keyword, nullptr, nullptr, true); | |||
| 4566 | } | |||
| 4567 | ||||
| 4568 | void TypoCorrectionConsumer::addName(StringRef Name, NamedDecl *ND, | |||
| 4569 | NestedNameSpecifier *NNS, bool isKeyword) { | |||
| 4570 | // Use a simple length-based heuristic to determine the minimum possible | |||
| 4571 | // edit distance. If the minimum isn't good enough, bail out early. | |||
| 4572 | StringRef TypoStr = Typo->getName(); | |||
| 4573 | unsigned MinED = abs((int)Name.size() - (int)TypoStr.size()); | |||
| 4574 | if (MinED && TypoStr.size() / MinED < 3) | |||
| 4575 | return; | |||
| 4576 | ||||
| 4577 | // Compute an upper bound on the allowable edit distance, so that the | |||
| 4578 | // edit-distance algorithm can short-circuit. | |||
| 4579 | unsigned UpperBound = (TypoStr.size() + 2) / 3; | |||
| 4580 | unsigned ED = TypoStr.edit_distance(Name, true, UpperBound); | |||
| 4581 | if (ED > UpperBound) return; | |||
| 4582 | ||||
| 4583 | TypoCorrection TC(&SemaRef.Context.Idents.get(Name), ND, NNS, ED); | |||
| 4584 | if (isKeyword) TC.makeKeyword(); | |||
| 4585 | TC.setCorrectionRange(nullptr, Result.getLookupNameInfo()); | |||
| 4586 | addCorrection(TC); | |||
| 4587 | } | |||
| 4588 | ||||
| 4589 | static const unsigned MaxTypoDistanceResultSets = 5; | |||
| 4590 | ||||
| 4591 | void TypoCorrectionConsumer::addCorrection(TypoCorrection Correction) { | |||
| 4592 | StringRef TypoStr = Typo->getName(); | |||
| 4593 | StringRef Name = Correction.getCorrectionAsIdentifierInfo()->getName(); | |||
| 4594 | ||||
| 4595 | // For very short typos, ignore potential corrections that have a different | |||
| 4596 | // base identifier from the typo or which have a normalized edit distance | |||
| 4597 | // longer than the typo itself. | |||
| 4598 | if (TypoStr.size() < 3 && | |||
| 4599 | (Name != TypoStr || Correction.getEditDistance(true) > TypoStr.size())) | |||
| 4600 | return; | |||
| 4601 | ||||
| 4602 | // If the correction is resolved but is not viable, ignore it. | |||
| 4603 | if (Correction.isResolved()) { | |||
| 4604 | checkCorrectionVisibility(SemaRef, Correction); | |||
| 4605 | if (!Correction || !isCandidateViable(*CorrectionValidator, Correction)) | |||
| 4606 | return; | |||
| 4607 | } | |||
| 4608 | ||||
| 4609 | TypoResultList &CList = | |||
| 4610 | CorrectionResults[Correction.getEditDistance(false)][Name]; | |||
| 4611 | ||||
| 4612 | if (!CList.empty() && !CList.back().isResolved()) | |||
| 4613 | CList.pop_back(); | |||
| 4614 | if (NamedDecl *NewND = Correction.getCorrectionDecl()) { | |||
| 4615 | auto RI = llvm::find_if(CList, [NewND](const TypoCorrection &TypoCorr) { | |||
| 4616 | return TypoCorr.getCorrectionDecl() == NewND; | |||
| 4617 | }); | |||
| 4618 | if (RI != CList.end()) { | |||
| 4619 | // The Correction refers to a decl already in the list. No insertion is | |||
| 4620 | // necessary and all further cases will return. | |||
| 4621 | ||||
| 4622 | auto IsDeprecated = [](Decl *D) { | |||
| 4623 | while (D) { | |||
| 4624 | if (D->isDeprecated()) | |||
| 4625 | return true; | |||
| 4626 | D = llvm::dyn_cast_or_null<NamespaceDecl>(D->getDeclContext()); | |||
| 4627 | } | |||
| 4628 | return false; | |||
| 4629 | }; | |||
| 4630 | ||||
| 4631 | // Prefer non deprecated Corrections over deprecated and only then | |||
| 4632 | // sort using an alphabetical order. | |||
| 4633 | std::pair<bool, std::string> NewKey = { | |||
| 4634 | IsDeprecated(Correction.getFoundDecl()), | |||
| 4635 | Correction.getAsString(SemaRef.getLangOpts())}; | |||
| 4636 | ||||
| 4637 | std::pair<bool, std::string> PrevKey = { | |||
| 4638 | IsDeprecated(RI->getFoundDecl()), | |||
| 4639 | RI->getAsString(SemaRef.getLangOpts())}; | |||
| 4640 | ||||
| 4641 | if (NewKey < PrevKey) | |||
| 4642 | *RI = Correction; | |||
| 4643 | return; | |||
| 4644 | } | |||
| 4645 | } | |||
| 4646 | if (CList.empty() || Correction.isResolved()) | |||
| 4647 | CList.push_back(Correction); | |||
| 4648 | ||||
| 4649 | while (CorrectionResults.size() > MaxTypoDistanceResultSets) | |||
| 4650 | CorrectionResults.erase(std::prev(CorrectionResults.end())); | |||
| 4651 | } | |||
| 4652 | ||||
| 4653 | void TypoCorrectionConsumer::addNamespaces( | |||
| 4654 | const llvm::MapVector<NamespaceDecl *, bool> &KnownNamespaces) { | |||
| 4655 | SearchNamespaces = true; | |||
| 4656 | ||||
| 4657 | for (auto KNPair : KnownNamespaces) | |||
| 4658 | Namespaces.addNameSpecifier(KNPair.first); | |||
| ||||
| 4659 | ||||
| 4660 | bool SSIsTemplate = false; | |||
| 4661 | if (NestedNameSpecifier *NNS = | |||
| 4662 | (SS && SS->isValid()) ? SS->getScopeRep() : nullptr) { | |||
| 4663 | if (const Type *T = NNS->getAsType()) | |||
| 4664 | SSIsTemplate = T->getTypeClass() == Type::TemplateSpecialization; | |||
| 4665 | } | |||
| 4666 | // Do not transform this into an iterator-based loop. The loop body can | |||
| 4667 | // trigger the creation of further types (through lazy deserialization) and | |||
| 4668 | // invalid iterators into this list. | |||
| 4669 | auto &Types = SemaRef.getASTContext().getTypes(); | |||
| 4670 | for (unsigned I = 0; I != Types.size(); ++I) { | |||
| 4671 | const auto *TI = Types[I]; | |||
| 4672 | if (CXXRecordDecl *CD = TI->getAsCXXRecordDecl()) { | |||
| 4673 | CD = CD->getCanonicalDecl(); | |||
| 4674 | if (!CD->isDependentType() && !CD->isAnonymousStructOrUnion() && | |||
| 4675 | !CD->isUnion() && CD->getIdentifier() && | |||
| 4676 | (SSIsTemplate || !isa<ClassTemplateSpecializationDecl>(CD)) && | |||
| 4677 | (CD->isBeingDefined() || CD->isCompleteDefinition())) | |||
| 4678 | Namespaces.addNameSpecifier(CD); | |||
| 4679 | } | |||
| 4680 | } | |||
| 4681 | } | |||
| 4682 | ||||
| 4683 | const TypoCorrection &TypoCorrectionConsumer::getNextCorrection() { | |||
| 4684 | if (++CurrentTCIndex < ValidatedCorrections.size()) | |||
| 4685 | return ValidatedCorrections[CurrentTCIndex]; | |||
| 4686 | ||||
| 4687 | CurrentTCIndex = ValidatedCorrections.size(); | |||
| 4688 | while (!CorrectionResults.empty()) { | |||
| 4689 | auto DI = CorrectionResults.begin(); | |||
| 4690 | if (DI->second.empty()) { | |||
| 4691 | CorrectionResults.erase(DI); | |||
| 4692 | continue; | |||
| 4693 | } | |||
| 4694 | ||||
| 4695 | auto RI = DI->second.begin(); | |||
| 4696 | if (RI->second.empty()) { | |||
| 4697 | DI->second.erase(RI); | |||
| 4698 | performQualifiedLookups(); | |||
| 4699 | continue; | |||
| 4700 | } | |||
| 4701 | ||||
| 4702 | TypoCorrection TC = RI->second.pop_back_val(); | |||
| 4703 | if (TC.isResolved() || TC.requiresImport() || resolveCorrection(TC)) { | |||
| 4704 | ValidatedCorrections.push_back(TC); | |||
| 4705 | return ValidatedCorrections[CurrentTCIndex]; | |||
| 4706 | } | |||
| 4707 | } | |||
| 4708 | return ValidatedCorrections[0]; // The empty correction. | |||
| 4709 | } | |||
| 4710 | ||||
| 4711 | bool TypoCorrectionConsumer::resolveCorrection(TypoCorrection &Candidate) { | |||
| 4712 | IdentifierInfo *Name = Candidate.getCorrectionAsIdentifierInfo(); | |||
| 4713 | DeclContext *TempMemberContext = MemberContext; | |||
| 4714 | CXXScopeSpec *TempSS = SS.get(); | |||
| 4715 | retry_lookup: | |||
| 4716 | LookupPotentialTypoResult(SemaRef, Result, Name, S, TempSS, TempMemberContext, | |||
| 4717 | EnteringContext, | |||
| 4718 | CorrectionValidator->IsObjCIvarLookup, | |||
| 4719 | Name == Typo && !Candidate.WillReplaceSpecifier()); | |||
| 4720 | switch (Result.getResultKind()) { | |||
| 4721 | case LookupResult::NotFound: | |||
| 4722 | case LookupResult::NotFoundInCurrentInstantiation: | |||
| 4723 | case LookupResult::FoundUnresolvedValue: | |||
| 4724 | if (TempSS) { | |||
| 4725 | // Immediately retry the lookup without the given CXXScopeSpec | |||
| 4726 | TempSS = nullptr; | |||
| 4727 | Candidate.WillReplaceSpecifier(true); | |||
| 4728 | goto retry_lookup; | |||
| 4729 | } | |||
| 4730 | if (TempMemberContext) { | |||
| 4731 | if (SS && !TempSS) | |||
| 4732 | TempSS = SS.get(); | |||
| 4733 | TempMemberContext = nullptr; | |||
| 4734 | goto retry_lookup; | |||
| 4735 | } | |||
| 4736 | if (SearchNamespaces) | |||
| 4737 | QualifiedResults.push_back(Candidate); | |||
| 4738 | break; | |||
| 4739 | ||||
| 4740 | case LookupResult::Ambiguous: | |||
| 4741 | // We don't deal with ambiguities. | |||
| 4742 | break; | |||
| 4743 | ||||
| 4744 | case LookupResult::Found: | |||
| 4745 | case LookupResult::FoundOverloaded: | |||
| 4746 | // Store all of the Decls for overloaded symbols | |||
| 4747 | for (auto *TRD : Result) | |||
| 4748 | Candidate.addCorrectionDecl(TRD); | |||
| 4749 | checkCorrectionVisibility(SemaRef, Candidate); | |||
| 4750 | if (!isCandidateViable(*CorrectionValidator, Candidate)) { | |||
| 4751 | if (SearchNamespaces) | |||
| 4752 | QualifiedResults.push_back(Candidate); | |||
| 4753 | break; | |||
| 4754 | } | |||
| 4755 | Candidate.setCorrectionRange(SS.get(), Result.getLookupNameInfo()); | |||
| 4756 | return true; | |||
| 4757 | } | |||
| 4758 | return false; | |||
| 4759 | } | |||
| 4760 | ||||
| 4761 | void TypoCorrectionConsumer::performQualifiedLookups() { | |||
| 4762 | unsigned TypoLen = Typo->getName().size(); | |||
| 4763 | for (const TypoCorrection &QR : QualifiedResults) { | |||
| 4764 | for (const auto &NSI : Namespaces) { | |||
| 4765 | DeclContext *Ctx = NSI.DeclCtx; | |||
| 4766 | const Type *NSType = NSI.NameSpecifier->getAsType(); | |||
| 4767 | ||||
| 4768 | // If the current NestedNameSpecifier refers to a class and the | |||
| 4769 | // current correction candidate is the name of that class, then skip | |||
| 4770 | // it as it is unlikely a qualified version of the class' constructor | |||
| 4771 | // is an appropriate correction. | |||
| 4772 | if (CXXRecordDecl *NSDecl = NSType ? NSType->getAsCXXRecordDecl() : | |||
| 4773 | nullptr) { | |||
| 4774 | if (NSDecl->getIdentifier() == QR.getCorrectionAsIdentifierInfo()) | |||
| 4775 | continue; | |||
| 4776 | } | |||
| 4777 | ||||
| 4778 | TypoCorrection TC(QR); | |||
| 4779 | TC.ClearCorrectionDecls(); | |||
| 4780 | TC.setCorrectionSpecifier(NSI.NameSpecifier); | |||
| 4781 | TC.setQualifierDistance(NSI.EditDistance); | |||
| 4782 | TC.setCallbackDistance(0); // Reset the callback distance | |||
| 4783 | ||||
| 4784 | // If the current correction candidate and namespace combination are | |||
| 4785 | // too far away from the original typo based on the normalized edit | |||
| 4786 | // distance, then skip performing a qualified name lookup. | |||
| 4787 | unsigned TmpED = TC.getEditDistance(true); | |||
| 4788 | if (QR.getCorrectionAsIdentifierInfo() != Typo && TmpED && | |||
| 4789 | TypoLen / TmpED < 3) | |||
| 4790 | continue; | |||
| 4791 | ||||
| 4792 | Result.clear(); | |||
| 4793 | Result.setLookupName(QR.getCorrectionAsIdentifierInfo()); | |||
| 4794 | if (!SemaRef.LookupQualifiedName(Result, Ctx)) | |||
| 4795 | continue; | |||
| 4796 | ||||
| 4797 | // Any corrections added below will be validated in subsequent | |||
| 4798 | // iterations of the main while() loop over the Consumer's contents. | |||
| 4799 | switch (Result.getResultKind()) { | |||
| 4800 | case LookupResult::Found: | |||
| 4801 | case LookupResult::FoundOverloaded: { | |||
| 4802 | if (SS && SS->isValid()) { | |||
| 4803 | std::string NewQualified = TC.getAsString(SemaRef.getLangOpts()); | |||
| 4804 | std::string OldQualified; | |||
| 4805 | llvm::raw_string_ostream OldOStream(OldQualified); | |||
| 4806 | SS->getScopeRep()->print(OldOStream, SemaRef.getPrintingPolicy()); | |||
| 4807 | OldOStream << Typo->getName(); | |||
| 4808 | // If correction candidate would be an identical written qualified | |||
| 4809 | // identifier, then the existing CXXScopeSpec probably included a | |||
| 4810 | // typedef that didn't get accounted for properly. | |||
| 4811 | if (OldOStream.str() == NewQualified) | |||
| 4812 | break; | |||
| 4813 | } | |||
| 4814 | for (LookupResult::iterator TRD = Result.begin(), TRDEnd = Result.end(); | |||
| 4815 | TRD != TRDEnd; ++TRD) { | |||
| 4816 | if (SemaRef.CheckMemberAccess(TC.getCorrectionRange().getBegin(), | |||
| 4817 | NSType ? NSType->getAsCXXRecordDecl() | |||
| 4818 | : nullptr, | |||
| 4819 | TRD.getPair()) == Sema::AR_accessible) | |||
| 4820 | TC.addCorrectionDecl(*TRD); | |||
| 4821 | } | |||
| 4822 | if (TC.isResolved()) { | |||
| 4823 | TC.setCorrectionRange(SS.get(), Result.getLookupNameInfo()); | |||
| 4824 | addCorrection(TC); | |||
| 4825 | } | |||
| 4826 | break; | |||
| 4827 | } | |||
| 4828 | case LookupResult::NotFound: | |||
| 4829 | case LookupResult::NotFoundInCurrentInstantiation: | |||
| 4830 | case LookupResult::Ambiguous: | |||
| 4831 | case LookupResult::FoundUnresolvedValue: | |||
| 4832 | break; | |||
| 4833 | } | |||
| 4834 | } | |||
| 4835 | } | |||
| 4836 | QualifiedResults.clear(); | |||
| 4837 | } | |||
| 4838 | ||||
| 4839 | TypoCorrectionConsumer::NamespaceSpecifierSet::NamespaceSpecifierSet( | |||
| 4840 | ASTContext &Context, DeclContext *CurContext, CXXScopeSpec *CurScopeSpec) | |||
| 4841 | : Context(Context), CurContextChain(buildContextChain(CurContext)) { | |||
| 4842 | if (NestedNameSpecifier *NNS = | |||
| 4843 | CurScopeSpec ? CurScopeSpec->getScopeRep() : nullptr) { | |||
| 4844 | llvm::raw_string_ostream SpecifierOStream(CurNameSpecifier); | |||
| 4845 | NNS->print(SpecifierOStream, Context.getPrintingPolicy()); | |||
| 4846 | ||||
| 4847 | getNestedNameSpecifierIdentifiers(NNS, CurNameSpecifierIdentifiers); | |||
| 4848 | } | |||
| 4849 | // Build the list of identifiers that would be used for an absolute | |||
| 4850 | // (from the global context) NestedNameSpecifier referring to the current | |||
| 4851 | // context. | |||
| 4852 | for (DeclContext *C : llvm::reverse(CurContextChain)) { | |||
| 4853 | if (auto *ND = dyn_cast_or_null<NamespaceDecl>(C)) | |||
| 4854 | CurContextIdentifiers.push_back(ND->getIdentifier()); | |||
| 4855 | } | |||
| 4856 | ||||
| 4857 | // Add the global context as a NestedNameSpecifier | |||
| 4858 | SpecifierInfo SI = {cast<DeclContext>(Context.getTranslationUnitDecl()), | |||
| 4859 | NestedNameSpecifier::GlobalSpecifier(Context), 1}; | |||
| 4860 | DistanceMap[1].push_back(SI); | |||
| 4861 | } | |||
| 4862 | ||||
| 4863 | auto TypoCorrectionConsumer::NamespaceSpecifierSet::buildContextChain( | |||
| 4864 | DeclContext *Start) -> DeclContextList { | |||
| 4865 | assert(Start && "Building a context chain from a null context")(static_cast <bool> (Start && "Building a context chain from a null context" ) ? void (0) : __assert_fail ("Start && \"Building a context chain from a null context\"" , "clang/lib/Sema/SemaLookup.cpp", 4865, __extension__ __PRETTY_FUNCTION__ )); | |||
| 4866 | DeclContextList Chain; | |||
| 4867 | for (DeclContext *DC = Start->getPrimaryContext(); DC != nullptr; | |||
| 4868 | DC = DC->getLookupParent()) { | |||
| 4869 | NamespaceDecl *ND = dyn_cast_or_null<NamespaceDecl>(DC); | |||
| 4870 | if (!DC->isInlineNamespace() && !DC->isTransparentContext() && | |||
| 4871 | !(ND && ND->isAnonymousNamespace())) | |||
| 4872 | Chain.push_back(DC->getPrimaryContext()); | |||
| 4873 | } | |||
| 4874 | return Chain; | |||
| 4875 | } | |||
| 4876 | ||||
| 4877 | unsigned | |||
| 4878 | TypoCorrectionConsumer::NamespaceSpecifierSet::buildNestedNameSpecifier( | |||
| 4879 | DeclContextList &DeclChain, NestedNameSpecifier *&NNS) { | |||
| 4880 | unsigned NumSpecifiers = 0; | |||
| 4881 | for (DeclContext *C : llvm::reverse(DeclChain)) { | |||
| 4882 | if (auto *ND = dyn_cast_or_null<NamespaceDecl>(C)) { | |||
| 4883 | NNS = NestedNameSpecifier::Create(Context, NNS, ND); | |||
| 4884 | ++NumSpecifiers; | |||
| 4885 | } else if (auto *RD = dyn_cast_or_null<RecordDecl>(C)) { | |||
| 4886 | NNS = NestedNameSpecifier::Create(Context, NNS, RD->isTemplateDecl(), | |||
| 4887 | RD->getTypeForDecl()); | |||
| 4888 | ++NumSpecifiers; | |||
| 4889 | } | |||
| 4890 | } | |||
| 4891 | return NumSpecifiers; | |||
| 4892 | } | |||
| 4893 | ||||
| 4894 | void TypoCorrectionConsumer::NamespaceSpecifierSet::addNameSpecifier( | |||
| 4895 | DeclContext *Ctx) { | |||
| 4896 | NestedNameSpecifier *NNS = nullptr; | |||
| 4897 | unsigned NumSpecifiers = 0; | |||
| 4898 | DeclContextList NamespaceDeclChain(buildContextChain(Ctx)); | |||
| 4899 | DeclContextList FullNamespaceDeclChain(NamespaceDeclChain); | |||
| 4900 | ||||
| 4901 | // Eliminate common elements from the two DeclContext chains. | |||
| 4902 | for (DeclContext *C : llvm::reverse(CurContextChain)) { | |||
| 4903 | if (NamespaceDeclChain.empty() || NamespaceDeclChain.back() != C) | |||
| 4904 | break; | |||
| 4905 | NamespaceDeclChain.pop_back(); | |||
| 4906 | } | |||
| 4907 | ||||
| 4908 | // Build the NestedNameSpecifier from what is left of the NamespaceDeclChain | |||
| 4909 | NumSpecifiers = buildNestedNameSpecifier(NamespaceDeclChain, NNS); | |||
| 4910 | ||||
| 4911 | // Add an explicit leading '::' specifier if needed. | |||
| 4912 | if (NamespaceDeclChain.empty()) { | |||
| 4913 | // Rebuild the NestedNameSpecifier as a globally-qualified specifier. | |||
| 4914 | NNS = NestedNameSpecifier::GlobalSpecifier(Context); | |||
| 4915 | NumSpecifiers = | |||
| 4916 | buildNestedNameSpecifier(FullNamespaceDeclChain, NNS); | |||
| 4917 | } else if (NamedDecl *ND
| |||
| 4918 | dyn_cast_or_null<NamedDecl>(NamespaceDeclChain.back())) { | |||
| 4919 | IdentifierInfo *Name = ND->getIdentifier(); | |||
| 4920 | bool SameNameSpecifier = false; | |||
| 4921 | if (llvm::is_contained(CurNameSpecifierIdentifiers, Name)) { | |||
| 4922 | std::string NewNameSpecifier; | |||
| 4923 | llvm::raw_string_ostream SpecifierOStream(NewNameSpecifier); | |||
| 4924 | SmallVector<const IdentifierInfo *, 4> NewNameSpecifierIdentifiers; | |||
| 4925 | getNestedNameSpecifierIdentifiers(NNS, NewNameSpecifierIdentifiers); | |||
| 4926 | NNS->print(SpecifierOStream, Context.getPrintingPolicy()); | |||
| ||||
| 4927 | SpecifierOStream.flush(); | |||
| 4928 | SameNameSpecifier = NewNameSpecifier == CurNameSpecifier; | |||
| 4929 | } | |||
| 4930 | if (SameNameSpecifier || llvm::is_contained(CurContextIdentifiers, Name)) { | |||
| 4931 | // Rebuild the NestedNameSpecifier as a globally-qualified specifier. | |||
| 4932 | NNS = NestedNameSpecifier::GlobalSpecifier(Context); | |||
| 4933 | NumSpecifiers = | |||
| 4934 | buildNestedNameSpecifier(FullNamespaceDeclChain, NNS); | |||
| 4935 | } | |||
| 4936 | } | |||
| 4937 | ||||
| 4938 | // If the built NestedNameSpecifier would be replacing an existing | |||
| 4939 | // NestedNameSpecifier, use the number of component identifiers that | |||
| 4940 | // would need to be changed as the edit distance instead of the number | |||
| 4941 | // of components in the built NestedNameSpecifier. | |||
| 4942 | if (NNS && !CurNameSpecifierIdentifiers.empty()) { | |||
| 4943 | SmallVector<const IdentifierInfo*, 4> NewNameSpecifierIdentifiers; | |||
| 4944 | getNestedNameSpecifierIdentifiers(NNS, NewNameSpecifierIdentifiers); | |||
| 4945 | NumSpecifiers = | |||
| 4946 | llvm::ComputeEditDistance(llvm::ArrayRef(CurNameSpecifierIdentifiers), | |||
| 4947 | llvm::ArrayRef(NewNameSpecifierIdentifiers)); | |||
| 4948 | } | |||
| 4949 | ||||
| 4950 | SpecifierInfo SI = {Ctx, NNS, NumSpecifiers}; | |||
| 4951 | DistanceMap[NumSpecifiers].push_back(SI); | |||
| 4952 | } | |||
| 4953 | ||||
| 4954 | /// Perform name lookup for a possible result for typo correction. | |||
| 4955 | static void LookupPotentialTypoResult(Sema &SemaRef, | |||
| 4956 | LookupResult &Res, | |||
| 4957 | IdentifierInfo *Name, | |||
| 4958 | Scope *S, CXXScopeSpec *SS, | |||
| 4959 | DeclContext *MemberContext, | |||
| 4960 | bool EnteringContext, | |||
| 4961 | bool isObjCIvarLookup, | |||
| 4962 | bool FindHidden) { | |||
| 4963 | Res.suppressDiagnostics(); | |||
| 4964 | Res.clear(); | |||
| 4965 | Res.setLookupName(Name); | |||
| 4966 | Res.setAllowHidden(FindHidden); | |||
| 4967 | if (MemberContext) { | |||
| 4968 | if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(MemberContext)) { | |||
| 4969 | if (isObjCIvarLookup) { | |||
| 4970 | if (ObjCIvarDecl *Ivar = Class->lookupInstanceVariable(Name)) { | |||
| 4971 | Res.addDecl(Ivar); | |||
| 4972 | Res.resolveKind(); | |||
| 4973 | return; | |||
| 4974 | } | |||
| 4975 | } | |||
| 4976 | ||||
| 4977 | if (ObjCPropertyDecl *Prop = Class->FindPropertyDeclaration( | |||
| 4978 | Name, ObjCPropertyQueryKind::OBJC_PR_query_instance)) { | |||
| 4979 | Res.addDecl(Prop); | |||
| 4980 | Res.resolveKind(); | |||
| 4981 | return; | |||
| 4982 | } | |||
| 4983 | } | |||
| 4984 | ||||
| 4985 | SemaRef.LookupQualifiedName(Res, MemberContext); | |||
| 4986 | return; | |||
| 4987 | } | |||
| 4988 | ||||
| 4989 | SemaRef.LookupParsedName(Res, S, SS, /*AllowBuiltinCreation=*/false, | |||
| 4990 | EnteringContext); | |||
| 4991 | ||||
| 4992 | // Fake ivar lookup; this should really be part of | |||
| 4993 | // LookupParsedName. | |||
| 4994 | if (ObjCMethodDecl *Method = SemaRef.getCurMethodDecl()) { | |||
| 4995 | if (Method->isInstanceMethod() && Method->getClassInterface() && | |||
| 4996 | (Res.empty() || | |||
| 4997 | (Res.isSingleResult() && | |||
| 4998 | Res.getFoundDecl()->isDefinedOutsideFunctionOrMethod()))) { | |||
| 4999 | if (ObjCIvarDecl *IV | |||
| 5000 | = Method->getClassInterface()->lookupInstanceVariable(Name)) { | |||
| 5001 | Res.addDecl(IV); | |||
| 5002 | Res.resolveKind(); | |||
| 5003 | } | |||
| 5004 | } | |||
| 5005 | } | |||
| 5006 | } | |||
| 5007 | ||||
| 5008 | /// Add keywords to the consumer as possible typo corrections. | |||
| 5009 | static void AddKeywordsToConsumer(Sema &SemaRef, | |||
| 5010 | TypoCorrectionConsumer &Consumer, | |||
| 5011 | Scope *S, CorrectionCandidateCallback &CCC, | |||
| 5012 | bool AfterNestedNameSpecifier) { | |||
| 5013 | if (AfterNestedNameSpecifier) { | |||
| 5014 | // For 'X::', we know exactly which keywords can appear next. | |||
| 5015 | Consumer.addKeywordResult("template"); | |||
| 5016 | if (CCC.WantExpressionKeywords) | |||
| 5017 | Consumer.addKeywordResult("operator"); | |||
| 5018 | return; | |||
| 5019 | } | |||
| 5020 | ||||
| 5021 | if (CCC.WantObjCSuper) | |||
| 5022 | Consumer.addKeywordResult("super"); | |||
| 5023 | ||||
| 5024 | if (CCC.WantTypeSpecifiers) { | |||
| 5025 | // Add type-specifier keywords to the set of results. | |||
| 5026 | static const char *const CTypeSpecs[] = { | |||
| 5027 | "char", "const", "double", "enum", "float", "int", "long", "short", | |||
| 5028 | "signed", "struct", "union", "unsigned", "void", "volatile", | |||
| 5029 | "_Complex", "_Imaginary", | |||
| 5030 | // storage-specifiers as well | |||
| 5031 | "extern", "inline", "static", "typedef" | |||
| 5032 | }; | |||
| 5033 | ||||
| 5034 | for (const auto *CTS : CTypeSpecs) | |||
| 5035 | Consumer.addKeywordResult(CTS); | |||
| 5036 | ||||
| 5037 | if (SemaRef.getLangOpts().C99) | |||
| 5038 | Consumer.addKeywordResult("restrict"); | |||
| 5039 | if (SemaRef.getLangOpts().Bool || SemaRef.getLangOpts().CPlusPlus) | |||
| 5040 | Consumer.addKeywordResult("bool"); | |||
| 5041 | else if (SemaRef.getLangOpts().C99) | |||
| 5042 | Consumer.addKeywordResult("_Bool"); | |||
| 5043 | ||||
| 5044 | if (SemaRef.getLangOpts().CPlusPlus) { | |||
| 5045 | Consumer.addKeywordResult("class"); | |||
| 5046 | Consumer.addKeywordResult("typename"); | |||
| 5047 | Consumer.addKeywordResult("wchar_t"); | |||
| 5048 | ||||
| 5049 | if (SemaRef.getLangOpts().CPlusPlus11) { | |||
| 5050 | Consumer.addKeywordResult("char16_t"); | |||
| 5051 | Consumer.addKeywordResult("char32_t"); | |||
| 5052 | Consumer.addKeywordResult("constexpr"); | |||
| 5053 | Consumer.addKeywordResult("decltype"); | |||
| 5054 | Consumer.addKeywordResult("thread_local"); | |||
| 5055 | } | |||
| 5056 | } | |||
| 5057 | ||||
| 5058 | if (SemaRef.getLangOpts().GNUKeywords) | |||
| 5059 | Consumer.addKeywordResult("typeof"); | |||
| 5060 | } else if (CCC.WantFunctionLikeCasts) { | |||
| 5061 | static const char *const CastableTypeSpecs[] = { | |||
| 5062 | "char", "double", "float", "int", "long", "short", | |||
| 5063 | "signed", "unsigned", "void" | |||
| 5064 | }; | |||
| 5065 | for (auto *kw : CastableTypeSpecs) | |||
| 5066 | Consumer.addKeywordResult(kw); | |||
| 5067 | } | |||
| 5068 | ||||
| 5069 | if (CCC.WantCXXNamedCasts && SemaRef.getLangOpts().CPlusPlus) { | |||
| 5070 | Consumer.addKeywordResult("const_cast"); | |||
| 5071 | Consumer.addKeywordResult("dynamic_cast"); | |||
| 5072 | Consumer.addKeywordResult("reinterpret_cast"); | |||
| 5073 | Consumer.addKeywordResult("static_cast"); | |||
| 5074 | } | |||
| 5075 | ||||
| 5076 | if (CCC.WantExpressionKeywords) { | |||
| 5077 | Consumer.addKeywordResult("sizeof"); | |||
| 5078 | if (SemaRef.getLangOpts().Bool || SemaRef.getLangOpts().CPlusPlus) { | |||
| 5079 | Consumer.addKeywordResult("false"); | |||
| 5080 | Consumer.addKeywordResult("true"); | |||
| 5081 | } | |||
| 5082 | ||||
| 5083 | if (SemaRef.getLangOpts().CPlusPlus) { | |||
| 5084 | static const char *const CXXExprs[] = { | |||
| 5085 | "delete", "new", "operator", "throw", "typeid" | |||
| 5086 | }; | |||
| 5087 | for (const auto *CE : CXXExprs) | |||
| 5088 | Consumer.addKeywordResult(CE); | |||
| 5089 | ||||
| 5090 | if (isa<CXXMethodDecl>(SemaRef.CurContext) && | |||
| 5091 | cast<CXXMethodDecl>(SemaRef.CurContext)->isInstance()) | |||
| 5092 | Consumer.addKeywordResult("this"); | |||
| 5093 | ||||
| 5094 | if (SemaRef.getLangOpts().CPlusPlus11) { | |||
| 5095 | Consumer.addKeywordResult("alignof"); | |||
| 5096 | Consumer.addKeywordResult("nullptr"); | |||
| 5097 | } | |||
| 5098 | } | |||
| 5099 | ||||
| 5100 | if (SemaRef.getLangOpts().C11) { | |||
| 5101 | // FIXME: We should not suggest _Alignof if the alignof macro | |||
| 5102 | // is present. | |||
| 5103 | Consumer.addKeywordResult("_Alignof"); | |||
| 5104 | } | |||
| 5105 | } | |||
| 5106 | ||||
| 5107 | if (CCC.WantRemainingKeywords) { | |||
| 5108 | if (SemaRef.getCurFunctionOrMethodDecl() || SemaRef.getCurBlock()) { | |||
| 5109 | // Statements. | |||
| 5110 | static const char *const CStmts[] = { | |||
| 5111 | "do", "else", "for", "goto", "if", "return", "switch", "while" }; | |||
| 5112 | for (const auto *CS : CStmts) | |||
| 5113 | Consumer.addKeywordResult(CS); | |||
| 5114 | ||||
| 5115 | if (SemaRef.getLangOpts().CPlusPlus) { | |||
| 5116 | Consumer.addKeywordResult("catch"); | |||
| 5117 | Consumer.addKeywordResult("try"); | |||
| 5118 | } | |||
| 5119 | ||||
| 5120 | if (S && S->getBreakParent()) | |||
| 5121 | Consumer.addKeywordResult("break"); | |||
| 5122 | ||||
| 5123 | if (S && S->getContinueParent()) | |||
| 5124 | Consumer.addKeywordResult("continue"); | |||
| 5125 | ||||
| 5126 | if (SemaRef.getCurFunction() && | |||
| 5127 | !SemaRef.getCurFunction()->SwitchStack.empty()) { | |||
| 5128 | Consumer.addKeywordResult("case"); | |||
| 5129 | Consumer.addKeywordResult("default"); | |||
| 5130 | } | |||
| 5131 | } else { | |||
| 5132 | if (SemaRef.getLangOpts().CPlusPlus) { | |||
| 5133 | Consumer.addKeywordResult("namespace"); | |||
| 5134 | Consumer.addKeywordResult("template"); | |||
| 5135 | } | |||
| 5136 | ||||
| 5137 | if (S && S->isClassScope()) { | |||
| 5138 | Consumer.addKeywordResult("explicit"); | |||
| 5139 | Consumer.addKeywordResult("friend"); | |||
| 5140 | Consumer.addKeywordResult("mutable"); | |||
| 5141 | Consumer.addKeywordResult("private"); | |||
| 5142 | Consumer.addKeywordResult("protected"); | |||
| 5143 | Consumer.addKeywordResult("public"); | |||
| 5144 | Consumer.addKeywordResult("virtual"); | |||
| 5145 | } | |||
| 5146 | } | |||
| 5147 | ||||
| 5148 | if (SemaRef.getLangOpts().CPlusPlus) { | |||
| 5149 | Consumer.addKeywordResult("using"); | |||
| 5150 | ||||
| 5151 | if (SemaRef.getLangOpts().CPlusPlus11) | |||
| 5152 | Consumer.addKeywordResult("static_assert"); | |||
| 5153 | } | |||
| 5154 | } | |||
| 5155 | } | |||
| 5156 | ||||
| 5157 | std::unique_ptr<TypoCorrectionConsumer> Sema::makeTypoCorrectionConsumer( | |||
| 5158 | const DeclarationNameInfo &TypoName, Sema::LookupNameKind LookupKind, | |||
| 5159 | Scope *S, CXXScopeSpec *SS, CorrectionCandidateCallback &CCC, | |||
| 5160 | DeclContext *MemberContext, bool EnteringContext, | |||
| 5161 | const ObjCObjectPointerType *OPT, bool ErrorRecovery) { | |||
| 5162 | ||||
| 5163 | if (Diags.hasFatalErrorOccurred() || !getLangOpts().SpellChecking || | |||
| 5164 | DisableTypoCorrection) | |||
| 5165 | return nullptr; | |||
| 5166 | ||||
| 5167 | // In Microsoft mode, don't perform typo correction in a template member | |||
| 5168 | // function dependent context because it interferes with the "lookup into | |||
| 5169 | // dependent bases of class templates" feature. | |||
| 5170 | if (getLangOpts().MSVCCompat && CurContext->isDependentContext() && | |||
| 5171 | isa<CXXMethodDecl>(CurContext)) | |||
| 5172 | return nullptr; | |||
| 5173 | ||||
| 5174 | // We only attempt to correct typos for identifiers. | |||
| 5175 | IdentifierInfo *Typo = TypoName.getName().getAsIdentifierInfo(); | |||
| 5176 | if (!Typo) | |||
| 5177 | return nullptr; | |||
| 5178 | ||||
| 5179 | // If the scope specifier itself was invalid, don't try to correct | |||
| 5180 | // typos. | |||
| 5181 | if (SS && SS->isInvalid()) | |||
| 5182 | return nullptr; | |||
| 5183 | ||||
| 5184 | // Never try to correct typos during any kind of code synthesis. | |||
| 5185 | if (!CodeSynthesisContexts.empty()) | |||
| 5186 | return nullptr; | |||
| 5187 | ||||
| 5188 | // Don't try to correct 'super'. | |||
| 5189 | if (S && S->isInObjcMethodScope() && Typo == getSuperIdentifier()) | |||
| 5190 | return nullptr; | |||
| 5191 | ||||
| 5192 | // Abort if typo correction already failed for this specific typo. | |||
| 5193 | IdentifierSourceLocations::iterator locs = TypoCorrectionFailures.find(Typo); | |||
| 5194 | if (locs != TypoCorrectionFailures.end() && | |||
| 5195 | locs->second.count(TypoName.getLoc())) | |||
| 5196 | return nullptr; | |||
| 5197 | ||||
| 5198 | // Don't try to correct the identifier "vector" when in AltiVec mode. | |||
| 5199 | // TODO: Figure out why typo correction misbehaves in this case, fix it, and | |||
| 5200 | // remove this workaround. | |||
| 5201 | if ((getLangOpts().AltiVec || getLangOpts().ZVector) && Typo->isStr("vector")) | |||
| 5202 | return nullptr; | |||
| 5203 | ||||
| 5204 | // Provide a stop gap for files that are just seriously broken. Trying | |||
| 5205 | // to correct all typos can turn into a HUGE performance penalty, causing | |||
| 5206 | // some files to take minutes to get rejected by the parser. | |||
| 5207 | unsigned Limit = getDiagnostics().getDiagnosticOptions().SpellCheckingLimit; | |||
| 5208 | if (Limit && TyposCorrected >= Limit) | |||
| 5209 | return nullptr; | |||
| 5210 | ++TyposCorrected; | |||
| 5211 | ||||
| 5212 | // If we're handling a missing symbol error, using modules, and the | |||
| 5213 | // special search all modules option is used, look for a missing import. | |||
| 5214 | if (ErrorRecovery && getLangOpts().Modules && | |||
| 5215 | getLangOpts().ModulesSearchAll) { | |||
| 5216 | // The following has the side effect of loading the missing module. | |||
| 5217 | getModuleLoader().lookupMissingImports(Typo->getName(), | |||
| 5218 | TypoName.getBeginLoc()); | |||
| 5219 | } | |||
| 5220 | ||||
| 5221 | // Extend the lifetime of the callback. We delayed this until here | |||
| 5222 | // to avoid allocations in the hot path (which is where no typo correction | |||
| 5223 | // occurs). Note that CorrectionCandidateCallback is polymorphic and | |||
| 5224 | // initially stack-allocated. | |||
| 5225 | std::unique_ptr<CorrectionCandidateCallback> ClonedCCC = CCC.clone(); | |||
| 5226 | auto Consumer = std::make_unique<TypoCorrectionConsumer>( | |||
| 5227 | *this, TypoName, LookupKind, S, SS, std::move(ClonedCCC), MemberContext, | |||
| 5228 | EnteringContext); | |||
| 5229 | ||||
| 5230 | // Perform name lookup to find visible, similarly-named entities. | |||
| 5231 | bool IsUnqualifiedLookup = false; | |||
| 5232 | DeclContext *QualifiedDC = MemberContext; | |||
| 5233 | if (MemberContext) { | |||
| 5234 | LookupVisibleDecls(MemberContext, LookupKind, *Consumer); | |||
| 5235 | ||||
| 5236 | // Look in qualified interfaces. | |||
| 5237 | if (OPT) { | |||
| 5238 | for (auto *I : OPT->quals()) | |||
| 5239 | LookupVisibleDecls(I, LookupKind, *Consumer); | |||
| 5240 | } | |||
| 5241 | } else if (SS && SS->isSet()) { | |||
| 5242 | QualifiedDC = computeDeclContext(*SS, EnteringContext); | |||
| 5243 | if (!QualifiedDC) | |||
| 5244 | return nullptr; | |||
| 5245 | ||||
| 5246 | LookupVisibleDecls(QualifiedDC, LookupKind, *Consumer); | |||
| 5247 | } else { | |||
| 5248 | IsUnqualifiedLookup = true; | |||
| 5249 | } | |||
| 5250 | ||||
| 5251 | // Determine whether we are going to search in the various namespaces for | |||
| 5252 | // corrections. | |||
| 5253 | bool SearchNamespaces | |||
| 5254 | = getLangOpts().CPlusPlus && | |||
| 5255 | (IsUnqualifiedLookup || (SS && SS->isSet())); | |||
| 5256 | ||||
| 5257 | if (IsUnqualifiedLookup || SearchNamespaces) { | |||
| 5258 | // For unqualified lookup, look through all of the names that we have | |||
| 5259 | // seen in this translation unit. | |||
| 5260 | // FIXME: Re-add the ability to skip very unlikely potential corrections. | |||
| 5261 | for (const auto &I : Context.Idents) | |||
| 5262 | Consumer->FoundName(I.getKey()); | |||
| 5263 | ||||
| 5264 | // Walk through identifiers in external identifier sources. | |||
| 5265 | // FIXME: Re-add the ability to skip very unlikely potential corrections. | |||
| 5266 | if (IdentifierInfoLookup *External | |||
| 5267 | = Context.Idents.getExternalIdentifierLookup()) { | |||
| 5268 | std::unique_ptr<IdentifierIterator> Iter(External->getIdentifiers()); | |||
| 5269 | do { | |||
| 5270 | StringRef Name = Iter->Next(); | |||
| 5271 | if (Name.empty()) | |||
| 5272 | break; | |||
| 5273 | ||||
| 5274 | Consumer->FoundName(Name); | |||
| 5275 | } while (true); | |||
| 5276 | } | |||
| 5277 | } | |||
| 5278 | ||||
| 5279 | AddKeywordsToConsumer(*this, *Consumer, S, | |||
| 5280 | *Consumer->getCorrectionValidator(), | |||
| 5281 | SS && SS->isNotEmpty()); | |||
| 5282 | ||||
| 5283 | // Build the NestedNameSpecifiers for the KnownNamespaces, if we're going | |||
| 5284 | // to search those namespaces. | |||
| 5285 | if (SearchNamespaces) { | |||
| 5286 | // Load any externally-known namespaces. | |||
| 5287 | if (ExternalSource && !LoadedExternalKnownNamespaces) { | |||
| 5288 | SmallVector<NamespaceDecl *, 4> ExternalKnownNamespaces; | |||
| 5289 | LoadedExternalKnownNamespaces = true; | |||
| 5290 | ExternalSource->ReadKnownNamespaces(ExternalKnownNamespaces); | |||
| 5291 | for (auto *N : ExternalKnownNamespaces) | |||
| 5292 | KnownNamespaces[N] = true; | |||
| 5293 | } | |||
| 5294 | ||||
| 5295 | Consumer->addNamespaces(KnownNamespaces); | |||
| 5296 | } | |||
| 5297 | ||||
| 5298 | return Consumer; | |||
| 5299 | } | |||
| 5300 | ||||
| 5301 | /// Try to "correct" a typo in the source code by finding | |||
| 5302 | /// visible declarations whose names are similar to the name that was | |||
| 5303 | /// present in the source code. | |||
| 5304 | /// | |||
| 5305 | /// \param TypoName the \c DeclarationNameInfo structure that contains | |||
| 5306 | /// the name that was present in the source code along with its location. | |||
| 5307 | /// | |||
| 5308 | /// \param LookupKind the name-lookup criteria used to search for the name. | |||
| 5309 | /// | |||
| 5310 | /// \param S the scope in which name lookup occurs. | |||
| 5311 | /// | |||
| 5312 | /// \param SS the nested-name-specifier that precedes the name we're | |||
| 5313 | /// looking for, if present. | |||
| 5314 | /// | |||
| 5315 | /// \param CCC A CorrectionCandidateCallback object that provides further | |||
| 5316 | /// validation of typo correction candidates. It also provides flags for | |||
| 5317 | /// determining the set of keywords permitted. | |||
| 5318 | /// | |||
| 5319 | /// \param MemberContext if non-NULL, the context in which to look for | |||
| 5320 | /// a member access expression. | |||
| 5321 | /// | |||
| 5322 | /// \param EnteringContext whether we're entering the context described by | |||
| 5323 | /// the nested-name-specifier SS. | |||
| 5324 | /// | |||
| 5325 | /// \param OPT when non-NULL, the search for visible declarations will | |||
| 5326 | /// also walk the protocols in the qualified interfaces of \p OPT. | |||
| 5327 | /// | |||
| 5328 | /// \returns a \c TypoCorrection containing the corrected name if the typo | |||
| 5329 | /// along with information such as the \c NamedDecl where the corrected name | |||
| 5330 | /// was declared, and any additional \c NestedNameSpecifier needed to access | |||
| 5331 | /// it (C++ only). The \c TypoCorrection is empty if there is no correction. | |||
| 5332 | TypoCorrection Sema::CorrectTypo(const DeclarationNameInfo &TypoName, | |||
| 5333 | Sema::LookupNameKind LookupKind, | |||
| 5334 | Scope *S, CXXScopeSpec *SS, | |||
| 5335 | CorrectionCandidateCallback &CCC, | |||
| 5336 | CorrectTypoKind Mode, | |||
| 5337 | DeclContext *MemberContext, | |||
| 5338 | bool EnteringContext, | |||
| 5339 | const ObjCObjectPointerType *OPT, | |||
| 5340 | bool RecordFailure) { | |||
| 5341 | // Always let the ExternalSource have the first chance at correction, even | |||
| 5342 | // if we would otherwise have given up. | |||
| 5343 | if (ExternalSource) { | |||
| 5344 | if (TypoCorrection Correction = | |||
| 5345 | ExternalSource->CorrectTypo(TypoName, LookupKind, S, SS, CCC, | |||
| 5346 | MemberContext, EnteringContext, OPT)) | |||
| 5347 | return Correction; | |||
| 5348 | } | |||
| 5349 | ||||
| 5350 | // Ugly hack equivalent to CTC == CTC_ObjCMessageReceiver; | |||
| 5351 | // WantObjCSuper is only true for CTC_ObjCMessageReceiver and for | |||
| 5352 | // some instances of CTC_Unknown, while WantRemainingKeywords is true | |||
| 5353 | // for CTC_Unknown but not for CTC_ObjCMessageReceiver. | |||
| 5354 | bool ObjCMessageReceiver = CCC.WantObjCSuper && !CCC.WantRemainingKeywords; | |||
| 5355 | ||||
| 5356 | IdentifierInfo *Typo = TypoName.getName().getAsIdentifierInfo(); | |||
| 5357 | auto Consumer = makeTypoCorrectionConsumer(TypoName, LookupKind, S, SS, CCC, | |||
| 5358 | MemberContext, EnteringContext, | |||
| 5359 | OPT, Mode == CTK_ErrorRecovery); | |||
| 5360 | ||||
| 5361 | if (!Consumer) | |||
| 5362 | return TypoCorrection(); | |||
| 5363 | ||||
| 5364 | // If we haven't found anything, we're done. | |||
| 5365 | if (Consumer->empty()) | |||
| 5366 | return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure); | |||
| 5367 | ||||
| 5368 | // Make sure the best edit distance (prior to adding any namespace qualifiers) | |||
| 5369 | // is not more that about a third of the length of the typo's identifier. | |||
| 5370 | unsigned ED = Consumer->getBestEditDistance(true); | |||
| 5371 | unsigned TypoLen = Typo->getName().size(); | |||
| 5372 | if (ED > 0 && TypoLen / ED < 3) | |||
| 5373 | return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure); | |||
| 5374 | ||||
| 5375 | TypoCorrection BestTC = Consumer->getNextCorrection(); | |||
| 5376 | TypoCorrection SecondBestTC = Consumer->getNextCorrection(); | |||
| 5377 | if (!BestTC) | |||
| 5378 | return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure); | |||
| 5379 | ||||
| 5380 | ED = BestTC.getEditDistance(); | |||
| 5381 | ||||
| 5382 | if (TypoLen >= 3 && ED > 0 && TypoLen / ED < 3) { | |||
| 5383 | // If this was an unqualified lookup and we believe the callback | |||
| 5384 | // object wouldn't have filtered out possible corrections, note | |||
| 5385 | // that no correction was found. | |||
| 5386 | return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure); | |||
| 5387 | } | |||
| 5388 | ||||
| 5389 | // If only a single name remains, return that result. | |||
| 5390 | if (!SecondBestTC || | |||
| 5391 | SecondBestTC.getEditDistance(false) > BestTC.getEditDistance(false)) { | |||
| 5392 | const TypoCorrection &Result = BestTC; | |||
| 5393 | ||||
| 5394 | // Don't correct to a keyword that's the same as the typo; the keyword | |||
| 5395 | // wasn't actually in scope. | |||
| 5396 | if (ED == 0 && Result.isKeyword()) | |||
| 5397 | return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure); | |||
| 5398 | ||||
| 5399 | TypoCorrection TC = Result; | |||
| 5400 | TC.setCorrectionRange(SS, TypoName); | |||
| 5401 | checkCorrectionVisibility(*this, TC); | |||
| 5402 | return TC; | |||
| 5403 | } else if (SecondBestTC && ObjCMessageReceiver) { | |||
| 5404 | // Prefer 'super' when we're completing in a message-receiver | |||
| 5405 | // context. | |||
| 5406 | ||||
| 5407 | if (BestTC.getCorrection().getAsString() != "super") { | |||
| 5408 | if (SecondBestTC.getCorrection().getAsString() == "super") | |||
| 5409 | BestTC = SecondBestTC; | |||
| 5410 | else if ((*Consumer)["super"].front().isKeyword()) | |||
| 5411 | BestTC = (*Consumer)["super"].front(); | |||
| 5412 | } | |||
| 5413 | // Don't correct to a keyword that's the same as the typo; the keyword | |||
| 5414 | // wasn't actually in scope. | |||
| 5415 | if (BestTC.getEditDistance() == 0 || | |||
| 5416 | BestTC.getCorrection().getAsString() != "super") | |||
| 5417 | return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure); | |||
| 5418 | ||||
| 5419 | BestTC.setCorrectionRange(SS, TypoName); | |||
| 5420 | return BestTC; | |||
| 5421 | } | |||
| 5422 | ||||
| 5423 | // Record the failure's location if needed and return an empty correction. If | |||
| 5424 | // this was an unqualified lookup and we believe the callback object did not | |||
| 5425 | // filter out possible corrections, also cache the failure for the typo. | |||
| 5426 | return FailedCorrection(Typo, TypoName.getLoc(), RecordFailure && !SecondBestTC); | |||
| 5427 | } | |||
| 5428 | ||||
| 5429 | /// Try to "correct" a typo in the source code by finding | |||
| 5430 | /// visible declarations whose names are similar to the name that was | |||
| 5431 | /// present in the source code. | |||
| 5432 | /// | |||
| 5433 | /// \param TypoName the \c DeclarationNameInfo structure that contains | |||
| 5434 | /// the name that was present in the source code along with its location. | |||
| 5435 | /// | |||
| 5436 | /// \param LookupKind the name-lookup criteria used to search for the name. | |||
| 5437 | /// | |||
| 5438 | /// \param S the scope in which name lookup occurs. | |||
| 5439 | /// | |||
| 5440 | /// \param SS the nested-name-specifier that precedes the name we're | |||
| 5441 | /// looking for, if present. | |||
| 5442 | /// | |||
| 5443 | /// \param CCC A CorrectionCandidateCallback object that provides further | |||
| 5444 | /// validation of typo correction candidates. It also provides flags for | |||
| 5445 | /// determining the set of keywords permitted. | |||
| 5446 | /// | |||
| 5447 | /// \param TDG A TypoDiagnosticGenerator functor that will be used to print | |||
| 5448 | /// diagnostics when the actual typo correction is attempted. | |||
| 5449 | /// | |||
| 5450 | /// \param TRC A TypoRecoveryCallback functor that will be used to build an | |||
| 5451 | /// Expr from a typo correction candidate. | |||
| 5452 | /// | |||
| 5453 | /// \param MemberContext if non-NULL, the context in which to look for | |||
| 5454 | /// a member access expression. | |||
| 5455 | /// | |||
| 5456 | /// \param EnteringContext whether we're entering the context described by | |||
| 5457 | /// the nested-name-specifier SS. | |||
| 5458 | /// | |||
| 5459 | /// \param OPT when non-NULL, the search for visible declarations will | |||
| 5460 | /// also walk the protocols in the qualified interfaces of \p OPT. | |||
| 5461 | /// | |||
| 5462 | /// \returns a new \c TypoExpr that will later be replaced in the AST with an | |||
| 5463 | /// Expr representing the result of performing typo correction, or nullptr if | |||
| 5464 | /// typo correction is not possible. If nullptr is returned, no diagnostics will | |||
| 5465 | /// be emitted and it is the responsibility of the caller to emit any that are | |||
| 5466 | /// needed. | |||
| 5467 | TypoExpr *Sema::CorrectTypoDelayed( | |||
| 5468 | const DeclarationNameInfo &TypoName, Sema::LookupNameKind LookupKind, | |||
| 5469 | Scope *S, CXXScopeSpec *SS, CorrectionCandidateCallback &CCC, | |||
| 5470 | TypoDiagnosticGenerator TDG, TypoRecoveryCallback TRC, CorrectTypoKind Mode, | |||
| 5471 | DeclContext *MemberContext, bool EnteringContext, | |||
| 5472 | const ObjCObjectPointerType *OPT) { | |||
| 5473 | auto Consumer = makeTypoCorrectionConsumer(TypoName, LookupKind, S, SS, CCC, | |||
| 5474 | MemberContext, EnteringContext, | |||
| 5475 | OPT, Mode == CTK_ErrorRecovery); | |||
| 5476 | ||||
| 5477 | // Give the external sema source a chance to correct the typo. | |||
| 5478 | TypoCorrection ExternalTypo; | |||
| 5479 | if (ExternalSource && Consumer) { | |||
| 5480 | ExternalTypo = ExternalSource->CorrectTypo( | |||
| 5481 | TypoName, LookupKind, S, SS, *Consumer->getCorrectionValidator(), | |||
| 5482 | MemberContext, EnteringContext, OPT); | |||
| 5483 | if (ExternalTypo) | |||
| 5484 | Consumer->addCorrection(ExternalTypo); | |||
| 5485 | } | |||
| 5486 | ||||
| 5487 | if (!Consumer || Consumer->empty()) | |||
| 5488 | return nullptr; | |||
| 5489 | ||||
| 5490 | // Make sure the best edit distance (prior to adding any namespace qualifiers) | |||
| 5491 | // is not more that about a third of the length of the typo's identifier. | |||
| 5492 | unsigned ED = Consumer->getBestEditDistance(true); | |||
| 5493 | IdentifierInfo *Typo = TypoName.getName().getAsIdentifierInfo(); | |||
| 5494 | if (!ExternalTypo && ED > 0 && Typo->getName().size() / ED < 3) | |||
| 5495 | return nullptr; | |||
| 5496 | ExprEvalContexts.back().NumTypos++; | |||
| 5497 | return createDelayedTypo(std::move(Consumer), std::move(TDG), std::move(TRC), | |||
| 5498 | TypoName.getLoc()); | |||
| 5499 | } | |||
| 5500 | ||||
| 5501 | void TypoCorrection::addCorrectionDecl(NamedDecl *CDecl) { | |||
| 5502 | if (!CDecl) return; | |||
| 5503 | ||||
| 5504 | if (isKeyword()) | |||
| 5505 | CorrectionDecls.clear(); | |||
| 5506 | ||||
| 5507 | CorrectionDecls.push_back(CDecl); | |||
| 5508 | ||||
| 5509 | if (!CorrectionName) | |||
| 5510 | CorrectionName = CDecl->getDeclName(); | |||
| 5511 | } | |||
| 5512 | ||||
| 5513 | std::string TypoCorrection::getAsString(const LangOptions &LO) const { | |||
| 5514 | if (CorrectionNameSpec) { | |||
| 5515 | std::string tmpBuffer; | |||
| 5516 | llvm::raw_string_ostream PrefixOStream(tmpBuffer); | |||
| 5517 | CorrectionNameSpec->print(PrefixOStream, PrintingPolicy(LO)); | |||
| 5518 | PrefixOStream << CorrectionName; | |||
| 5519 | return PrefixOStream.str(); | |||
| 5520 | } | |||
| 5521 | ||||
| 5522 | return CorrectionName.getAsString(); | |||
| 5523 | } | |||
| 5524 | ||||
| 5525 | bool CorrectionCandidateCallback::ValidateCandidate( | |||
| 5526 | const TypoCorrection &candidate) { | |||
| 5527 | if (!candidate.isResolved()) | |||
| 5528 | return true; | |||
| 5529 | ||||
| 5530 | if (candidate.isKeyword()) | |||
| 5531 | return WantTypeSpecifiers || WantExpressionKeywords || WantCXXNamedCasts || | |||
| 5532 | WantRemainingKeywords || WantObjCSuper; | |||
| 5533 | ||||
| 5534 | bool HasNonType = false; | |||
| 5535 | bool HasStaticMethod = false; | |||
| 5536 | bool HasNonStaticMethod = false; | |||
| 5537 | for (Decl *D : candidate) { | |||
| 5538 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D)) | |||
| 5539 | D = FTD->getTemplatedDecl(); | |||
| 5540 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { | |||
| 5541 | if (Method->isStatic()) | |||
| 5542 | HasStaticMethod = true; | |||
| 5543 | else | |||
| 5544 | HasNonStaticMethod = true; | |||
| 5545 | } | |||
| 5546 | if (!isa<TypeDecl>(D)) | |||
| 5547 | HasNonType = true; | |||
| 5548 | } | |||
| 5549 | ||||
| 5550 | if (IsAddressOfOperand && HasNonStaticMethod && !HasStaticMethod && | |||
| 5551 | !candidate.getCorrectionSpecifier()) | |||
| 5552 | return false; | |||
| 5553 | ||||
| 5554 | return WantTypeSpecifiers || HasNonType; | |||
| 5555 | } | |||
| 5556 | ||||
| 5557 | FunctionCallFilterCCC::FunctionCallFilterCCC(Sema &SemaRef, unsigned NumArgs, | |||
| 5558 | bool HasExplicitTemplateArgs, | |||
| 5559 | MemberExpr *ME) | |||
| 5560 | : NumArgs(NumArgs), HasExplicitTemplateArgs(HasExplicitTemplateArgs), | |||
| 5561 | CurContext(SemaRef.CurContext), MemberFn(ME) { | |||
| 5562 | WantTypeSpecifiers = false; | |||
| 5563 | WantFunctionLikeCasts = SemaRef.getLangOpts().CPlusPlus && | |||
| 5564 | !HasExplicitTemplateArgs && NumArgs == 1; | |||
| 5565 | WantCXXNamedCasts = HasExplicitTemplateArgs && NumArgs == 1; | |||
| 5566 | WantRemainingKeywords = false; | |||
| 5567 | } | |||
| 5568 | ||||
| 5569 | bool FunctionCallFilterCCC::ValidateCandidate(const TypoCorrection &candidate) { | |||
| 5570 | if (!candidate.getCorrectionDecl()) | |||
| 5571 | return candidate.isKeyword(); | |||
| 5572 | ||||
| 5573 | for (auto *C : candidate) { | |||
| 5574 | FunctionDecl *FD = nullptr; | |||
| 5575 | NamedDecl *ND = C->getUnderlyingDecl(); | |||
| 5576 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND)) | |||
| 5577 | FD = FTD->getTemplatedDecl(); | |||
| 5578 | if (!HasExplicitTemplateArgs && !FD) { | |||
| 5579 | if (!(FD = dyn_cast<FunctionDecl>(ND)) && isa<ValueDecl>(ND)) { | |||
| 5580 | // If the Decl is neither a function nor a template function, | |||
| 5581 | // determine if it is a pointer or reference to a function. If so, | |||
| 5582 | // check against the number of arguments expected for the pointee. | |||
| 5583 | QualType ValType = cast<ValueDecl>(ND)->getType(); | |||
| 5584 | if (ValType.isNull()) | |||
| 5585 | continue; | |||
| 5586 | if (ValType->isAnyPointerType() || ValType->isReferenceType()) | |||
| 5587 | ValType = ValType->getPointeeType(); | |||
| 5588 | if (const FunctionProtoType *FPT = ValType->getAs<FunctionProtoType>()) | |||
| 5589 | if (FPT->getNumParams() == NumArgs) | |||
| 5590 | return true; | |||
| 5591 | } | |||
| 5592 | } | |||
| 5593 | ||||
| 5594 | // A typo for a function-style cast can look like a function call in C++. | |||
| 5595 | if ((HasExplicitTemplateArgs ? getAsTypeTemplateDecl(ND) != nullptr | |||
| 5596 | : isa<TypeDecl>(ND)) && | |||
| 5597 | CurContext->getParentASTContext().getLangOpts().CPlusPlus) | |||
| 5598 | // Only a class or class template can take two or more arguments. | |||
| 5599 | return NumArgs <= 1 || HasExplicitTemplateArgs || isa<CXXRecordDecl>(ND); | |||
| 5600 | ||||
| 5601 | // Skip the current candidate if it is not a FunctionDecl or does not accept | |||
| 5602 | // the current number of arguments. | |||
| 5603 | if (!FD || !(FD->getNumParams() >= NumArgs && | |||
| 5604 | FD->getMinRequiredArguments() <= NumArgs)) | |||
| 5605 | continue; | |||
| 5606 | ||||
| 5607 | // If the current candidate is a non-static C++ method, skip the candidate | |||
| 5608 | // unless the method being corrected--or the current DeclContext, if the | |||
| 5609 | // function being corrected is not a method--is a method in the same class | |||
| 5610 | // or a descendent class of the candidate's parent class. | |||
| 5611 | if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) { | |||
| 5612 | if (MemberFn || !MD->isStatic()) { | |||
| 5613 | const auto *CurMD = | |||
| 5614 | MemberFn | |||
| 5615 | ? dyn_cast_if_present<CXXMethodDecl>(MemberFn->getMemberDecl()) | |||
| 5616 | : dyn_cast_if_present<CXXMethodDecl>(CurContext); | |||
| 5617 | const CXXRecordDecl *CurRD = | |||
| 5618 | CurMD ? CurMD->getParent()->getCanonicalDecl() : nullptr; | |||
| 5619 | const CXXRecordDecl *RD = MD->getParent()->getCanonicalDecl(); | |||
| 5620 | if (!CurRD || (CurRD != RD && !CurRD->isDerivedFrom(RD))) | |||
| 5621 | continue; | |||
| 5622 | } | |||
| 5623 | } | |||
| 5624 | return true; | |||
| 5625 | } | |||
| 5626 | return false; | |||
| 5627 | } | |||
| 5628 | ||||
| 5629 | void Sema::diagnoseTypo(const TypoCorrection &Correction, | |||
| 5630 | const PartialDiagnostic &TypoDiag, | |||
| 5631 | bool ErrorRecovery) { | |||
| 5632 | diagnoseTypo(Correction, TypoDiag, PDiag(diag::note_previous_decl), | |||
| 5633 | ErrorRecovery); | |||
| 5634 | } | |||
| 5635 | ||||
| 5636 | /// Find which declaration we should import to provide the definition of | |||
| 5637 | /// the given declaration. | |||
| 5638 | static const NamedDecl *getDefinitionToImport(const NamedDecl *D) { | |||
| 5639 | if (const auto *VD = dyn_cast<VarDecl>(D)) | |||
| 5640 | return VD->getDefinition(); | |||
| 5641 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) | |||
| 5642 | return FD->getDefinition(); | |||
| 5643 | if (const auto *TD = dyn_cast<TagDecl>(D)) | |||
| 5644 | return TD->getDefinition(); | |||
| 5645 | if (const auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) | |||
| 5646 | return ID->getDefinition(); | |||
| 5647 | if (const auto *PD = dyn_cast<ObjCProtocolDecl>(D)) | |||
| 5648 | return PD->getDefinition(); | |||
| 5649 | if (const auto *TD = dyn_cast<TemplateDecl>(D)) | |||
| 5650 | if (const NamedDecl *TTD = TD->getTemplatedDecl()) | |||
| 5651 | return getDefinitionToImport(TTD); | |||
| 5652 | return nullptr; | |||
| 5653 | } | |||
| 5654 | ||||
| 5655 | void Sema::diagnoseMissingImport(SourceLocation Loc, const NamedDecl *Decl, | |||
| 5656 | MissingImportKind MIK, bool Recover) { | |||
| 5657 | // Suggest importing a module providing the definition of this entity, if | |||
| 5658 | // possible. | |||
| 5659 | const NamedDecl *Def = getDefinitionToImport(Decl); | |||
| 5660 | if (!Def) | |||
| 5661 | Def = Decl; | |||
| 5662 | ||||
| 5663 | Module *Owner = getOwningModule(Def); | |||
| 5664 | assert(Owner && "definition of hidden declaration is not in a module")(static_cast <bool> (Owner && "definition of hidden declaration is not in a module" ) ? void (0) : __assert_fail ("Owner && \"definition of hidden declaration is not in a module\"" , "clang/lib/Sema/SemaLookup.cpp", 5664, __extension__ __PRETTY_FUNCTION__ )); | |||
| 5665 | ||||
| 5666 | llvm::SmallVector<Module*, 8> OwningModules; | |||
| 5667 | OwningModules.push_back(Owner); | |||
| 5668 | auto Merged = Context.getModulesWithMergedDefinition(Def); | |||
| 5669 | OwningModules.insert(OwningModules.end(), Merged.begin(), Merged.end()); | |||
| 5670 | ||||
| 5671 | diagnoseMissingImport(Loc, Def, Def->getLocation(), OwningModules, MIK, | |||
| 5672 | Recover); | |||
| 5673 | } | |||
| 5674 | ||||
| 5675 | /// Get a "quoted.h" or <angled.h> include path to use in a diagnostic | |||
| 5676 | /// suggesting the addition of a #include of the specified file. | |||
| 5677 | static std::string getHeaderNameForHeader(Preprocessor &PP, const FileEntry *E, | |||
| 5678 | llvm::StringRef IncludingFile) { | |||
| 5679 | bool IsSystem = false; | |||
| 5680 | auto Path = PP.getHeaderSearchInfo().suggestPathToFileForDiagnostics( | |||
| 5681 | E, IncludingFile, &IsSystem); | |||
| 5682 | return (IsSystem ? '<' : '"') + Path + (IsSystem ? '>' : '"'); | |||
| 5683 | } | |||
| 5684 | ||||
| 5685 | void Sema::diagnoseMissingImport(SourceLocation UseLoc, const NamedDecl *Decl, | |||
| 5686 | SourceLocation DeclLoc, | |||
| 5687 | ArrayRef<Module *> Modules, | |||
| 5688 | MissingImportKind MIK, bool Recover) { | |||
| 5689 | assert(!Modules.empty())(static_cast <bool> (!Modules.empty()) ? void (0) : __assert_fail ("!Modules.empty()", "clang/lib/Sema/SemaLookup.cpp", 5689, __extension__ __PRETTY_FUNCTION__)); | |||
| 5690 | ||||
| 5691 | auto NotePrevious = [&] { | |||
| 5692 | // FIXME: Suppress the note backtrace even under | |||
| 5693 | // -fdiagnostics-show-note-include-stack. We don't care how this | |||
| 5694 | // declaration was previously reached. | |||
| 5695 | Diag(DeclLoc, diag::note_unreachable_entity) << (int)MIK; | |||
| 5696 | }; | |||
| 5697 | ||||
| 5698 | // Weed out duplicates from module list. | |||
| 5699 | llvm::SmallVector<Module*, 8> UniqueModules; | |||
| 5700 | llvm::SmallDenseSet<Module*, 8> UniqueModuleSet; | |||
| 5701 | for (auto *M : Modules) { | |||
| 5702 | if (M->isGlobalModule() || M->isPrivateModule()) | |||
| 5703 | continue; | |||
| 5704 | if (UniqueModuleSet.insert(M).second) | |||
| 5705 | UniqueModules.push_back(M); | |||
| 5706 | } | |||
| 5707 | ||||
| 5708 | // Try to find a suitable header-name to #include. | |||
| 5709 | std::string HeaderName; | |||
| 5710 | if (const FileEntry *Header = | |||
| 5711 | PP.getHeaderToIncludeForDiagnostics(UseLoc, DeclLoc)) { | |||
| 5712 | if (const FileEntry *FE = | |||
| 5713 | SourceMgr.getFileEntryForID(SourceMgr.getFileID(UseLoc))) | |||
| 5714 | HeaderName = getHeaderNameForHeader(PP, Header, FE->tryGetRealPathName()); | |||
| 5715 | } | |||
| 5716 | ||||
| 5717 | // If we have a #include we should suggest, or if all definition locations | |||
| 5718 | // were in global module fragments, don't suggest an import. | |||
| 5719 | if (!HeaderName.empty() || UniqueModules.empty()) { | |||
| 5720 | // FIXME: Find a smart place to suggest inserting a #include, and add | |||
| 5721 | // a FixItHint there. | |||
| 5722 | Diag(UseLoc, diag::err_module_unimported_use_header) | |||
| 5723 | << (int)MIK << Decl << !HeaderName.empty() << HeaderName; | |||
| 5724 | // Produce a note showing where the entity was declared. | |||
| 5725 | NotePrevious(); | |||
| 5726 | if (Recover) | |||
| 5727 | createImplicitModuleImportForErrorRecovery(UseLoc, Modules[0]); | |||
| 5728 | return; | |||
| 5729 | } | |||
| 5730 | ||||
| 5731 | Modules = UniqueModules; | |||
| 5732 | ||||
| 5733 | if (Modules.size() > 1) { | |||
| 5734 | std::string ModuleList; | |||
| 5735 | unsigned N = 0; | |||
| 5736 | for (const auto *M : Modules) { | |||
| 5737 | ModuleList += "\n "; | |||
| 5738 | if (++N == 5 && N != Modules.size()) { | |||
| 5739 | ModuleList += "[...]"; | |||
| 5740 | break; | |||
| 5741 | } | |||
| 5742 | ModuleList += M->getFullModuleName(); | |||
| 5743 | } | |||
| 5744 | ||||
| 5745 | Diag(UseLoc, diag::err_module_unimported_use_multiple) | |||
| 5746 | << (int)MIK << Decl << ModuleList; | |||
| 5747 | } else { | |||
| 5748 | // FIXME: Add a FixItHint that imports the corresponding module. | |||
| 5749 | Diag(UseLoc, diag::err_module_unimported_use) | |||
| 5750 | << (int)MIK << Decl << Modules[0]->getFullModuleName(); | |||
| 5751 | } | |||
| 5752 | ||||
| 5753 | NotePrevious(); | |||
| 5754 | ||||
| 5755 | // Try to recover by implicitly importing this module. | |||
| 5756 | if (Recover) | |||
| 5757 | createImplicitModuleImportForErrorRecovery(UseLoc, Modules[0]); | |||
| 5758 | } | |||
| 5759 | ||||
| 5760 | /// Diagnose a successfully-corrected typo. Separated from the correction | |||
| 5761 | /// itself to allow external validation of the result, etc. | |||
| 5762 | /// | |||
| 5763 | /// \param Correction The result of performing typo correction. | |||
| 5764 | /// \param TypoDiag The diagnostic to produce. This will have the corrected | |||
| 5765 | /// string added to it (and usually also a fixit). | |||
| 5766 | /// \param PrevNote A note to use when indicating the location of the entity to | |||
| 5767 | /// which we are correcting. Will have the correction string added to it. | |||
| 5768 | /// \param ErrorRecovery If \c true (the default), the caller is going to | |||
| 5769 | /// recover from the typo as if the corrected string had been typed. | |||
| 5770 | /// In this case, \c PDiag must be an error, and we will attach a fixit | |||
| 5771 | /// to it. | |||
| 5772 | void Sema::diagnoseTypo(const TypoCorrection &Correction, | |||
| 5773 | const PartialDiagnostic &TypoDiag, | |||
| 5774 | const PartialDiagnostic &PrevNote, | |||
| 5775 | bool ErrorRecovery) { | |||
| 5776 | std::string CorrectedStr = Correction.getAsString(getLangOpts()); | |||
| 5777 | std::string CorrectedQuotedStr = Correction.getQuoted(getLangOpts()); | |||
| 5778 | FixItHint FixTypo = FixItHint::CreateReplacement( | |||
| 5779 | Correction.getCorrectionRange(), CorrectedStr); | |||
| 5780 | ||||
| 5781 | // Maybe we're just missing a module import. | |||
| 5782 | if (Correction.requiresImport()) { | |||
| 5783 | NamedDecl *Decl = Correction.getFoundDecl(); | |||
| 5784 | assert(Decl && "import required but no declaration to import")(static_cast <bool> (Decl && "import required but no declaration to import" ) ? void (0) : __assert_fail ("Decl && \"import required but no declaration to import\"" , "clang/lib/Sema/SemaLookup.cpp", 5784, __extension__ __PRETTY_FUNCTION__ )); | |||
| 5785 | ||||
| 5786 | diagnoseMissingImport(Correction.getCorrectionRange().getBegin(), Decl, | |||
| 5787 | MissingImportKind::Declaration, ErrorRecovery); | |||
| 5788 | return; | |||
| 5789 | } | |||
| 5790 | ||||
| 5791 | Diag(Correction.getCorrectionRange().getBegin(), TypoDiag) | |||
| 5792 | << CorrectedQuotedStr << (ErrorRecovery ? FixTypo : FixItHint()); | |||
| 5793 | ||||
| 5794 | NamedDecl *ChosenDecl = | |||
| 5795 | Correction.isKeyword() ? nullptr : Correction.getFoundDecl(); | |||
| 5796 | if (PrevNote.getDiagID() && ChosenDecl) | |||
| 5797 | Diag(ChosenDecl->getLocation(), PrevNote) | |||
| 5798 | << CorrectedQuotedStr << (ErrorRecovery ? FixItHint() : FixTypo); | |||
| 5799 | ||||
| 5800 | // Add any extra diagnostics. | |||
| 5801 | for (const PartialDiagnostic &PD : Correction.getExtraDiagnostics()) | |||
| 5802 | Diag(Correction.getCorrectionRange().getBegin(), PD); | |||
| 5803 | } | |||
| 5804 | ||||
| 5805 | TypoExpr *Sema::createDelayedTypo(std::unique_ptr<TypoCorrectionConsumer> TCC, | |||
| 5806 | TypoDiagnosticGenerator TDG, | |||
| 5807 | TypoRecoveryCallback TRC, | |||
| 5808 | SourceLocation TypoLoc) { | |||
| 5809 | assert(TCC && "createDelayedTypo requires a valid TypoCorrectionConsumer")(static_cast <bool> (TCC && "createDelayedTypo requires a valid TypoCorrectionConsumer" ) ? void (0) : __assert_fail ("TCC && \"createDelayedTypo requires a valid TypoCorrectionConsumer\"" , "clang/lib/Sema/SemaLookup.cpp", 5809, __extension__ __PRETTY_FUNCTION__ )); | |||
| 5810 | auto TE = new (Context) TypoExpr(Context.DependentTy, TypoLoc); | |||
| 5811 | auto &State = DelayedTypos[TE]; | |||
| 5812 | State.Consumer = std::move(TCC); | |||
| 5813 | State.DiagHandler = std::move(TDG); | |||
| 5814 | State.RecoveryHandler = std::move(TRC); | |||
| 5815 | if (TE) | |||
| 5816 | TypoExprs.push_back(TE); | |||
| 5817 | return TE; | |||
| 5818 | } | |||
| 5819 | ||||
| 5820 | const Sema::TypoExprState &Sema::getTypoExprState(TypoExpr *TE) const { | |||
| 5821 | auto Entry = DelayedTypos.find(TE); | |||
| 5822 | assert(Entry != DelayedTypos.end() &&(static_cast <bool> (Entry != DelayedTypos.end() && "Failed to get the state for a TypoExpr!") ? void (0) : __assert_fail ("Entry != DelayedTypos.end() && \"Failed to get the state for a TypoExpr!\"" , "clang/lib/Sema/SemaLookup.cpp", 5823, __extension__ __PRETTY_FUNCTION__ )) | |||
| 5823 | "Failed to get the state for a TypoExpr!")(static_cast <bool> (Entry != DelayedTypos.end() && "Failed to get the state for a TypoExpr!") ? void (0) : __assert_fail ("Entry != DelayedTypos.end() && \"Failed to get the state for a TypoExpr!\"" , "clang/lib/Sema/SemaLookup.cpp", 5823, __extension__ __PRETTY_FUNCTION__ )); | |||
| 5824 | return Entry->second; | |||
| 5825 | } | |||
| 5826 | ||||
| 5827 | void Sema::clearDelayedTypo(TypoExpr *TE) { | |||
| 5828 | DelayedTypos.erase(TE); | |||
| 5829 | } | |||
| 5830 | ||||
| 5831 | void Sema::ActOnPragmaDump(Scope *S, SourceLocation IILoc, IdentifierInfo *II) { | |||
| 5832 | DeclarationNameInfo Name(II, IILoc); | |||
| 5833 | LookupResult R(*this, Name, LookupAnyName, Sema::NotForRedeclaration); | |||
| 5834 | R.suppressDiagnostics(); | |||
| 5835 | R.setHideTags(false); | |||
| 5836 | LookupName(R, S); | |||
| 5837 | R.dump(); | |||
| 5838 | } | |||
| 5839 | ||||
| 5840 | void Sema::ActOnPragmaDump(Expr *E) { | |||
| 5841 | E->dump(); | |||
| 5842 | } |