mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
C++, remove symbol hax for template params
This commit is contained in:
parent
470bac3d1c
commit
c823ffbcf1
@ -2065,23 +2065,30 @@ class ASTNamespace(ASTBase):
|
|||||||
|
|
||||||
|
|
||||||
class Symbol(object):
|
class Symbol(object):
|
||||||
def __init__(self, parent, identifier,
|
def _assert_invariants(self):
|
||||||
templateParams, templateArgs, declaration):
|
if not self.parent:
|
||||||
if not parent:
|
|
||||||
# parent == None means global scope, so declaration means a parent
|
# parent == None means global scope, so declaration means a parent
|
||||||
assert not identifier
|
assert not self.identifier
|
||||||
assert not templateParams
|
assert not self.templateParams
|
||||||
assert not templateArgs
|
assert not self.templateArgs
|
||||||
assert not declaration
|
assert not self.declaration
|
||||||
|
assert not self.docname
|
||||||
else:
|
else:
|
||||||
if not identifier:
|
if not self.identifier:
|
||||||
# in case it's an operator
|
# in case it's an operator
|
||||||
assert declaration
|
assert self.declaration
|
||||||
|
if self.declaration:
|
||||||
|
assert self.docname
|
||||||
|
|
||||||
|
def __init__(self, parent, identifier,
|
||||||
|
templateParams, templateArgs, declaration, docname):
|
||||||
self.parent = parent
|
self.parent = parent
|
||||||
self.identifier = identifier
|
self.identifier = identifier
|
||||||
self.templateParams = templateParams # template<templateParams>
|
self.templateParams = templateParams # template<templateParams>
|
||||||
self.templateArgs = templateArgs # identifier<templateArgs>
|
self.templateArgs = templateArgs # identifier<templateArgs>
|
||||||
self.declaration = declaration
|
self.declaration = declaration
|
||||||
|
self.docname = docname
|
||||||
|
self._assert_invariants()
|
||||||
|
|
||||||
self.children = []
|
self.children = []
|
||||||
if self.parent:
|
if self.parent:
|
||||||
@ -2097,14 +2104,13 @@ class Symbol(object):
|
|||||||
decl = ASTDeclaration('templateParam', None, None, p)
|
decl = ASTDeclaration('templateParam', None, None, p)
|
||||||
nne = ASTNestedNameElement(p.get_identifier(), None)
|
nne = ASTNestedNameElement(p.get_identifier(), None)
|
||||||
nn = ASTNestedName([nne], rooted=False)
|
nn = ASTNestedName([nne], rooted=False)
|
||||||
self._add_symbols(nn, [], decl)
|
self._add_symbols(nn, [], decl, docname)
|
||||||
|
|
||||||
def get_all_symbols(self):
|
def get_all_symbols(self):
|
||||||
# assumed to be in post-order
|
yield self
|
||||||
for sChild in self.children:
|
for sChild in self.children:
|
||||||
for s in sChild.get_all_symbols():
|
for s in sChild.get_all_symbols():
|
||||||
yield s
|
yield s
|
||||||
yield self
|
|
||||||
|
|
||||||
def get_lookup_key(self):
|
def get_lookup_key(self):
|
||||||
if not self.parent:
|
if not self.parent:
|
||||||
@ -2166,7 +2172,7 @@ class Symbol(object):
|
|||||||
return s
|
return s
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def _add_symbols(self, nestedName, templateDecls, declaration):
|
def _add_symbols(self, nestedName, templateDecls, declaration, docname):
|
||||||
# This condition should be checked at the parser level.
|
# This condition should be checked at the parser level.
|
||||||
# Each template argument list must have a template parameter list.
|
# Each template argument list must have a template parameter list.
|
||||||
# But to declare a template there must be an additional template parameter list.
|
# But to declare a template there must be an additional template parameter list.
|
||||||
@ -2180,9 +2186,29 @@ class Symbol(object):
|
|||||||
declarationScope = parentSymbol
|
declarationScope = parentSymbol
|
||||||
names = nestedName.names
|
names = nestedName.names
|
||||||
iTemplateDecl = 0
|
iTemplateDecl = 0
|
||||||
for iName in range(len(names)):
|
for name in names[:-1]:
|
||||||
name = names[iName]
|
# there shouldn't be anything inside an operator
|
||||||
if iName + 1 == len(names):
|
# (other than template parameters, which are not added this way, right?)
|
||||||
|
assert not name.is_operator()
|
||||||
|
identifier = name.identifier
|
||||||
|
templateArgs = name.templateArgs
|
||||||
|
if templateArgs:
|
||||||
|
assert iTemplateDecl < len(templateDecls)
|
||||||
|
templateParams = templateDecls[iTemplateDecl]
|
||||||
|
iTemplateDecl += 1
|
||||||
|
else:
|
||||||
|
templateParams = None
|
||||||
|
symbol = parentSymbol._find_named_symbol(identifier,
|
||||||
|
templateParams,
|
||||||
|
templateArgs,
|
||||||
|
operator=None)
|
||||||
|
if symbol is None:
|
||||||
|
symbol = Symbol(parent=parentSymbol, identifier=identifier,
|
||||||
|
templateParams=templateParams,
|
||||||
|
templateArgs=templateArgs, declaration=None,
|
||||||
|
docname=None)
|
||||||
|
parentSymbol = symbol
|
||||||
|
name = names[-1]
|
||||||
if name.is_operator():
|
if name.is_operator():
|
||||||
identifier = None
|
identifier = None
|
||||||
templateArgs = None
|
templateArgs = None
|
||||||
@ -2215,8 +2241,10 @@ class Symbol(object):
|
|||||||
# .. namespace:: nullptr
|
# .. namespace:: nullptr
|
||||||
# .. class:: Test
|
# .. class:: Test
|
||||||
symbol.declaration = declaration
|
symbol.declaration = declaration
|
||||||
|
symbol.docname = docname
|
||||||
declaration.symbol = symbol
|
declaration.symbol = symbol
|
||||||
declaration.declarationScope = declarationScope
|
declaration.declarationScope = declarationScope
|
||||||
|
symbol._assert_invariants()
|
||||||
return symbol
|
return symbol
|
||||||
# it may simply be a functin overload
|
# it may simply be a functin overload
|
||||||
# TODO: it could be a duplicate but let's just insert anyway
|
# TODO: it could be a duplicate but let's just insert anyway
|
||||||
@ -2224,52 +2252,34 @@ class Symbol(object):
|
|||||||
symbol = Symbol(parent=parentSymbol, identifier=identifier,
|
symbol = Symbol(parent=parentSymbol, identifier=identifier,
|
||||||
templateParams=templateParams,
|
templateParams=templateParams,
|
||||||
templateArgs=templateArgs,
|
templateArgs=templateArgs,
|
||||||
declaration=declaration)
|
declaration=declaration,
|
||||||
|
docname=docname)
|
||||||
declaration.declarationScope = declarationScope
|
declaration.declarationScope = declarationScope
|
||||||
else:
|
else:
|
||||||
symbol = Symbol(parent=parentSymbol, identifier=identifier,
|
symbol = Symbol(parent=parentSymbol, identifier=identifier,
|
||||||
templateParams=templateParams,
|
templateParams=templateParams,
|
||||||
templateArgs=templateArgs,
|
templateArgs=templateArgs,
|
||||||
declaration=declaration)
|
declaration=declaration,
|
||||||
|
docname=docname)
|
||||||
if declaration:
|
if declaration:
|
||||||
declaration.declarationScope = declarationScope
|
declaration.declarationScope = declarationScope
|
||||||
return symbol
|
return symbol
|
||||||
else:
|
|
||||||
# there shouldn't be anything inside an operator
|
|
||||||
assert not name.is_operator()
|
|
||||||
identifier = name.identifier
|
|
||||||
templateArgs = name.templateArgs
|
|
||||||
if templateArgs:
|
|
||||||
assert iTemplateDecl < len(templateDecls)
|
|
||||||
templateParams = templateDecls[iTemplateDecl]
|
|
||||||
iTemplateDecl += 1
|
|
||||||
else:
|
|
||||||
templateParams = None
|
|
||||||
symbol = parentSymbol._find_named_symbol(identifier,
|
|
||||||
templateParams,
|
|
||||||
templateArgs,
|
|
||||||
operator=None)
|
|
||||||
if symbol is None:
|
|
||||||
symbol = Symbol(parent=parentSymbol, identifier=identifier,
|
|
||||||
templateParams=templateParams,
|
|
||||||
templateArgs=templateArgs, declaration=None)
|
|
||||||
parentSymbol = symbol
|
|
||||||
assert False # should have returned in the loop
|
|
||||||
|
|
||||||
def add_name(self, nestedName, templatePrefix=None):
|
def add_name(self, nestedName, templatePrefix=None):
|
||||||
if templatePrefix:
|
if templatePrefix:
|
||||||
templateDecls = templatePrefix.templates
|
templateDecls = templatePrefix.templates
|
||||||
else:
|
else:
|
||||||
templateDecls = []
|
templateDecls = []
|
||||||
return self._add_symbols(nestedName, templateDecls, declaration=None)
|
return self._add_symbols(nestedName, templateDecls,
|
||||||
|
declaration=None, docname=None)
|
||||||
|
|
||||||
def add_declaration(self, declaration):
|
def add_declaration(self, declaration, docname):
|
||||||
nestedName = declaration.name
|
nestedName = declaration.name
|
||||||
if declaration.templatePrefix:
|
if declaration.templatePrefix:
|
||||||
templateDecls = declaration.templatePrefix.templates
|
templateDecls = declaration.templatePrefix.templates
|
||||||
else:
|
else:
|
||||||
templateDecls = []
|
templateDecls = []
|
||||||
return self._add_symbols(nestedName, templateDecls, declaration)
|
return self._add_symbols(nestedName, templateDecls, declaration, docname)
|
||||||
|
|
||||||
def find_identifier(self, identifier):
|
def find_identifier(self, identifier):
|
||||||
for s in self.children:
|
for s in self.children:
|
||||||
@ -3307,7 +3317,8 @@ class CPPObject(ObjectDescription):
|
|||||||
return
|
return
|
||||||
Symbol(parent=targetSymbol, identifier=symbol.identifier,
|
Symbol(parent=targetSymbol, identifier=symbol.identifier,
|
||||||
templateParams=None, templateArgs=None,
|
templateParams=None, templateArgs=None,
|
||||||
declaration=symbol.declaration.clone())
|
declaration=symbol.declaration.clone(),
|
||||||
|
docname=self.env.docname)
|
||||||
|
|
||||||
def add_target_and_index(self, ast, sig, signode):
|
def add_target_and_index(self, ast, sig, signode):
|
||||||
# general note: name must be lstrip(':')'ed, to remove "::"
|
# general note: name must be lstrip(':')'ed, to remove "::"
|
||||||
@ -3368,9 +3379,8 @@ class CPPObject(ObjectDescription):
|
|||||||
symbol = parentSymbol.add_name(name)
|
symbol = parentSymbol.add_name(name)
|
||||||
self.env.ref_context['cpp:lastSymbol'] = symbol
|
self.env.ref_context['cpp:lastSymbol'] = symbol
|
||||||
raise ValueError
|
raise ValueError
|
||||||
symbol = parentSymbol.add_declaration(ast)
|
symbol = parentSymbol.add_declaration(ast, docname=self.env.docname)
|
||||||
self.env.ref_context['cpp:lastSymbol'] = symbol
|
self.env.ref_context['cpp:lastSymbol'] = symbol
|
||||||
symbol.docname = self.env.docname
|
|
||||||
|
|
||||||
if ast.objectType == 'enumerator':
|
if ast.objectType == 'enumerator':
|
||||||
self._add_enumerator_to_parent(ast)
|
self._add_enumerator_to_parent(ast)
|
||||||
@ -3628,7 +3638,7 @@ class CPPDomain(Domain):
|
|||||||
'enumerator': CPPXRefRole()
|
'enumerator': CPPXRefRole()
|
||||||
}
|
}
|
||||||
initial_data = {
|
initial_data = {
|
||||||
'rootSymbol': Symbol(None, None, None, None, None),
|
'rootSymbol': Symbol(None, None, None, None, None, None),
|
||||||
'names': {} # full name for indexing -> docname
|
'names': {} # full name for indexing -> docname
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -3637,14 +3647,7 @@ class CPPDomain(Domain):
|
|||||||
for symbol in rootSymbol.get_all_symbols():
|
for symbol in rootSymbol.get_all_symbols():
|
||||||
if not symbol.declaration:
|
if not symbol.declaration:
|
||||||
continue
|
continue
|
||||||
try:
|
if symbol.docname != docname:
|
||||||
sDocname = symbol.docname
|
|
||||||
except AttributeError:
|
|
||||||
# it's a template parameter
|
|
||||||
# the symbols are yielded in post-order, so this should be fine
|
|
||||||
assert symbol.parent
|
|
||||||
sDocname = symbol.parent.docname
|
|
||||||
if sDocname != docname:
|
|
||||||
continue
|
continue
|
||||||
symbol.declaration = None
|
symbol.declaration = None
|
||||||
symbol.docname = None
|
symbol.docname = None
|
||||||
@ -3699,12 +3702,8 @@ class CPPDomain(Domain):
|
|||||||
declaration = s.declaration
|
declaration = s.declaration
|
||||||
fullNestedName = s.get_full_nested_name()
|
fullNestedName = s.get_full_nested_name()
|
||||||
name = text_type(fullNestedName).lstrip(':')
|
name = text_type(fullNestedName).lstrip(':')
|
||||||
try:
|
|
||||||
docname = s.docname
|
docname = s.docname
|
||||||
except AttributeError:
|
assert docname
|
||||||
# it's a template parameter
|
|
||||||
assert s.parent
|
|
||||||
docname = s.parent.docname
|
|
||||||
return make_refnode(builder, fromdocname, docname,
|
return make_refnode(builder, fromdocname, docname,
|
||||||
declaration.get_newest_id(), contnode, name
|
declaration.get_newest_id(), contnode, name
|
||||||
), declaration.objectType
|
), declaration.objectType
|
||||||
@ -3726,13 +3725,11 @@ class CPPDomain(Domain):
|
|||||||
def get_objects(self):
|
def get_objects(self):
|
||||||
rootSymbol = self.data['rootSymbol']
|
rootSymbol = self.data['rootSymbol']
|
||||||
for symbol in rootSymbol.get_all_symbols():
|
for symbol in rootSymbol.get_all_symbols():
|
||||||
if not symbol.declaration:
|
if symbol.declaration is None:
|
||||||
continue
|
continue
|
||||||
|
assert symbol.docname
|
||||||
name = text_type(symbol.get_full_nested_name()).lstrip(':')
|
name = text_type(symbol.get_full_nested_name()).lstrip(':')
|
||||||
objectType = symbol.declaration.objectType
|
objectType = symbol.declaration.objectType
|
||||||
try:
|
|
||||||
docname = symbol.docname
|
docname = symbol.docname
|
||||||
except AttributeError:
|
|
||||||
continue
|
|
||||||
newestId = symbol.declaration.get_newest_id()
|
newestId = symbol.declaration.get_newest_id()
|
||||||
yield (name, name, objectType, docname, newestId, 1)
|
yield (name, name, objectType, docname, newestId, 1)
|
||||||
|
@ -46,8 +46,8 @@ def check(name, input, idv1output=None, idv2output=None, output=None):
|
|||||||
print("Result: ", res)
|
print("Result: ", res)
|
||||||
print("Expected: ", output)
|
print("Expected: ", output)
|
||||||
raise DefinitionError("")
|
raise DefinitionError("")
|
||||||
rootSymbol = Symbol(None, None, None, None, None)
|
rootSymbol = Symbol(None, None, None, None, None, None)
|
||||||
symbol = rootSymbol.add_declaration(ast)
|
symbol = rootSymbol.add_declaration(ast, docname="Test")
|
||||||
ast.describe_signature([], 'lastIsName', symbol)
|
ast.describe_signature([], 'lastIsName', symbol)
|
||||||
|
|
||||||
if idv2output:
|
if idv2output:
|
||||||
|
Loading…
Reference in New Issue
Block a user