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; });
185 const QCString &strippedTemplateParams,
213 const QCString &explicitScopePart=
"",
218 const QCString &explicitScopePart=
""
239 if (n.
isEmpty())
return nullptr;
244 std::unique_ptr<ArgumentList> actTemplParams;
245 if (!strippedTemplateParams.
isEmpty())
252 if (qualifierIndex!=-1)
255 explicitScopePart=name.
left(qualifierIndex);
258 name=name.
mid(qualifierIndex+2);
274 bool hasUsingStatements =
284 size_t scopeNameLen = scope->
name().
length()+1;
285 size_t nameLen = name.
length()+1;
286 size_t explicitPartLen = explicitScopePart.
length();
287 size_t fileScopeLen = hasUsingStatements ? 1+
m_fileScope->absFilePath().length() : 0;
304 if (hasUsingStatements)
316 if (std::find(visitedKeys.begin(),visitedKeys.end(),key.
str())!=std::end(visitedKeys))
324 visitedKeys.push_back(key.
str());
334 if (pTemplSpec) *pTemplSpec=pval->
templSpec;
335 if (pTypeDef) *pTypeDef=pval->
typeDef;
337 AUTO_TRACE_EXIT(
"found cached name={} templSpec={} typeDef={} resolvedTypedef={}",
349 int minDistance=10000;
355 getResolvedType(visitedKeys,scope,d,explicitScopePart,actTemplParams.get(),
356 minDistance,bestMatch,bestTypedef,bestTemplSpec,bestResolvedType);
358 if (minDistance==0)
break;
363 *pTypeDef = bestTypedef;
367 *pTemplSpec = bestTemplSpec;
371 *pResolvedType = bestResolvedType;
377 LookupInfo(bestMatch,bestTypedef,bestTemplSpec,bestResolvedType));
379 visitedKeys.erase(std::remove(visitedKeys.begin(), visitedKeys.end(), key.
str()), visitedKeys.end());
381 AUTO_TRACE_EXIT(
"found name={} templSpec={} typeDef={} resolvedTypedef={}",
402 AUTO_TRACE(
"scope={} name={} args={} checkCV={} insideCode={}",
403 scope->
name(),n,args,checkCV,insideCode);
404 if (n.
isEmpty())
return nullptr;
409 std::unique_ptr<ArgumentList> actTemplParams;
410 if (!strippedTemplateParams.
isEmpty())
417 if (qualifierIndex!=-1)
420 explicitScopePart=name.
left(qualifierIndex);
423 name=name.
mid(qualifierIndex+2);
425 AUTO_TRACE_ADD(
"qualifierIndex={} name={} explicitScopePart={} strippedTemplateParams={}",
426 qualifierIndex,name,explicitScopePart,strippedTemplateParams);
439 AUTO_TRACE_ADD(
"no symbols with name '{}' (including unspecialized)",name);
444 bool hasUsingStatements =
455 size_t nameLen = name.
length()+1;
456 size_t explicitPartLen = explicitScopePart.
length();
457 size_t strippedTemplateParamsLen = strippedTemplateParams.
length();
458 size_t fileScopeLen = hasUsingStatements ? 1+
m_fileScope->absFilePath().length() : 0;
459 size_t argsLen = args.
length()+1;
464 key.reserve(scopeNameLen+nameLen+explicitPartLen+strippedTemplateParamsLen+fileScopeLen+argsLen);
472 key+=explicitScopePart.
str();
473 key+=strippedTemplateParams.
str();
479 if (hasUsingStatements)
494 if (std::find(visitedKeys.begin(),visitedKeys.end(),key)!=std::end(visitedKeys))
501 visitedKeys.push_back(key);
510 if (pTemplSpec) *pTemplSpec=pval->
templSpec;
511 if (pTypeDef) *pTypeDef=pval->
typeDef;
513 AUTO_TRACE_EXIT(
"found cached name={} templSpec={} typeDef={} resolvedTypedef={}",
524 int minDistance=10000;
531 d->isLinkableInProject() ||
538 getResolvedSymbol(visitedKeys,scope,d,args,checkCV,insideCode,explicitScopePart,strippedTemplateParams,
false,
539 minDistance,bestMatch,bestTypedef,bestTemplSpec,bestResolvedType);
541 if (minDistance==0)
break;
546 if (bestMatch==
nullptr && args==
"()")
553 minDistance,bestMatch,bestTypedef,bestTemplSpec,bestResolvedType);
555 if (minDistance==0)
break;
561 *pTypeDef = bestTypedef;
565 *pTemplSpec = bestTemplSpec;
569 *pResolvedType = bestResolvedType;
573 LookupInfo lookupInfo(bestMatch,bestTypedef,bestTemplSpec,bestResolvedType);
578 visitedKeys.erase(std::remove(visitedKeys.begin(),visitedKeys.end(),key),visitedKeys.end());
580 AUTO_TRACE_EXIT(
"found name={} templSpec={} typeDef={} resolvedTypedef={}",
615 accessStack,scope,d,explicitScopePart);
630 if (distance<minDistance)
632 AUTO_TRACE_ADD(
"found symbol={} at distance={} minDistance={}",cd->
name(),distance,minDistance);
633 minDistance=distance;
635 bestTypedef =
nullptr;
636 bestTemplSpec.
clear();
639 else if (distance==minDistance &&
654 AUTO_TRACE_ADD(
"found symbol={} at distance={} minDistance={}",cd->
name(),distance,minDistance);
655 minDistance=distance;
657 bestTypedef =
nullptr;
658 bestTemplSpec.
clear();
680 if (distance<minDistance)
684 minDistance=distance;
689 AUTO_TRACE_ADD(
"found symbol={} at distance={} minDistance={}",cd->
name(),distance,minDistance);
692 bestTemplSpec = spec;
693 bestResolvedType = type;
699 bestTypedef = enumType;
708 bestTemplSpec = spec;
709 bestResolvedType = type;
716 bestTemplSpec.
clear();
717 bestResolvedType.
clear();
732 if (distance<minDistance)
734 AUTO_TRACE_ADD(
"found enum={} at distance={} minDistance={}",md->
name(),distance,minDistance);
735 minDistance=distance;
750 bestMatch?bestMatch->
name():
QCString(
"<none>"),bestResolvedType);
762 const QCString &strippedTemplateParams,
776 int distance =
isAccessibleFromWithExpScope(visitedKeys,visitedNamespaces,accessStack,scope,d,explicitScopePart+strippedTemplateParams);
777 if (distance==-1 && !strippedTemplateParams.
isEmpty())
793 if (distance<minDistance)
795 AUTO_TRACE_ADD(
"found symbol={} at distance={} minDistance={}",d->
name(),distance,minDistance);
796 minDistance=distance;
798 bestTypedef =
nullptr;
799 bestTemplSpec.
clear();
802 else if (distance==minDistance &&
817 AUTO_TRACE_ADD(
"found symbol={} at distance={} minDistance={}",d->
name(),distance,minDistance);
818 minDistance=distance;
820 bestTypedef =
nullptr;
821 bestTemplSpec.
clear();
857 if (match && distance<minDistance)
859 AUTO_TRACE_ADD(
"found symbol={} at distance={} minDistance={}",md->
name(),distance,minDistance);
860 minDistance=distance;
871 if (distance<minDistance)
873 AUTO_TRACE_ADD(
"found symbol={} at distance={} minDistance={}",d->
name(),distance,minDistance);
874 minDistance=distance;
876 bestTypedef =
nullptr;
877 bestTemplSpec.
clear();
878 bestResolvedType.
clear();
887 bestMatch?bestMatch->
name():
QCString(
"<none>"),bestResolvedType);
927 actTemplParams && !actTemplParams->
empty())
933 int tl=
static_cast<int>(type.
length());
935 while (ip>=0 && (type.
at(ip)==
'*' || type.
at(ip)==
'&' || type.
at(ip)==
' '))
939 type=type.
left(ip+1);
945 tl=
static_cast<int>(type.
length());
946 while (sp<tl && type.
at(sp)==
' ') sp++;
949 &memTypeDef,
nullptr,pResolvedType);
951 if (memTypeDef && memTypeDef->
isTypedef())
957 else if (memTypeDef && memTypeDef->
isEnumerate() && pMemType)
959 *pMemType = memTypeDef;
966 int i=type.
find(
'<');
969 if (pTemplSpec) *pTemplSpec = type.
mid(i);
977 i=
static_cast<int>(type.
length());
981 if (pTemplSpec) *pTemplSpec = type.
mid(i);
994 if (sp>0) pResolvedType->
prepend(typedefValue.
left(sp));
995 if (ip<tl-1) pResolvedType->
append(typedefValue.
right(tl-ip-1));
999 *pResolvedType = typedefValue;
1013 pTemplSpec ? *pTemplSpec :
QCString(),
1014 pResolvedType ? *pResolvedType :
QCString()
1023 pTemplSpec ? *pTemplSpec :
"<nullptr>",
1024 pResolvedType ? *pResolvedType :
"<nullptr>"
1034 return isParentScope(
parent->getOuterScope(),item);
1047 AUTO_TRACE(
"scope={} item={} explictScopePart={}",
1049 if (explicitScopePart.
isEmpty())
1071 bool nestedClassInsideBaseClass =
1077 bool enumValueWithinEnum =
1082 if (itemScope==newScope)
1086 else if (nestedClassInsideBaseClass)
1099 else if (enumValueWithinEnum)
1109 visitedNamespaces.emplace(newScope->
name().
str(),newScope);
1125 if (visitedNamespaces.find(nd->name().str())==visitedNamespaces.end())
1151 result = (i==-1) ? -1 : i+2;
1184 result= (i==-1) ? -1 : i+2;
1235 if (namespaceMember && namespaceMember->
isEnumerate())
1237 next = namespaceMember;
1274 (
toFileDef(current))->getUsedDefinitions(),qualScopePart);
1277 if (current==
nullptr)
break;
1293 for (
const auto &d : dl)
1295 if (d->localName()==localName)
1311 AUTO_TRACE(
"item={} explicitScopePart={} level={}",item?item->
name():
QCString(), explicitScopePart, level);
1312 for (
const auto &und : nl)
1324 if (!und->getUsedNamespaces().empty() && std::find(visitedNamespaces.begin(),visitedNamespaces.end(),key.
str())==std::end(visitedNamespaces))
1326 visitedNamespaces.push_back(key.
str());
1347 for (
const auto &ud : dl)
1366 AUTO_TRACE(
"scope={} item={} item.definitionType={}",
1387 if ((itemIsMember || itemIsClass) &&
1389 (itemScope && itemScope->
name().
startsWith(
"anonymous_namespace{"))
1397 else if (itemIsClass)
1404 bool memberAccessibleFromScope =
1410 bool nestedClassInsideBaseClass =
1416 bool enumValueOfStrongEnum =
1424 if (itemScope==scope || memberAccessibleFromScope || nestedClassInsideBaseClass || enumValueOfStrongEnum)
1426 AUTO_TRACE_ADD(
"memberAccessibleFromScope={} nestedClassInsideBaseClass={} enumValueOfStrongEnum={}",
1427 memberAccessibleFromScope, nestedClassInsideBaseClass, enumValueOfStrongEnum);
1428 int distanceToBase=0;
1429 if (nestedClassInsideBaseClass)
1435 else if (memberAccessibleFromScope &&
1442 result+=distanceToBase;
1521 result= (i==-1) ? -1 : i+2;
1536 if (name.
isEmpty())
return result;
1543 int minDistance=10000;
1546 const int maxAddrSize = 20;
1547 char ptr_str[maxAddrSize];
1548 int num =
qsnprintf(ptr_str,maxAddrSize,
"%p:",(
void *)scope);
1550 key.reserve(num+name.
length()+1);
1558 if (pTypeDef) *pTypeDef = it->second.second;
1559 return it->second.first;
1576 if (distance!=-1 && distance<minDistance)
1579 minDistance=distance;
1589 if (pTypeDef) *pTypeDef=bestMatch;
1596 g_substMap.emplace(key,std::make_pair(result,bestMatch));
1607 :
p(std::make_unique<
Private>(fileScope))
1618 bool mayBeUnlinkable,
1621 AUTO_TRACE(
"scope={} name={} mayBeUnlinkable={} mayBeHidden={}",
1622 scope?scope->
name():
QCString(), name, mayBeUnlinkable, mayBeHidden);
1625 auto lang = scope ? scope->
getLanguage() : SrcLangExt::Cpp;
1627 if (scope==
nullptr ||
1632 ((lang==SrcLangExt::Java || lang==SrcLangExt::CSharp) &&
QCString(name).find(
"::")!=-1)
1647 result =
p->getResolvedTypeRec(visitedKeys,scope,lookupName,&
p->typeDef,&
p->templateSpec,&
p->resolvedType);
1648 if (result==
nullptr)
1655 if (!mayBeUnlinkable && result && !result->
isLinkable())
1657 if (!mayBeHidden || !result->
isHidden())
1674 AUTO_TRACE(
"scope={} name={} args={} checkCV={} insideCode={}",
1675 scope?scope->
name():
QCString(), name, args, checkCV, insideCode);
1679 const Definition *result =
p->getResolvedSymbolRec(visitedKeys,scope,name,args,checkCV,insideCode,onlyLinkable,&
p->typeDef,&
p->templateSpec,&
p->resolvedType);
1692 int result =
p->isAccessibleFrom(visitedKeys,accessStack,scope,item);
1700 AUTO_TRACE(
"scope={} item={} explicitScopePart={}",
1706 int result =
p->isAccessibleFromWithExpScope(visitedKeys,visitedNamespaces,accessStack,scope,item,explicitScopePart);
1713 p->setFileScope(fileScope);
1723 return p->templateSpec;
1728 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)
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.