16#include <unordered_map>
29#if !ENABLE_SYMBOLRESOLVER_TRACING
33#define AUTO_TRACE(...) (void)0
34#define AUTO_TRACE_ADD(...) (void)0
35#define AUTO_TRACE_EXIT(...) (void)0
42static std::unordered_map<std::string, std::pair<QCString,const MemberDef *> >
g_substMap;
77 m_elements.emplace_back(scope,fileScope,item,expScope);
86 [&](
const AccessElem &e) { return e.scope==scope && e.fileScope==fileScope && e.item==item; });
92 [&](
const AccessElem &e) { return e.scope==scope && e.fileScope==fileScope && e.item==item && e.expScope==expScope; });
186 const QCString &strippedTemplateParams,
214 const QCString &explicitScopePart=
"",
219 const QCString &explicitScopePart=
""
240 if (n.
isEmpty())
return nullptr;
245 std::unique_ptr<ArgumentList> actTemplParams;
246 if (!strippedTemplateParams.
isEmpty())
253 if (qualifierIndex!=-1)
256 explicitScopePart=name.
left(qualifierIndex);
259 name=name.
mid(qualifierIndex+2);
275 bool hasUsingStatements =
285 size_t scopeNameLen = scope->
name().
length()+1;
286 size_t nameLen = name.
length()+1;
287 size_t explicitPartLen = explicitScopePart.
length();
288 size_t fileScopeLen = hasUsingStatements ? 1+
m_fileScope->absFilePath().length() : 0;
305 if (hasUsingStatements)
317 if (std::find(visitedKeys.begin(),visitedKeys.end(),key.
str())!=std::end(visitedKeys))
325 visitedKeys.push_back(key.
str());
335 if (pTemplSpec) *pTemplSpec=pval->
templSpec;
336 if (pTypeDef) *pTypeDef=pval->
typeDef;
338 AUTO_TRACE_EXIT(
"found cached name={} templSpec={} typeDef={} resolvedTypedef={}",
350 int minDistance=10000;
356 getResolvedType(visitedKeys,scope,d,explicitScopePart,actTemplParams.get(),
357 minDistance,bestMatch,bestTypedef,bestTemplSpec,bestResolvedType);
359 if (minDistance==0)
break;
364 *pTypeDef = bestTypedef;
368 *pTemplSpec = bestTemplSpec;
372 *pResolvedType = bestResolvedType;
378 LookupInfo(bestMatch,bestTypedef,bestTemplSpec,bestResolvedType));
380 visitedKeys.erase(std::remove(visitedKeys.begin(), visitedKeys.end(), key.
str()), visitedKeys.end());
382 AUTO_TRACE_EXIT(
"found name={} templSpec={} typeDef={} resolvedTypedef={}",
403 AUTO_TRACE(
"scope={} name={} args={} checkCV={} insideCode={}",
404 scope->
name(),n,args,checkCV,insideCode);
405 if (n.
isEmpty())
return nullptr;
410 std::unique_ptr<ArgumentList> actTemplParams;
411 if (!strippedTemplateParams.
isEmpty())
418 if (qualifierIndex!=-1)
421 explicitScopePart=name.
left(qualifierIndex);
424 name=name.
mid(qualifierIndex+2);
426 AUTO_TRACE_ADD(
"qualifierIndex={} name={} explicitScopePart={} strippedTemplateParams={}",
427 qualifierIndex,name,explicitScopePart,strippedTemplateParams);
440 AUTO_TRACE_ADD(
"no symbols with name '{}' (including unspecialized)",name);
445 bool hasUsingStatements =
456 size_t nameLen = name.
length()+1;
457 size_t explicitPartLen = explicitScopePart.
length();
458 size_t strippedTemplateParamsLen = strippedTemplateParams.
length();
459 size_t fileScopeLen = hasUsingStatements ? 1+
m_fileScope->absFilePath().length() : 0;
460 size_t argsLen = args.
length()+1;
465 key.reserve(scopeNameLen+nameLen+explicitPartLen+strippedTemplateParamsLen+fileScopeLen+argsLen);
473 key+=explicitScopePart.
str();
474 key+=strippedTemplateParams.
str();
480 if (hasUsingStatements)
495 if (std::find(visitedKeys.begin(),visitedKeys.end(),key)!=std::end(visitedKeys))
502 visitedKeys.push_back(key);
511 if (pTemplSpec) *pTemplSpec=pval->
templSpec;
512 if (pTypeDef) *pTypeDef=pval->
typeDef;
514 AUTO_TRACE_EXIT(
"found cached name={} templSpec={} typeDef={} resolvedTypedef={}",
525 int minDistance=10000;
532 d->isLinkableInProject() ||
539 getResolvedSymbol(visitedKeys,scope,d,args,checkCV,insideCode,explicitScopePart,strippedTemplateParams,
false,
540 minDistance,bestMatch,bestTypedef,bestTemplSpec,bestResolvedType);
542 if (minDistance==0)
break;
547 if (bestMatch==
nullptr && args==
"()")
554 minDistance,bestMatch,bestTypedef,bestTemplSpec,bestResolvedType);
556 if (minDistance==0)
break;
562 *pTypeDef = bestTypedef;
566 *pTemplSpec = bestTemplSpec;
570 *pResolvedType = bestResolvedType;
574 LookupInfo lookupInfo(bestMatch,bestTypedef,bestTemplSpec,bestResolvedType);
579 visitedKeys.erase(std::remove(visitedKeys.begin(),visitedKeys.end(),key),visitedKeys.end());
581 AUTO_TRACE_EXIT(
"found name={} templSpec={} typeDef={} resolvedTypedef={}",
616 accessStack,scope,d,explicitScopePart);
631 if (distance<minDistance)
633 AUTO_TRACE_ADD(
"found symbol={} at distance={} minDistance={}",cd->
name(),distance,minDistance);
634 minDistance=distance;
636 bestTypedef =
nullptr;
637 bestTemplSpec.
clear();
640 else if (distance==minDistance &&
655 AUTO_TRACE_ADD(
"found symbol={} at distance={} minDistance={}",cd->
name(),distance,minDistance);
656 minDistance=distance;
658 bestTypedef =
nullptr;
659 bestTemplSpec.
clear();
681 if (distance<minDistance)
685 minDistance=distance;
690 AUTO_TRACE_ADD(
"found symbol={} at distance={} minDistance={}",cd->
name(),distance,minDistance);
693 bestTemplSpec = spec;
694 bestResolvedType = type;
700 bestTypedef = enumType;
709 bestTemplSpec = spec;
710 bestResolvedType = type;
717 bestTemplSpec.
clear();
718 bestResolvedType.
clear();
733 if (distance<minDistance)
735 AUTO_TRACE_ADD(
"found enum={} at distance={} minDistance={}",md->
name(),distance,minDistance);
736 minDistance=distance;
751 bestMatch?bestMatch->
name():
QCString(
"<none>"),bestResolvedType);
763 const QCString &strippedTemplateParams,
777 int distance =
isAccessibleFromWithExpScope(visitedKeys,visitedNamespaces,accessStack,scope,d,explicitScopePart+strippedTemplateParams);
778 if (distance==-1 && !strippedTemplateParams.
isEmpty())
794 if (distance<minDistance)
796 AUTO_TRACE_ADD(
"found symbol={} at distance={} minDistance={}",d->
name(),distance,minDistance);
797 minDistance=distance;
799 bestTypedef =
nullptr;
800 bestTemplSpec.
clear();
803 else if (distance==minDistance &&
818 AUTO_TRACE_ADD(
"found symbol={} at distance={} minDistance={}",d->
name(),distance,minDistance);
819 minDistance=distance;
821 bestTypedef =
nullptr;
822 bestTemplSpec.
clear();
858 if (match && distance<minDistance)
860 AUTO_TRACE_ADD(
"found symbol={} at distance={} minDistance={}",md->
name(),distance,minDistance);
861 minDistance=distance;
872 if (distance<minDistance)
874 AUTO_TRACE_ADD(
"found symbol={} at distance={} minDistance={}",d->
name(),distance,minDistance);
875 minDistance=distance;
877 bestTypedef =
nullptr;
878 bestTemplSpec.
clear();
879 bestResolvedType.
clear();
888 bestMatch?bestMatch->
name():
QCString(
"<none>"),bestResolvedType);
928 actTemplParams && !actTemplParams->
empty())
934 int tl=
static_cast<int>(type.
length());
936 while (ip>=0 && (type.
at(ip)==
'*' || type.
at(ip)==
'&' || type.
at(ip)==
' '))
940 type=type.
left(ip+1);
946 tl=
static_cast<int>(type.
length());
947 while (sp<tl && type.
at(sp)==
' ') sp++;
950 &memTypeDef,
nullptr,pResolvedType);
952 if (memTypeDef && memTypeDef->
isTypedef())
958 else if (memTypeDef && memTypeDef->
isEnumerate() && pMemType)
960 *pMemType = memTypeDef;
967 int i=type.
find(
'<');
970 if (pTemplSpec) *pTemplSpec = type.
mid(i);
978 i=
static_cast<int>(type.
length());
982 if (pTemplSpec) *pTemplSpec = type.
mid(i);
995 if (sp>0) pResolvedType->
prepend(typedefValue.
left(sp));
996 if (ip<tl-1) pResolvedType->
append(typedefValue.
right(tl-ip-1));
1000 *pResolvedType = typedefValue;
1014 pTemplSpec ? *pTemplSpec :
QCString(),
1015 pResolvedType ? *pResolvedType :
QCString()
1024 pTemplSpec ? *pTemplSpec :
"<nullptr>",
1025 pResolvedType ? *pResolvedType :
"<nullptr>"
1035 return isParentScope(
parent->getOuterScope(),item);
1048 AUTO_TRACE(
"scope={} item={} explictScopePart={}",
1050 if (explicitScopePart.
isEmpty())
1072 bool nestedClassInsideBaseClass =
1078 bool enumValueWithinEnum =
1083 if (itemScope==newScope)
1087 else if (nestedClassInsideBaseClass)
1100 else if (enumValueWithinEnum)
1110 visitedNamespaces.emplace(newScope->
name().
str(),newScope);
1126 if (visitedNamespaces.find(nd->name().str())==visitedNamespaces.end())
1152 result = (i==-1) ? -1 : i+2;
1185 result= (i==-1) ? -1 : i+2;
1208 AUTO_TRACE_ADD(
"qualScopePart={} memTypeDef={}",qualScopePart,memTypeDef?memTypeDef->
name():
"");
1244 if (namespaceMember && namespaceMember->
isEnumerate())
1246 next = namespaceMember;
1283 (
toFileDef(current))->getUsedDefinitions(),qualScopePart);
1286 if (current==
nullptr)
break;
1302 for (
const auto &d : dl)
1304 if (d->localName()==localName)
1320 AUTO_TRACE(
"item={} explicitScopePart={} level={}",item?item->
name():
QCString(), explicitScopePart, level);
1321 for (
const auto &und : nl)
1333 if (!und->getUsedNamespaces().empty() && std::find(visitedNamespaces.begin(),visitedNamespaces.end(),key.
str())==std::end(visitedNamespaces))
1335 visitedNamespaces.push_back(key.
str());
1356 for (
const auto &ud : dl)
1375 AUTO_TRACE(
"scope={} item={} item.definitionType={}",
1396 if ((itemIsMember || itemIsClass) &&
1398 (itemScope && itemScope->
name().
startsWith(
"anonymous_namespace{"))
1406 else if (itemIsClass)
1413 bool memberAccessibleFromScope =
1419 bool nestedClassInsideBaseClass =
1425 bool enumValueOfStrongEnum =
1433 if (itemScope==scope || memberAccessibleFromScope || nestedClassInsideBaseClass || enumValueOfStrongEnum)
1435 AUTO_TRACE_ADD(
"memberAccessibleFromScope={} nestedClassInsideBaseClass={} enumValueOfStrongEnum={}",
1436 memberAccessibleFromScope, nestedClassInsideBaseClass, enumValueOfStrongEnum);
1437 int distanceToBase=0;
1438 if (nestedClassInsideBaseClass)
1444 else if (memberAccessibleFromScope &&
1451 result+=distanceToBase;
1530 result= (i==-1) ? -1 : i+2;
1545 if (name.
isEmpty())
return result;
1552 int minDistance=10000;
1555 const int maxAddrSize = 20;
1556 char ptr_str[maxAddrSize];
1557 int num =
qsnprintf(ptr_str,maxAddrSize,
"%p:",(
void *)scope);
1559 key.reserve(num+name.
length()+1);
1567 if (pTypeDef) *pTypeDef = it->second.second;
1568 return it->second.first;
1585 if (distance!=-1 && distance<minDistance)
1588 minDistance=distance;
1598 if (pTypeDef) *pTypeDef=bestMatch;
1605 g_substMap.emplace(key,std::make_pair(result,bestMatch));
1616 :
p(std::make_unique<
Private>(fileScope))
1627 bool mayBeUnlinkable,
1630 AUTO_TRACE(
"scope={} name={} mayBeUnlinkable={} mayBeHidden={}",
1631 scope?scope->
name():
QCString(), name, mayBeUnlinkable, mayBeHidden);
1635 p->fileScope() ?
p->fileScope()->getLanguage() :
1638 if (scope==
nullptr ||
1643 ((lang==SrcLangExt::Java || lang==SrcLangExt::CSharp) &&
QCString(name).find(
"::")!=-1)
1658 result =
p->getResolvedTypeRec(visitedKeys,scope,lookupName,&
p->typeDef,&
p->templateSpec,&
p->resolvedType);
1659 if (result==
nullptr)
1666 if (!mayBeUnlinkable && result && !result->
isLinkable())
1668 if (!mayBeHidden || !result->
isHidden())
1685 AUTO_TRACE(
"scope={} name={} args={} checkCV={} insideCode={}",
1686 scope?scope->
name():
QCString(), name, args, checkCV, insideCode);
1690 const Definition *result =
p->getResolvedSymbolRec(visitedKeys,scope,name,args,checkCV,insideCode,onlyLinkable,&
p->typeDef,&
p->templateSpec,&
p->resolvedType);
1703 int result =
p->isAccessibleFrom(visitedKeys,accessStack,scope,item);
1711 AUTO_TRACE(
"scope={} item={} explicitScopePart={}",
1717 int result =
p->isAccessibleFromWithExpScope(visitedKeys,visitedNamespaces,accessStack,scope,item,explicitScopePart);
1724 p->setFileScope(fileScope);
1734 return p->templateSpec;
1739 return p->resolvedType;
Helper class representing the stack of items considered while resolving the scope.
bool find(const Definition *scope, const FileDef *fileScope, const Definition *item, const QCString &expScope)
std::vector< AccessElem > m_elements
bool find(const Definition *scope, const FileDef *fileScope, const Definition *item)
void push(const Definition *scope, const FileDef *fileScope, const Definition *item)
void push(const Definition *scope, const FileDef *fileScope, const Definition *item, const QCString &expScope)
This class represents an function or template argument list.
A abstract class representing of a compound symbol.
virtual const ArgumentList & templateArguments() const =0
Returns the template arguments of this class.
virtual bool isTemplate() const =0
Returns TRUE if this class is a template.
virtual const MemberDef * getMemberByName(const QCString &) const =0
Returns the member with the given name.
virtual bool isTemplateArgument() const =0
virtual FileDef * getFileDef() const =0
Returns the namespace this compound is in, or 0 if it has a global scope.
The common base class of all entity definitions found in the sources.
virtual SrcLangExt getLanguage() const =0
Returns the programming language this definition was written in.
virtual QCString getDefFileName() const =0
virtual bool isLinkable() const =0
virtual DefType definitionType() const =0
virtual bool isHidden() const =0
virtual const Definition * findInnerCompound(const QCString &name) const =0
virtual QCString qualifiedName() const =0
virtual bool isArtificial() const =0
virtual Definition * getOuterScope() const =0
virtual bool isReference() const =0
virtual const QCString & name() const =0
static Cache< std::string, LookupInfo > * typeLookupCache
static NamespaceDefMutable * globalScope
static Cache< std::string, LookupInfo > * symbolLookupCache
static SymbolMap< Definition > * symbolMap
A model of a file symbol.
virtual const LinkedRefMap< NamespaceDef > & getUsedNamespaces() const =0
virtual const LinkedRefMap< const Definition > & getUsedDefinitions() const =0
Container class representing a vector of objects with keys.
A model of a class/file/namespace member symbol.
virtual QCString typeString() const =0
virtual QCString getCachedResolvedTypedef() const =0
virtual const ClassDef * getCachedTypedefVal() const =0
virtual const ClassDef * getClassDef() const =0
virtual bool isTypedef() const =0
virtual const FileDef * getFileDef() const =0
virtual const ArgumentList & argumentList() const =0
virtual bool isStrongEnumValue() const =0
virtual QCString getCachedTypedefTemplSpec() const =0
virtual bool isTypedefValCached() const =0
virtual std::optional< ArgumentList > formalTemplateArguments() const =0
virtual bool isEnumerate() const =0
virtual QCString argsString() const =0
virtual bool isCallable() const =0
virtual const MemberDef * getEnumScope() const =0
virtual bool isEnumValue() const =0
virtual void cacheTypedefVal(const ClassDef *val, const QCString &templSpec, const QCString &resolvedType)=0
An abstract interface of a namespace symbol.
virtual const LinkedRefMap< NamespaceDef > & getUsedNamespaces() const =0
virtual const MemberDef * getMemberByName(const QCString &) const =0
virtual const LinkedRefMap< const Definition > & getUsedDefinitions() const =0
This is an alternative implementation of QCString.
int find(char c, int index=0, bool cs=TRUE) const
QCString & prepend(const char *s)
size_t length() const
Returns the length of the string, not counting the 0-terminator.
bool startsWith(const char *s) const
QCString mid(size_t index, size_t len=static_cast< size_t >(-1)) const
char & at(size_t i)
Returns a reference to the character at index i.
char * rawData()
Returns a writable pointer to the data.
bool isEmpty() const
Returns TRUE iff the string is empty.
QCString stripWhiteSpace() const
returns a copy of this string with leading and trailing whitespace removed
const std::string & str() const
QCString & append(char c)
QCString right(size_t len) const
size_t size() const
Returns the length of the string, not counting the 0-terminator.
int findRev(char c, int index=-1, bool cs=TRUE) const
const char * data() const
Returns a pointer to the contents of the string in the form of a 0-terminated C string.
QCString left(size_t len) const
bool stripPrefix(const QCString &prefix)
int isAccessibleFrom(const Definition *scope, const Definition *item)
Checks if symbol item is accessible from within scope.
int isAccessibleFromWithExpScope(const Definition *scope, const Definition *item, const QCString &explicitScopePart)
Check if symbol item is accessible from within scope, where it has to match the explicitScopePart.
QCString getResolvedType() const
In case a call to resolveClass() points to a typedef or using declaration.
const Definition * resolveSymbol(const Definition *scope, const QCString &name, const QCString &args=QCString(), bool checkCV=false, bool insideCode=false, bool onlyLinkable=false)
Find the symbool definition matching name within the scope set.
std::unique_ptr< Private > p
const ClassDef * resolveClass(const Definition *scope, const QCString &name, bool maybeUnlinkable=false, bool mayBeHidden=false)
Find the class definition matching name within the scope set.
SymbolResolver(const FileDef *fileScope=nullptr)
QCString getTemplateSpec() const
In case a call to resolveClass() points to a template specialization, the template part is return via...
void setFileScope(const FileDef *fd)
Sets or updates the file scope using when resolving symbols.
const MemberDef * getTypedef() const
In case a call to resolveClass() resolves to a type member (e.g.
ClassDef * getClass(const QCString &n)
ClassDef * toClassDef(Definition *d)
#define Config_getBool(name)
std::vector< std::string > StringVector
std::unique_ptr< ArgumentList > stringToArgumentList(SrcLangExt lang, const QCString &argsString, QCString *extraTypeChars=nullptr)
#define AUTO_TRACE_ADD(...)
#define AUTO_TRACE_EXIT(...)
constexpr DocNodeVariant * parent(DocNodeVariant *n)
returns the parent node of a given node n or nullptr if the node has no parent.
FileDef * toFileDef(Definition *d)
MemberDefMutable * toMemberDefMutable(Definition *d)
MemberDef * toMemberDef(Definition *d)
void replaceNamespaceAliases(QCString &name)
NamespaceDef * toNamespaceDef(Definition *d)
const char * qPrint(const char *s)
char * qstrcpy(char *dst, const char *src)
const FileDef * fileScope
AccessElem(const Definition *d, const FileDef *f, const Definition *i)
AccessElem(const Definition *d, const FileDef *f, const Definition *i, const QCString &e)
const Definition * definition
const MemberDef * typeDef
int isAccessibleFromWithExpScope(VisitedKeys &visitedKeys, VisitedNamespaces &visitedNamespaces, AccessStack &accessStack, const Definition *scope, const Definition *item, const QCString &explicitScopePart)
const MemberDef * typeDef
const FileDef * m_fileScope
const ClassDef * getResolvedTypeRec(VisitedKeys &visitedKeys, const Definition *scope, const QCString &n, const MemberDef **pTypeDef, QCString *pTemplSpec, QCString *pResolvedType)
void getResolvedSymbol(VisitedKeys &visitedKeys, const Definition *scope, const Definition *d, const QCString &args, bool checkCV, bool insideCode, const QCString &explicitScopePart, const QCString &strippedTemplateParams, bool forceCallable, int &minDistance, const Definition *&bestMatch, const MemberDef *&bestTypedef, QCString &bestTemplSpec, QCString &bestResolvedType)
bool accessibleViaUsingDefinition(VisitedKeys &visitedKeys, const LinkedRefMap< const Definition > &dl, const Definition *item, const QCString &explicitScopePart="")
const Definition * getResolvedSymbolRec(VisitedKeys &visitedKeys, const Definition *scope, const QCString &n, const QCString &args, bool checkCV, bool insideCode, bool onlyLinkable, const MemberDef **pTypeDef, QCString *pTemplSpec, QCString *pResolvedType)
bool accessibleViaUsingNamespace(VisitedKeys &visitedKeys, VisitedNamespaceKeys &visitedNamespaces, const LinkedRefMap< NamespaceDef > &nl, const Definition *item, const QCString &explicitScopePart="", int level=0)
const ClassDef * newResolveTypedef(VisitedKeys &visitedKeys, const Definition *scope, const MemberDef *md, const MemberDef **pMemType, QCString *pTemplSpec, QCString *pResolvedType, const ArgumentList *actTemplParams=nullptr)
int isAccessibleFrom(VisitedKeys &visitedKeys, AccessStack &accessStack, const Definition *scope, const Definition *item)
Private(const FileDef *f)
const Definition * endOfPathIsUsedClass(const LinkedRefMap< const Definition > &dl, const QCString &localName)
QCString substTypedef(VisitedKeys &visitedKeys, const Definition *scope, const QCString &name, const MemberDef **pTypeDef=nullptr)
const FileDef * fileScope() const
void setFileScope(const FileDef *fileScope)
const Definition * followPath(VisitedKeys &visitedKeys, const Definition *start, const QCString &path)
std::unordered_map< std::string, const MemberDef * > m_resolvedTypedefs
void getResolvedType(VisitedKeys &visitedKeys, const Definition *scope, const Definition *d, const QCString &explicitScopePart, const ArgumentList *actTemplParams, int &minDistance, const ClassDef *&bestMatch, const MemberDef *&bestTypedef, QCString &bestTemplSpec, QCString &bestResolvedType)
StringVector VisitedNamespaceKeys
static bool isCodeSymbol(Definition::DefType defType)
static std::recursive_mutex g_cacheTypedefMutex
std::unordered_map< std::string, const Definition * > VisitedNamespaces
static std::mutex g_substMapMutex
static std::unordered_map< std::string, std::pair< QCString, const MemberDef * > > g_substMap
static std::mutex g_cacheMutex
QCString substituteTemplateArgumentsInString(const QCString &nm, const ArgumentList &formalArgs, const ArgumentList *actualArgs)
bool matchArguments2(const Definition *srcScope, const FileDef *srcFileScope, const ArgumentList *srcAl, const Definition *dstScope, const FileDef *dstFileScope, const ArgumentList *dstAl, bool checkCV, SrcLangExt lang)
int computeQualifiedIndex(const QCString &name)
Return the index of the last :: in the string name that is still before the first <.
QCString argListToString(const ArgumentList &al, bool useCanonicalType, bool showDefVals)
QCString mangleCSharpGenericName(const QCString &name)
QCString stripTemplateSpecifiersFromScope(const QCString &fullName, bool parentOnly, QCString *pLastScopeStripped, QCString scopeName, bool allowArtificial)
int getScopeFragment(const QCString &s, int p, int *l)
A bunch of utility functions.