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; });
184 const QCString &strippedTemplateParams,
212 const QCString &explicitScopePart=
"",
217 const QCString &explicitScopePart=
""
238 if (n.
isEmpty())
return nullptr;
243 std::unique_ptr<ArgumentList> actTemplParams;
244 if (!strippedTemplateParams.
isEmpty())
251 if (qualifierIndex!=-1)
254 explicitScopePart=name.
left(qualifierIndex);
257 name=name.
mid(qualifierIndex+2);
273 bool hasUsingStatements =
283 size_t scopeNameLen = scope->
name().
length()+1;
284 size_t nameLen = name.
length()+1;
285 size_t explicitPartLen = explicitScopePart.
length();
286 size_t fileScopeLen = hasUsingStatements ? 1+
m_fileScope->absFilePath().length() : 0;
303 if (hasUsingStatements)
315 if (std::find(visitedKeys.begin(),visitedKeys.end(),key.
str())!=std::end(visitedKeys))
323 visitedKeys.push_back(key.
str());
333 if (pTemplSpec) *pTemplSpec=pval->
templSpec;
334 if (pTypeDef) *pTypeDef=pval->
typeDef;
336 AUTO_TRACE_EXIT(
"found cached name={} templSpec={} typeDef={} resolvedTypedef={}",
348 int minDistance=10000;
354 getResolvedType(visitedKeys,scope,d,explicitScopePart,actTemplParams.get(),
355 minDistance,bestMatch,bestTypedef,bestTemplSpec,bestResolvedType);
357 if (minDistance==0)
break;
362 *pTypeDef = bestTypedef;
366 *pTemplSpec = bestTemplSpec;
370 *pResolvedType = bestResolvedType;
376 LookupInfo(bestMatch,bestTypedef,bestTemplSpec,bestResolvedType));
378 visitedKeys.erase(std::remove(visitedKeys.begin(), visitedKeys.end(), key.
str()), visitedKeys.end());
380 AUTO_TRACE_EXIT(
"found name={} templSpec={} typeDef={} resolvedTypedef={}",
400 AUTO_TRACE(
"scope={} name={} args={} checkCV={} insideCode={}",
401 scope->
name(),n,args,checkCV,insideCode);
402 if (n.
isEmpty())
return nullptr;
407 std::unique_ptr<ArgumentList> actTemplParams;
408 if (!strippedTemplateParams.
isEmpty())
415 if (qualifierIndex!=-1)
418 explicitScopePart=name.
left(qualifierIndex);
421 name=name.
mid(qualifierIndex+2);
423 AUTO_TRACE_ADD(
"qualifierIndex={} name={} explicitScopePart={} strippedTemplateParams={}",
424 qualifierIndex,name,explicitScopePart,strippedTemplateParams);
437 AUTO_TRACE_ADD(
"no symbols with name '{}' (including unspecialized)",name);
442 bool hasUsingStatements =
453 size_t nameLen = name.
length()+1;
454 size_t explicitPartLen = explicitScopePart.
length();
455 size_t strippedTemplateParamsLen = strippedTemplateParams.
length();
456 size_t fileScopeLen = hasUsingStatements ? 1+
m_fileScope->absFilePath().length() : 0;
457 size_t argsLen = args.
length()+1;
462 key.reserve(scopeNameLen+nameLen+explicitPartLen+strippedTemplateParamsLen+fileScopeLen+argsLen);
470 key+=explicitScopePart.
str();
471 key+=strippedTemplateParams.
str();
477 if (hasUsingStatements)
492 if (std::find(visitedKeys.begin(),visitedKeys.end(),key)!=std::end(visitedKeys))
499 visitedKeys.push_back(key);
508 if (pTemplSpec) *pTemplSpec=pval->
templSpec;
509 if (pTypeDef) *pTypeDef=pval->
typeDef;
511 AUTO_TRACE_EXIT(
"found cached name={} templSpec={} typeDef={} resolvedTypedef={}",
522 int minDistance=10000;
528 getResolvedSymbol(visitedKeys,scope,d,args,checkCV,insideCode,explicitScopePart,strippedTemplateParams,
false,
529 minDistance,bestMatch,bestTypedef,bestTemplSpec,bestResolvedType);
531 if (minDistance==0)
break;
536 if (bestMatch==
nullptr && args==
"()")
543 minDistance,bestMatch,bestTypedef,bestTemplSpec,bestResolvedType);
545 if (minDistance==0)
break;
551 *pTypeDef = bestTypedef;
555 *pTemplSpec = bestTemplSpec;
559 *pResolvedType = bestResolvedType;
563 LookupInfo lookupInfo(bestMatch,bestTypedef,bestTemplSpec,bestResolvedType);
568 visitedKeys.erase(std::remove(visitedKeys.begin(),visitedKeys.end(),key),visitedKeys.end());
570 AUTO_TRACE_EXIT(
"found name={} templSpec={} typeDef={} resolvedTypedef={}",
605 accessStack,scope,d,explicitScopePart);
620 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();
629 else if (distance==minDistance &&
644 AUTO_TRACE_ADD(
"found symbol={} at distance={} minDistance={}",cd->
name(),distance,minDistance);
645 minDistance=distance;
647 bestTypedef =
nullptr;
648 bestTemplSpec.
clear();
670 if (distance<minDistance)
674 minDistance=distance;
679 AUTO_TRACE_ADD(
"found symbol={} at distance={} minDistance={}",cd->
name(),distance,minDistance);
682 bestTemplSpec = spec;
683 bestResolvedType = type;
689 bestTypedef = enumType;
698 bestTemplSpec = spec;
699 bestResolvedType = type;
706 bestTemplSpec.
clear();
707 bestResolvedType.
clear();
722 if (distance<minDistance)
724 AUTO_TRACE_ADD(
"found enum={} at distance={} minDistance={}",md->
name(),distance,minDistance);
725 minDistance=distance;
740 bestMatch?bestMatch->
name():
QCString(
"<none>"),bestResolvedType);
752 const QCString &strippedTemplateParams,
766 int distance =
isAccessibleFromWithExpScope(visitedKeys,visitedNamespaces,accessStack,scope,d,explicitScopePart+strippedTemplateParams);
767 if (distance==-1 && !strippedTemplateParams.
isEmpty())
783 if (distance<minDistance)
785 AUTO_TRACE_ADD(
"found symbol={} at distance={} minDistance={}",d->
name(),distance,minDistance);
786 minDistance=distance;
788 bestTypedef =
nullptr;
789 bestTemplSpec.
clear();
792 else if (distance==minDistance &&
807 AUTO_TRACE_ADD(
"found symbol={} at distance={} minDistance={}",d->
name(),distance,minDistance);
808 minDistance=distance;
810 bestTypedef =
nullptr;
811 bestTemplSpec.
clear();
847 if (match && distance<minDistance)
849 AUTO_TRACE_ADD(
"found symbol={} at distance={} minDistance={}",md->
name(),distance,minDistance);
850 minDistance=distance;
861 if (distance<minDistance)
863 AUTO_TRACE_ADD(
"found symbol={} at distance={} minDistance={}",d->
name(),distance,minDistance);
864 minDistance=distance;
866 bestTypedef =
nullptr;
867 bestTemplSpec.
clear();
868 bestResolvedType.
clear();
877 bestMatch?bestMatch->
name():
QCString(
"<none>"),bestResolvedType);
917 actTemplParams && !actTemplParams->
empty())
923 int tl=
static_cast<int>(type.
length());
925 while (ip>=0 && (type.
at(ip)==
'*' || type.
at(ip)==
'&' || type.
at(ip)==
' '))
929 type=type.
left(ip+1);
935 tl=
static_cast<int>(type.
length());
936 while (sp<tl && type.
at(sp)==
' ') sp++;
939 &memTypeDef,
nullptr,pResolvedType);
941 if (memTypeDef && memTypeDef->
isTypedef())
947 else if (memTypeDef && memTypeDef->
isEnumerate() && pMemType)
949 *pMemType = memTypeDef;
956 int i=type.
find(
'<');
959 if (pTemplSpec) *pTemplSpec = type.
mid(i);
967 i=
static_cast<int>(type.
length());
971 if (pTemplSpec) *pTemplSpec = type.
mid(i);
984 if (sp>0) pResolvedType->
prepend(typedefValue.
left(sp));
985 if (ip<tl-1) pResolvedType->
append(typedefValue.
right(tl-ip-1));
989 *pResolvedType = typedefValue;
1003 pTemplSpec ? *pTemplSpec :
QCString(),
1004 pResolvedType ? *pResolvedType :
QCString()
1013 pTemplSpec ? *pTemplSpec :
"<nullptr>",
1014 pResolvedType ? *pResolvedType :
"<nullptr>"
1024 return isParentScope(
parent->getOuterScope(),item);
1037 AUTO_TRACE(
"scope={} item={} explictScopePart={}",
1039 if (explicitScopePart.
isEmpty())
1061 bool nestedClassInsideBaseClass =
1067 bool enumValueWithinEnum =
1072 if (itemScope==newScope)
1076 else if (nestedClassInsideBaseClass)
1089 else if (enumValueWithinEnum)
1099 visitedNamespaces.emplace(newScope->
name().
str(),newScope);
1115 if (visitedNamespaces.find(nd->name().str())==visitedNamespaces.end())
1141 result = (i==-1) ? -1 : i+2;
1174 result= (i==-1) ? -1 : i+2;
1225 if (namespaceMember && namespaceMember->
isEnumerate())
1227 next = namespaceMember;
1264 (
toFileDef(current))->getUsedDefinitions(),qualScopePart);
1267 if (current==
nullptr)
break;
1283 for (
const auto &d : dl)
1285 if (d->localName()==localName)
1301 AUTO_TRACE(
"item={} explicitScopePart={} level={}",item?item->
name():
QCString(), explicitScopePart, level);
1302 for (
const auto &und : nl)
1314 if (!und->getUsedNamespaces().empty() && std::find(visitedNamespaces.begin(),visitedNamespaces.end(),key.
str())==std::end(visitedNamespaces))
1316 visitedNamespaces.push_back(key.
str());
1337 for (
const auto &ud : dl)
1356 AUTO_TRACE(
"scope={} item={} item.definitionType={}",
1377 if ((itemIsMember || itemIsClass) &&
1379 (itemScope && itemScope->
name().
startsWith(
"anonymous_namespace{"))
1387 else if (itemIsClass)
1394 bool memberAccessibleFromScope =
1400 bool nestedClassInsideBaseClass =
1406 bool enumValueOfStrongEnum =
1414 if (itemScope==scope || memberAccessibleFromScope || nestedClassInsideBaseClass || enumValueOfStrongEnum)
1416 AUTO_TRACE_ADD(
"memberAccessibleFromScope={} nestedClassInsideBaseClass={} enumValueOfStrongEnum={}",
1417 memberAccessibleFromScope, nestedClassInsideBaseClass, enumValueOfStrongEnum);
1418 int distanceToBase=0;
1419 if (nestedClassInsideBaseClass)
1425 else if (memberAccessibleFromScope &&
1432 result+=distanceToBase;
1511 result= (i==-1) ? -1 : i+2;
1526 if (name.
isEmpty())
return result;
1533 int minDistance=10000;
1536 const int maxAddrSize = 20;
1537 char ptr_str[maxAddrSize];
1538 int num =
qsnprintf(ptr_str,maxAddrSize,
"%p:",(
void *)scope);
1540 key.reserve(num+name.
length()+1);
1548 if (pTypeDef) *pTypeDef = it->second.second;
1549 return it->second.first;
1566 if (distance!=-1 && distance<minDistance)
1569 minDistance=distance;
1579 if (pTypeDef) *pTypeDef=bestMatch;
1586 g_substMap.emplace(key,std::make_pair(result,bestMatch));
1597 :
p(std::make_unique<
Private>(fileScope))
1608 bool mayBeUnlinkable,
1611 AUTO_TRACE(
"scope={} name={} mayBeUnlinkable={} mayBeHidden={}",
1612 scope?scope->
name():
QCString(), name, mayBeUnlinkable, mayBeHidden);
1617 if (scope==
nullptr ||
1637 result =
p->getResolvedTypeRec(visitedKeys,scope,lookupName,&
p->typeDef,&
p->templateSpec,&
p->resolvedType);
1638 if (result==
nullptr)
1645 if (!mayBeUnlinkable && result && !result->
isLinkable())
1647 if (!mayBeHidden || !result->
isHidden())
1663 AUTO_TRACE(
"scope={} name={} args={} checkCV={} insideCode={}",
1664 scope?scope->
name():
QCString(), name, args, checkCV, insideCode);
1668 const Definition *result =
p->getResolvedSymbolRec(visitedKeys,scope,name,args,checkCV,insideCode,&
p->typeDef,&
p->templateSpec,&
p->resolvedType);
1681 int result =
p->isAccessibleFrom(visitedKeys,accessStack,scope,item);
1689 AUTO_TRACE(
"scope={} item={} explicitScopePart={}",
1695 int result =
p->isAccessibleFromWithExpScope(visitedKeys,visitedNamespaces,accessStack,scope,item,explicitScopePart);
1702 p->setFileScope(fileScope);
1712 return p->templateSpec;
1717 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)
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="")
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)
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 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.