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;
68 m_elements.emplace_back(scope,fileScope,item,expScope);
77 [&](
const AccessElem &e) { return e.scope==scope && e.fileScope==fileScope && e.item==item; });
83 [&](
const AccessElem &e) { return e.scope==scope && e.fileScope==fileScope && e.item==item && e.expScope==expScope; });
202 const QCString &explicitScopePart=
"",
207 const QCString &explicitScopePart=
""
228 if (n.
isEmpty())
return nullptr;
233 std::unique_ptr<ArgumentList> actTemplParams;
234 if (!strippedTemplateParams.
isEmpty())
241 if (qualifierIndex!=-1)
244 explicitScopePart=name.
left(qualifierIndex);
247 name=name.
mid(qualifierIndex+2);
263 bool hasUsingStatements =
273 size_t scopeNameLen = scope->
name().
length()+1;
274 size_t nameLen = name.
length()+1;
275 size_t explicitPartLen = explicitScopePart.
length();
276 size_t fileScopeLen = hasUsingStatements ? 1+
m_fileScope->absFilePath().length() : 0;
293 if (hasUsingStatements)
305 if (std::find(visitedKeys.begin(),visitedKeys.end(),key.
str())!=std::end(visitedKeys))
313 visitedKeys.push_back(key.
str());
323 if (pTemplSpec) *pTemplSpec=pval->
templSpec;
324 if (pTypeDef) *pTypeDef=pval->
typeDef;
326 AUTO_TRACE_EXIT(
"found cached name={} templSpec={} typeDef={} resolvedTypedef={}",
338 int minDistance=10000;
342 getResolvedType(visitedKeys,scope,d,explicitScopePart,actTemplParams.get(),
343 minDistance,bestMatch,bestTypedef,bestTemplSpec,bestResolvedType);
344 if (minDistance==0)
break;
349 *pTypeDef = bestTypedef;
353 *pTemplSpec = bestTemplSpec;
357 *pResolvedType = bestResolvedType;
363 LookupInfo(bestMatch,bestTypedef,bestTemplSpec,bestResolvedType));
365 visitedKeys.erase(std::remove(visitedKeys.begin(), visitedKeys.end(), key.
str()), visitedKeys.end());
367 AUTO_TRACE_EXIT(
"found name={} templSpec={} typeDef={} resolvedTypedef={}",
387 AUTO_TRACE(
"scope={} name={} args={} checkCV={} insideCode={}",
388 scope->
name(),n,args,checkCV,insideCode);
389 if (n.
isEmpty())
return nullptr;
394 std::unique_ptr<ArgumentList> actTemplParams;
395 if (!strippedTemplateParams.
isEmpty())
402 if (qualifierIndex!=-1)
405 explicitScopePart=name.
left(qualifierIndex);
408 name=name.
mid(qualifierIndex+2);
410 AUTO_TRACE_ADD(
"qualifierIndex={} name={} explicitScopePart={}",qualifierIndex,name,explicitScopePart);
423 AUTO_TRACE_ADD(
"no symbols with name '{}' (including unspecialized)",name);
428 bool hasUsingStatements =
439 size_t nameLen = name.
length()+1;
440 size_t explicitPartLen = explicitScopePart.
length();
441 size_t fileScopeLen = hasUsingStatements ? 1+
m_fileScope->absFilePath().length() : 0;
442 size_t argsLen = args.
length()+1;
447 key.reserve(scopeNameLen+nameLen+explicitPartLen+fileScopeLen+argsLen);
455 key+=explicitScopePart.
str();
461 if (hasUsingStatements)
476 if (std::find(visitedKeys.begin(),visitedKeys.end(),key)!=std::end(visitedKeys))
483 visitedKeys.push_back(key);
492 if (pTemplSpec) *pTemplSpec=pval->
templSpec;
493 if (pTypeDef) *pTypeDef=pval->
typeDef;
495 AUTO_TRACE_EXIT(
"found cached name={} templSpec={} typeDef={} resolvedTypedef={}",
506 int minDistance=10000;
510 getResolvedSymbol(visitedKeys,scope,d,args,checkCV,insideCode,explicitScopePart,
false,
511 minDistance,bestMatch,bestTypedef,bestTemplSpec,bestResolvedType);
512 if (minDistance==0)
break;
517 if (bestMatch==
nullptr && args==
"()")
522 minDistance,bestMatch,bestTypedef,bestTemplSpec,bestResolvedType);
523 if (minDistance==0)
break;
529 *pTypeDef = bestTypedef;
533 *pTemplSpec = bestTemplSpec;
537 *pResolvedType = bestResolvedType;
541 LookupInfo lookupInfo(bestMatch,bestTypedef,bestTemplSpec,bestResolvedType);
546 visitedKeys.erase(std::remove(visitedKeys.begin(),visitedKeys.end(),key),visitedKeys.end());
548 AUTO_TRACE_EXIT(
"found name={} templSpec={} typeDef={} resolvedTypedef={}",
583 accessStack,scope,d,explicitScopePart);
598 if (distance<minDistance)
600 AUTO_TRACE_ADD(
"found symbol={} at distance={} minDistance={}",cd->
name(),distance,minDistance);
601 minDistance=distance;
603 bestTypedef =
nullptr;
604 bestTemplSpec.
clear();
607 else if (distance==minDistance &&
622 AUTO_TRACE_ADD(
"found symbol={} at distance={} minDistance={}",cd->
name(),distance,minDistance);
623 minDistance=distance;
625 bestTypedef =
nullptr;
626 bestTemplSpec.
clear();
648 if (distance<minDistance)
652 minDistance=distance;
657 AUTO_TRACE_ADD(
"found symbol={} at distance={} minDistance={}",cd->
name(),distance,minDistance);
660 bestTemplSpec = spec;
661 bestResolvedType = type;
667 bestTypedef = enumType;
676 bestTemplSpec = spec;
677 bestResolvedType = type;
684 bestTemplSpec.
clear();
685 bestResolvedType.
clear();
700 if (distance<minDistance)
702 AUTO_TRACE_ADD(
"found enum={} at distance={} minDistance={}",md->
name(),distance,minDistance);
703 minDistance=distance;
718 bestMatch?bestMatch->
name():
QCString(
"<none>"),bestResolvedType);
756 if (distance<minDistance)
758 AUTO_TRACE_ADD(
"found symbol={} at distance={} minDistance={}",d->
name(),distance,minDistance);
759 minDistance=distance;
761 bestTypedef =
nullptr;
762 bestTemplSpec.
clear();
765 else if (distance==minDistance &&
780 AUTO_TRACE_ADD(
"found symbol={} at distance={} minDistance={}",d->
name(),distance,minDistance);
781 minDistance=distance;
783 bestTypedef =
nullptr;
784 bestTemplSpec.
clear();
820 if (match && distance<minDistance)
822 AUTO_TRACE_ADD(
"found symbol={} at distance={} minDistance={}",md->
name(),distance,minDistance);
823 minDistance=distance;
833 if (distance<minDistance)
835 AUTO_TRACE_ADD(
"found symbol={} at distance={} minDistance={}",d->
name(),distance,minDistance);
836 minDistance=distance;
838 bestTypedef =
nullptr;
839 bestTemplSpec.
clear();
840 bestResolvedType.
clear();
849 bestMatch?bestMatch->
name():
QCString(
"<none>"),bestResolvedType);
889 actTemplParams && !actTemplParams->
empty())
895 int tl=
static_cast<int>(type.
length());
897 while (ip>=0 && (type.
at(ip)==
'*' || type.
at(ip)==
'&' || type.
at(ip)==
' '))
901 type=type.
left(ip+1);
907 tl=
static_cast<int>(type.
length());
908 while (sp<tl && type.
at(sp)==
' ') sp++;
911 &memTypeDef,
nullptr,pResolvedType);
913 if (memTypeDef && memTypeDef->
isTypedef())
919 else if (memTypeDef && memTypeDef->
isEnumerate() && pMemType)
921 *pMemType = memTypeDef;
928 int i=type.
find(
'<');
931 if (pTemplSpec) *pTemplSpec = type.
mid(i);
939 i=
static_cast<int>(type.
length());
943 if (pTemplSpec) *pTemplSpec = type.
mid(i);
956 if (sp>0) pResolvedType->
prepend(typedefValue.
left(sp));
957 if (ip<tl-1) pResolvedType->
append(typedefValue.
right(tl-ip-1));
961 *pResolvedType = typedefValue;
975 pTemplSpec ? *pTemplSpec :
QCString(),
976 pResolvedType ? *pResolvedType :
QCString()
985 pTemplSpec ? *pTemplSpec :
"<nullptr>",
986 pResolvedType ? *pResolvedType :
"<nullptr>"
996 return isParentScope(
parent->getOuterScope(),item);
1009 AUTO_TRACE(
"scope={} item={} explictScopePart={}",
1011 if (explicitScopePart.
isEmpty())
1033 bool nestedClassInsideBaseClass =
1039 bool enumValueWithinEnum =
1044 if (itemScope==newScope)
1048 else if (nestedClassInsideBaseClass)
1061 else if (enumValueWithinEnum)
1071 visitedNamespaces.emplace(newScope->
name().
str(),newScope);
1087 if (visitedNamespaces.find(nd->name().str())==visitedNamespaces.end())
1113 result = (i==-1) ? -1 : i+2;
1146 result= (i==-1) ? -1 : i+2;
1197 if (namespaceMember && namespaceMember->
isEnumerate())
1199 next = namespaceMember;
1236 (
toFileDef(current))->getUsedDefinitions(),qualScopePart);
1239 if (current==
nullptr)
break;
1255 for (
const auto &d : dl)
1257 if (d->localName()==localName)
1273 AUTO_TRACE(
"item={} explicitScopePart={} level={}",item?item->
name():
QCString(), explicitScopePart, level);
1274 for (
const auto &und : nl)
1286 if (!und->getUsedNamespaces().empty() && std::find(visitedNamespaces.begin(),visitedNamespaces.end(),key.
str())==std::end(visitedNamespaces))
1288 visitedNamespaces.push_back(key.
str());
1309 for (
const auto &ud : dl)
1348 if ((itemIsMember || itemIsClass) &&
1350 (itemScope && itemScope->
name().
startsWith(
"anonymous_namespace{"))
1358 else if (itemIsClass)
1365 bool memberAccessibleFromScope =
1371 bool nestedClassInsideBaseClass =
1377 bool enumValueOfStrongEnum =
1385 if (itemScope==scope || memberAccessibleFromScope || nestedClassInsideBaseClass || enumValueOfStrongEnum)
1387 AUTO_TRACE_ADD(
"memberAccessibleFromScope={} nestedClassInsideBaseClass={} enumValueOfStrongEnum={}",
1388 memberAccessibleFromScope, nestedClassInsideBaseClass, enumValueOfStrongEnum);
1389 int distanceToBase=0;
1390 if (nestedClassInsideBaseClass)
1396 else if (memberAccessibleFromScope &&
1403 result+=distanceToBase;
1482 result= (i==-1) ? -1 : i+2;
1497 if (name.
isEmpty())
return result;
1504 int minDistance=10000;
1507 const int maxAddrSize = 20;
1508 char ptr_str[maxAddrSize];
1509 int num =
qsnprintf(ptr_str,maxAddrSize,
"%p:",(
void *)scope);
1511 key.reserve(num+name.
length()+1);
1519 if (pTypeDef) *pTypeDef = it->second.second;
1520 return it->second.first;
1537 if (distance!=-1 && distance<minDistance)
1540 minDistance=distance;
1550 if (pTypeDef) *pTypeDef=bestMatch;
1557 g_substMap.emplace(key,std::make_pair(result,bestMatch));
1568 :
p(std::make_unique<
Private>(fileScope))
1579 bool mayBeUnlinkable,
1582 AUTO_TRACE(
"scope={} name={} mayBeUnlinkable={} mayBeHidden={}",
1583 scope?scope->
name():
QCString(), name, mayBeUnlinkable, mayBeHidden);
1586 if (scope==
nullptr ||
1604 result =
p->getResolvedTypeRec(visitedKeys,scope,name,&
p->typeDef,&
p->templateSpec,&
p->resolvedType);
1605 if (result==
nullptr)
1612 if (!mayBeUnlinkable && result && !result->
isLinkable())
1614 if (!mayBeHidden || !result->
isHidden())
1630 AUTO_TRACE(
"scope={} name={} args={} checkCV={} insideCode={}",
1631 scope?scope->
name():
QCString(), name, args, checkCV, insideCode);
1635 const Definition *result =
p->getResolvedSymbolRec(visitedKeys,scope,name,args,checkCV,insideCode,&
p->typeDef,&
p->templateSpec,&
p->resolvedType);
1648 int result =
p->isAccessibleFrom(visitedKeys,accessStack,scope,item);
1656 AUTO_TRACE(
"scope={} item={} explicitScopePart={}",
1662 int result =
p->isAccessibleFromWithExpScope(visitedKeys,visitedNamespaces,accessStack,scope,item,explicitScopePart);
1669 p->setFileScope(fileScope);
1679 return p->templateSpec;
1684 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)
const Definition * resolveSymbol(const Definition *scope, const QCString &name, const QCString &args=QCString(), bool checkCV=false, bool insideCode=false)
Find the symbool definition matching name within the scope set.
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.
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)
bool accessibleViaUsingDefinition(VisitedKeys &visitedKeys, const LinkedRefMap< const Definition > &dl, const Definition *item, const QCString &explicitScopePart="")
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)
void setFileScope(const FileDef *fileScope)
void getResolvedSymbol(VisitedKeys &visitedKeys, const Definition *scope, const Definition *d, const QCString &args, bool checkCV, bool insideCode, const QCString &explicitScopePart, bool forceCallable, int &minDistance, const Definition *&bestMatch, const MemberDef *&bestTypedef, QCString &bestTemplSpec, QCString &bestResolvedType)
const Definition * followPath(VisitedKeys &visitedKeys, const Definition *start, const QCString &path)
const Definition * getResolvedSymbolRec(VisitedKeys &visitedKeys, const Definition *scope, const QCString &n, const QCString &args, bool checkCV, bool insideCode, const MemberDef **pTypeDef, QCString *pTemplSpec, QCString *pResolvedType)
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 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 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.