diff options
Diffstat (limited to 'sphinx/domains/cpp.py')
| -rw-r--r-- | sphinx/domains/cpp.py | 1342 |
1 files changed, 798 insertions, 544 deletions
diff --git a/sphinx/domains/cpp.py b/sphinx/domains/cpp.py index 6740638ae..ff5487598 100644 --- a/sphinx/domains/cpp.py +++ b/sphinx/domains/cpp.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- """ sphinx.domains.cpp ~~~~~~~~~~~~~~~~~~ @@ -10,23 +9,25 @@ """ import re +import warnings from copy import deepcopy from docutils import nodes, utils from docutils.parsers.rst import directives -from six import iteritems, text_type from sphinx import addnodes +from sphinx.deprecation import RemovedInSphinx40Warning from sphinx.directives import ObjectDescription from sphinx.domains import Domain, ObjType from sphinx.environment import NoUri from sphinx.locale import _, __ from sphinx.roles import XRefRole +from sphinx.transforms import SphinxTransform +from sphinx.transforms.post_transforms import ReferencesResolver from sphinx.util import logging from sphinx.util.docfields import Field, GroupedField from sphinx.util.docutils import SphinxDirective from sphinx.util.nodes import make_refnode -from sphinx.util.pycompat import UnicodeMixin if False: @@ -72,7 +73,7 @@ logger = logging.getLogger(__name__) Grammar ---------------------------------------------------------------------------- - See http://www.nongnu.org/hcb/ for the grammar, + See https://www.nongnu.org/hcb/ for the grammar, and https://github.com/cplusplus/draft/blob/master/source/grammar.tex, and https://github.com/cplusplus/concepts-ts for the newest grammar. @@ -347,7 +348,7 @@ _fold_operator_re = re.compile(r'''(?x) | != | [<>=/*%+|&^~-]=? ''') -# see http://en.cppreference.com/w/cpp/keyword +# see https://en.cppreference.com/w/cpp/keyword _keywords = [ 'alignas', 'alignof', 'and', 'and_eq', 'asm', 'auto', 'bitand', 'bitor', 'bool', 'break', 'case', 'catch', 'char', 'char16_t', 'char32_t', 'class', @@ -382,7 +383,7 @@ _id_fundamental_v1 = { 'signed long': 'l', 'unsigned long': 'L', 'bool': 'b' -} # type: Dict[unicode, unicode] +} _id_shorthands_v1 = { 'std::string': 'ss', 'std::ostream': 'os', @@ -390,7 +391,7 @@ _id_shorthands_v1 = { 'std::iostream': 'ios', 'std::vector': 'v', 'std::map': 'm' -} # type: Dict[unicode, unicode] +} _id_operator_v1 = { 'new': 'new-operator', 'new[]': 'new-array-operator', @@ -439,7 +440,7 @@ _id_operator_v1 = { '->': 'pointer-operator', '()': 'call-operator', '[]': 'subscript-operator' -} # type: Dict[unicode, unicode] +} # ------------------------------------------------------------------------------ # Id v > 1 constants @@ -484,7 +485,7 @@ _id_fundamental_v2 = { 'auto': 'Da', 'decltype(auto)': 'Dc', 'std::nullptr_t': 'Dn' -} # type: Dict[unicode, unicode] +} _id_operator_v2 = { 'new': 'nw', 'new[]': 'na', @@ -535,7 +536,7 @@ _id_operator_v2 = { '()': 'cl', '[]': 'ix', '.*': 'ds' # this one is not overloadable, but we need it for expressions -} # type: Dict[unicode, unicode] +} _id_operator_unary_v2 = { '++': 'pp_', '--': 'mm_', @@ -549,7 +550,7 @@ _id_operator_unary_v2 = { _id_char_from_prefix = { None: 'c', 'u8': 'c', 'u': 'Ds', 'U': 'Di', 'L': 'w' -} # type: Dict[unicode, unicode] +} # type: Dict[Any, str] # these are ordered by preceedence _expression_bin_ops = [ ['||'], @@ -575,28 +576,28 @@ _id_explicit_cast = { } -class NoOldIdError(UnicodeMixin, Exception): - # Used to avoid implementing unneeded id generation for old id schmes. - def __init__(self, description=""): - # type: (unicode) -> None - self.description = description - - def __unicode__(self): - # type: () -> unicode - return self.description - +class NoOldIdError(Exception): + # Used to avoid implementing unneeded id generation for old id schemes. + @property + def description(self): + # type: () -> str + warnings.warn('%s.description is deprecated. ' + 'Coerce the instance to a string instead.' % self.__class__.__name__, + RemovedInSphinx40Warning, stacklevel=2) + return str(self) -class DefinitionError(UnicodeMixin, Exception): - def __init__(self, description): - # type: (unicode) -> None - self.description = description - def __unicode__(self): - # type: () -> unicode - return self.description +class DefinitionError(Exception): + @property + def description(self): + # type: () -> str + warnings.warn('%s.description is deprecated. ' + 'Coerce the instance to a string instead.' % self.__class__.__name__, + RemovedInSphinx40Warning, stacklevel=2) + return str(self) -class _DuplicateSymbolError(UnicodeMixin, Exception): +class _DuplicateSymbolError(Exception): def __init__(self, symbol, declaration): # type: (Symbol, Any) -> None assert symbol @@ -604,55 +605,51 @@ class _DuplicateSymbolError(UnicodeMixin, Exception): self.symbol = symbol self.declaration = declaration - def __unicode__(self): - # type: () -> unicode + def __str__(self): + # type: () -> str return "Internal C++ duplicate symbol error:\n%s" % self.symbol.dump(0) -class ASTBase(UnicodeMixin): +class ASTBase: def __eq__(self, other): # type: (Any) -> bool if type(self) is not type(other): return False try: - for key, value in iteritems(self.__dict__): + for key, value in self.__dict__.items(): if value != getattr(other, key): return False except AttributeError: return False return True - def __ne__(self, other): - # type: (Any) -> bool - return not self.__eq__(other) - __hash__ = None # type: Callable[[], int] def clone(self): - # type: () -> ASTBase + # type: () -> Any """Clone a definition expression node.""" return deepcopy(self) def _stringify(self, transform): - # type: (Callable[[Any], unicode]) -> unicode + # type: (Callable[[Any], str]) -> str raise NotImplementedError(repr(self)) - def __unicode__(self): - # type: () -> unicode - return self._stringify(lambda ast: text_type(ast)) + def __str__(self): + # type: () -> str + return self._stringify(lambda ast: str(ast)) def get_display_string(self): - # type: () -> unicode + # type: () -> str return self._stringify(lambda ast: ast.get_display_string()) def __repr__(self): # type: () -> str - return '<%s %s>' % (self.__class__.__name__, self) + return '<%s>' % self.__class__.__name__ def _verify_description_mode(mode): - # type: (unicode) -> None - if mode not in ('lastIsName', 'noneIsName', 'markType', 'param'): + # type: (str) -> None + if mode not in ('lastIsName', 'noneIsName', 'markType', 'markName', 'param'): raise Exception("Description mode '%s' is invalid." % mode) @@ -662,7 +659,7 @@ def _verify_description_mode(mode): class ASTCPPAttribute(ASTBase): def __init__(self, arg): - # type: (unicode) -> None + # type: (str) -> None self.arg = arg def _stringify(self, transform): @@ -670,18 +667,19 @@ class ASTCPPAttribute(ASTBase): def describe_signature(self, signode): # type: (addnodes.desc_signature) -> None - txt = text_type(self) + txt = str(self) signode.append(nodes.Text(txt, txt)) class ASTGnuAttribute(ASTBase): def __init__(self, name, args): - # type: (unicode, Any) -> None + # type: (str, Any) -> None self.name = name self.args = args def _stringify(self, transform): - res = [self.name] # type: List[unicode] + # type: (Callable[[Any], str]) -> str + res = [self.name] if self.args: res.append('(') res.append(transform(self.args)) @@ -695,7 +693,8 @@ class ASTGnuAttributeList(ASTBase): self.attrs = attrs def _stringify(self, transform): - res = ['__attribute__(('] # type: List[unicode] + # type: (Callable[[Any], str]) -> str + res = ['__attribute__(('] first = True for attr in self.attrs: if not first: @@ -707,7 +706,7 @@ class ASTGnuAttributeList(ASTBase): def describe_signature(self, signode): # type: (addnodes.desc_signature) -> None - txt = text_type(self) + txt = str(self) signode.append(nodes.Text(txt, txt)) @@ -715,10 +714,11 @@ class ASTIdAttribute(ASTBase): """For simple attributes defined by the user.""" def __init__(self, id): - # type: (unicode) -> None + # type: (str) -> None self.id = id def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str return self.id def describe_signature(self, signode): @@ -730,16 +730,17 @@ class ASTParenAttribute(ASTBase): """For paren attributes defined by the user.""" def __init__(self, id, arg): - # type: (unicode, unicode) -> None + # type: (str, str) -> None self.id = id self.arg = arg def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str return self.id + '(' + self.arg + ')' def describe_signature(self, signode): # type: (addnodes.desc_signature) -> None - txt = text_type(self) + txt = str(self) signode.append(nodes.Text(txt, txt)) @@ -749,9 +750,11 @@ class ASTParenAttribute(ASTBase): class ASTPointerLiteral(ASTBase): def _stringify(self, transform): - return u'nullptr' + # type: (Callable[[Any], str]) -> str + return 'nullptr' def get_id(self, version): + # type: (int) -> str return 'LDnE' def describe_signature(self, signode, mode, env, symbol): @@ -763,45 +766,54 @@ class ASTBooleanLiteral(ASTBase): self.value = value def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str if self.value: - return u'true' + return 'true' else: - return u'false' + return 'false' def get_id(self, version): + # type: (int) -> str if self.value: return 'L1E' else: return 'L0E' def describe_signature(self, signode, mode, env, symbol): - signode.append(nodes.Text(text_type(self))) + signode.append(nodes.Text(str(self))) class ASTNumberLiteral(ASTBase): def __init__(self, data): - # type: (unicode) -> None + # type: (str) -> None self.data = data def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str return self.data def get_id(self, version): + # type: (int) -> str return "L%sE" % self.data def describe_signature(self, signode, mode, env, symbol): - txt = text_type(self) + txt = str(self) signode.append(nodes.Text(txt, txt)) -class UnsupportedMultiCharacterCharLiteral(UnicodeMixin, Exception): - def __init__(self, decoded): - self.decoded = decoded +class UnsupportedMultiCharacterCharLiteral(Exception): + @property + def decoded(self): + # type: () -> str + warnings.warn('%s.decoded is deprecated. ' + 'Coerce the instance to a string instead.' % self.__class__.__name__, + RemovedInSphinx40Warning, stacklevel=2) + return str(self) class ASTCharLiteral(ASTBase): def __init__(self, prefix, data): - # type: (unicode, unicode) -> None + # type: (str, str) -> None self.prefix = prefix # may be None when no prefix self.data = data assert prefix in _id_char_from_prefix @@ -813,41 +825,47 @@ class ASTCharLiteral(ASTBase): raise UnsupportedMultiCharacterCharLiteral(decoded) def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str if self.prefix is None: return "'" + self.data + "'" else: return self.prefix + "'" + self.data + "'" def get_id(self, version): + # type: (int) -> str return self.type + str(self.value) def describe_signature(self, signode, mode, env, symbol): - txt = text_type(self) + txt = str(self) signode.append(nodes.Text(txt, txt)) class ASTStringLiteral(ASTBase): def __init__(self, data): - # type: (unicode) -> None + # type: (str) -> None self.data = data def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str return self.data def get_id(self, version): + # type: (int) -> str # note: the length is not really correct with escaping return "LA%d_KcE" % (len(self.data) - 2) def describe_signature(self, signode, mode, env, symbol): - txt = text_type(self) + txt = str(self) signode.append(nodes.Text(txt, txt)) class ASTThisLiteral(ASTBase): def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str return "this" def get_id(self, version): + # type: (int) -> str return "fpT" def describe_signature(self, signode, mode, env, symbol): @@ -859,9 +877,11 @@ class ASTParenExpr(ASTBase): self.expr = expr def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str return '(' + transform(self.expr) + ')' def get_id(self, version): + # type: (int) -> str return self.expr.get_id(version) def describe_signature(self, signode, mode, env, symbol): @@ -872,32 +892,34 @@ class ASTParenExpr(ASTBase): class ASTFoldExpr(ASTBase): def __init__(self, leftExpr, op, rightExpr): - # type: (Any, unicode, Any) -> None + # type: (Any, str, Any) -> None assert leftExpr is not None or rightExpr is not None self.leftExpr = leftExpr self.op = op self.rightExpr = rightExpr def _stringify(self, transform): - res = [u'('] + # type: (Callable[[Any], str]) -> str + res = ['('] if self.leftExpr: res.append(transform(self.leftExpr)) - res.append(u' ') + res.append(' ') res.append(transform(self.op)) - res.append(u' ') - res.append(u'...') + res.append(' ') + res.append('...') if self.rightExpr: - res.append(u' ') + res.append(' ') res.append(transform(self.op)) - res.append(u' ') + res.append(' ') res.append(transform(self.rightExpr)) - res.append(u')') - return u''.join(res) + res.append(')') + return ''.join(res) def get_id(self, version): + # type: (int) -> str assert version >= 3 if version == 3: - return text_type(self) + return str(self) # https://github.com/itanium-cxx-abi/cxx-abi/pull/67 res = [] if self.leftExpr is None: # (... op expr) @@ -908,7 +930,7 @@ class ASTFoldExpr(ASTBase): # we don't check where the parameter pack is, # we just always call this a binary left fold res.append('fL') - res.append(str(_id_operator_v2[self.op])) # TODO: remove str when merging to 2.0 + res.append(_id_operator_v2[self.op]) if self.leftExpr: res.append(self.leftExpr.get_id(version)) if self.rightExpr: @@ -939,6 +961,7 @@ class ASTBinOpExpr(ASTBase): self.ops = ops def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str res = [] res.append(transform(self.exprs[0])) for i in range(1, len(self.exprs)): @@ -946,16 +969,17 @@ class ASTBinOpExpr(ASTBase): res.append(self.ops[i - 1]) res.append(' ') res.append(transform(self.exprs[i])) - return u''.join(res) + return ''.join(res) def get_id(self, version): + # type: (int) -> str assert version >= 2 res = [] for i in range(len(self.ops)): res.append(_id_operator_v2[self.ops[i]]) res.append(self.exprs[i].get_id(version)) res.append(self.exprs[-1].get_id(version)) - return u''.join(res) + return ''.join(res) def describe_signature(self, signode, mode, env, symbol): self.exprs[0].describe_signature(signode, mode, env, symbol) @@ -974,6 +998,7 @@ class ASTAssignmentExpr(ASTBase): self.ops = ops def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str res = [] res.append(transform(self.exprs[0])) for i in range(1, len(self.exprs)): @@ -981,15 +1006,16 @@ class ASTAssignmentExpr(ASTBase): res.append(self.ops[i - 1]) res.append(' ') res.append(transform(self.exprs[i])) - return u''.join(res) + return ''.join(res) def get_id(self, version): + # type: (int) -> str res = [] for i in range(len(self.ops)): res.append(_id_operator_v2[self.ops[i]]) res.append(self.exprs[i].get_id(version)) res.append(self.exprs[-1].get_id(version)) - return u''.join(res) + return ''.join(res) def describe_signature(self, signode, mode, env, symbol): self.exprs[0].describe_signature(signode, mode, env, symbol) @@ -1006,13 +1032,15 @@ class ASTCastExpr(ASTBase): self.expr = expr def _stringify(self, transform): - res = [u'('] + # type: (Callable[[Any], str]) -> str + res = ['('] res.append(transform(self.typ)) - res.append(u')') + res.append(')') res.append(transform(self.expr)) - return u''.join(res) + return ''.join(res) def get_id(self, version): + # type: (int) -> str return 'cv' + self.typ.get_id(version) + self.expr.get_id(version) def describe_signature(self, signode, mode, env, symbol): @@ -1028,9 +1056,11 @@ class ASTUnaryOpExpr(ASTBase): self.expr = expr def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str return transform(self.op) + transform(self.expr) def get_id(self, version): + # type: (int) -> str return _id_operator_unary_v2[self.op] + self.expr.get_id(version) def describe_signature(self, signode, mode, env, symbol): @@ -1043,9 +1073,11 @@ class ASTSizeofParamPack(ASTBase): self.identifier = identifier def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str return "sizeof...(" + transform(self.identifier) + ")" def get_id(self, version): + # type: (int) -> str return 'sZ' + self.identifier.get_id(version) def describe_signature(self, signode, mode, env, symbol): @@ -1060,9 +1092,11 @@ class ASTSizeofType(ASTBase): self.typ = typ def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str return "sizeof(" + transform(self.typ) + ")" def get_id(self, version): + # type: (int) -> str return 'st' + self.typ.get_id(version) def describe_signature(self, signode, mode, env, symbol): @@ -1076,9 +1110,11 @@ class ASTSizeofExpr(ASTBase): self.expr = expr def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str return "sizeof " + transform(self.expr) def get_id(self, version): + # type: (int) -> str return 'sz' + self.expr.get_id(version) def describe_signature(self, signode, mode, env, symbol): @@ -1091,9 +1127,11 @@ class ASTAlignofExpr(ASTBase): self.typ = typ def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str return "alignof(" + transform(self.typ) + ")" def get_id(self, version): + # type: (int) -> str return 'at' + self.typ.get_id(version) def describe_signature(self, signode, mode, env, symbol): @@ -1107,9 +1145,11 @@ class ASTNoexceptExpr(ASTBase): self.expr = expr def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str return "noexcept(" + transform(self.expr) + ")" def get_id(self, version): + # type: (int) -> str return 'nx' + self.expr.get_id(version) def describe_signature(self, signode, mode, env, symbol): @@ -1120,7 +1160,7 @@ class ASTNoexceptExpr(ASTBase): class ASTNewExpr(ASTBase): def __init__(self, rooted, isNewTypeId, typ, initList, initType): - # type: (bool, bool, ASTType, List[Any], unicode) -> None + # type: (bool, bool, ASTType, List[Any], str) -> None self.rooted = rooted self.isNewTypeId = isNewTypeId self.typ = typ @@ -1130,6 +1170,7 @@ class ASTNewExpr(ASTBase): assert self.initType in ')}' def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str res = [] if self.rooted: res.append('::') @@ -1149,9 +1190,10 @@ class ASTNewExpr(ASTBase): first = False res.append(transform(e)) res.append(self.initType) - return u''.join(res) + return ''.join(res) def get_id(self, version): + # type: (int) -> str # the array part will be in the type mangling, so na is not used res = ['nw'] # TODO: placement @@ -1167,7 +1209,7 @@ class ASTNewExpr(ASTBase): assert False else: res.append('E') - return u''.join(res) + return ''.join(res) def describe_signature(self, signode, mode, env, symbol): if self.rooted: @@ -1199,6 +1241,7 @@ class ASTDeleteExpr(ASTBase): self.expr = expr def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str res = [] if self.rooted: res.append('::') @@ -1206,9 +1249,10 @@ class ASTDeleteExpr(ASTBase): if self.array: res.append('[] ') res.append(transform(self.expr)) - return u''.join(res) + return ''.join(res) def get_id(self, version): + # type: (int) -> str if self.array: id = "da" else: @@ -1232,15 +1276,17 @@ class ASTExplicitCast(ASTBase): self.expr = expr def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str res = [self.cast] res.append('<') res.append(transform(self.typ)) res.append('>(') res.append(transform(self.expr)) res.append(')') - return u''.join(res) + return ''.join(res) def get_id(self, version): + # type: (int) -> str return (_id_explicit_cast[self.cast] + self.typ.get_id(version) + self.expr.get_id(version)) @@ -1261,9 +1307,11 @@ class ASTTypeId(ASTBase): self.isType = isType def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str return 'typeid(' + transform(self.typeOrExpr) + ')' def get_id(self, version): + # type: (int) -> str prefix = 'ti' if self.isType else 'te' return prefix + self.typeOrExpr.get_id(version) @@ -1279,22 +1327,24 @@ class ASTPostfixCallExpr(ASTBase): self.exprs = exprs def _stringify(self, transform): - res = [u'('] + # type: (Callable[[Any], str]) -> str + res = ['('] first = True for e in self.exprs: if not first: - res.append(u', ') + res.append(', ') first = False res.append(transform(e)) - res.append(u')') - return u''.join(res) + res.append(')') + return ''.join(res) def get_id(self, idPrefix, version): + # type: (str, int) -> str res = ['cl', idPrefix] for e in self.exprs: res.append(e.get_id(version)) res.append('E') - return u''.join(res) + return ''.join(res) def describe_signature(self, signode, mode, env, symbol): signode.append(nodes.Text('(')) @@ -1312,9 +1362,11 @@ class ASTPostfixArray(ASTBase): self.expr = expr def _stringify(self, transform): - return u'[' + transform(self.expr) + ']' + # type: (Callable[[Any], str]) -> str + return '[' + transform(self.expr) + ']' def get_id(self, idPrefix, version): + # type: (str, int) -> str return 'ix' + idPrefix + self.expr.get_id(version) def describe_signature(self, signode, mode, env, symbol): @@ -1325,9 +1377,11 @@ class ASTPostfixArray(ASTBase): class ASTPostfixInc(ASTBase): def _stringify(self, transform): - return u'++' + # type: (Callable[[Any], str]) -> str + return '++' def get_id(self, idPrefix, version): + # type: (str, int) -> str return 'pp' + idPrefix def describe_signature(self, signode, mode, env, symbol): @@ -1336,9 +1390,11 @@ class ASTPostfixInc(ASTBase): class ASTPostfixDec(ASTBase): def _stringify(self, transform): - return u'--' + # type: (Callable[[Any], str]) -> str + return '--' def get_id(self, idPrefix, version): + # type: (str, int) -> str return 'mm' + idPrefix def describe_signature(self, signode, mode, env, symbol): @@ -1350,9 +1406,11 @@ class ASTPostfixMember(ASTBase): self.name = name def _stringify(self, transform): - return u'.' + transform(self.name) + # type: (Callable[[Any], str]) -> str + return '.' + transform(self.name) def get_id(self, idPrefix, version): + # type: (str, int) -> str return 'dt' + idPrefix + self.name.get_id(version) def describe_signature(self, signode, mode, env, symbol): @@ -1365,9 +1423,11 @@ class ASTPostfixMemberOfPointer(ASTBase): self.name = name def _stringify(self, transform): - return u'->' + transform(self.name) + # type: (Callable[[Any], str]) -> str + return '->' + transform(self.name) def get_id(self, idPrefix, version): + # type: (str, int) -> str return 'pt' + idPrefix + self.name.get_id(version) def describe_signature(self, signode, mode, env, symbol): @@ -1382,12 +1442,14 @@ class ASTPostfixExpr(ASTBase): self.postFixes = postFixes def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str res = [transform(self.prefix)] for p in self.postFixes: res.append(transform(p)) - return u''.join(res) + return ''.join(res) def get_id(self, version): + # type: (int) -> str id = self.prefix.get_id(version) for p in self.postFixes: id = p.get_id(id, version) @@ -1404,9 +1466,11 @@ class ASTPackExpansionExpr(ASTBase): self.expr = expr def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str return transform(self.expr) + '...' def get_id(self, version): + # type: (int) -> str id = self.expr.get_id(version) return 'sp' + id @@ -1420,10 +1484,12 @@ class ASTFallbackExpr(ASTBase): self.expr = expr def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str return self.expr def get_id(self, version): - return text_type(self.expr) + # type: (int) -> str + return str(self.expr) def describe_signature(self, signode, mode, env, symbol): signode += nodes.Text(self.expr) @@ -1435,7 +1501,7 @@ class ASTFallbackExpr(ASTBase): class ASTIdentifier(ASTBase): def __init__(self, identifier): - # type: (unicode) -> None + # type: (str) -> None assert identifier is not None assert len(identifier) != 0 self.identifier = identifier @@ -1444,7 +1510,7 @@ class ASTIdentifier(ASTBase): return self.identifier[0] == '@' def get_id(self, version): - # type: (int) -> unicode + # type: (int) -> str if self.is_anon() and version < 3: raise NoOldIdError() if version == 1: @@ -1459,22 +1525,22 @@ class ASTIdentifier(ASTBase): return 'D0' else: if self.is_anon(): - return u'Ut%d_%s' % (len(self.identifier) - 1, self.identifier[1:]) + return 'Ut%d_%s' % (len(self.identifier) - 1, self.identifier[1:]) else: - return text_type(len(self.identifier)) + self.identifier + return str(len(self.identifier)) + self.identifier - # and this is where we finally make a difference between __unicode__ and the display string + # and this is where we finally make a difference between __str__ and the display string - def __unicode__(self): - # type: () -> unicode + def __str__(self): + # type: () -> str return self.identifier def get_display_string(self): - # type: () -> unicode - return u"[anonymous]" if self.is_anon() else self.identifier + # type: () -> str + return "[anonymous]" if self.is_anon() else self.identifier def describe_signature(self, signode, mode, env, prefix, templateArgs, symbol): - # type: (Any, unicode, BuildEnvironment, unicode, unicode, Symbol) -> None + # type: (Any, str, BuildEnvironment, str, str, Symbol) -> None _verify_description_mode(mode) if mode == 'markType': targetText = prefix + self.identifier + templateArgs @@ -1505,7 +1571,7 @@ class ASTIdentifier(ASTBase): class ASTTemplateKeyParamPackIdDefault(ASTBase): def __init__(self, key, identifier, parameterPack, default): - # type: (unicode, ASTIdentifier, bool, ASTType) -> None + # type: (str, ASTIdentifier, bool, ASTType) -> None assert key if parameterPack: assert default is None @@ -1519,7 +1585,7 @@ class ASTTemplateKeyParamPackIdDefault(ASTBase): return self.identifier def get_id(self, version): - # type: (int) -> unicode + # type: (int) -> str assert version >= 2 # this is not part of the normal name mangling in C++ res = [] @@ -1530,7 +1596,8 @@ class ASTTemplateKeyParamPackIdDefault(ASTBase): return ''.join(res) def _stringify(self, transform): - res = [self.key] # type: List[unicode] + # type: (Callable[[Any], str]) -> str + res = [self.key] if self.parameterPack: if self.identifier: res.append(' ') @@ -1545,7 +1612,7 @@ class ASTTemplateKeyParamPackIdDefault(ASTBase): return ''.join(res) def describe_signature(self, signode, mode, env, symbol): - # type: (addnodes.desc_signature, unicode, BuildEnvironment, Symbol) -> None + # type: (addnodes.desc_signature, str, BuildEnvironment, Symbol) -> None signode += nodes.Text(self.key) if self.parameterPack: if self.identifier: @@ -1574,6 +1641,7 @@ class ASTTemplateParamType(ASTBase): @property def isPack(self): + # type: () -> bool return self.data.parameterPack def get_identifier(self): @@ -1581,7 +1649,7 @@ class ASTTemplateParamType(ASTBase): return self.data.get_identifier() def get_id(self, version, objectType=None, symbol=None): - # type: (int, unicode, Symbol) -> unicode + # type: (int, str, Symbol) -> str # this is not part of the normal name mangling in C++ assert version >= 2 if symbol: @@ -1591,10 +1659,11 @@ class ASTTemplateParamType(ASTBase): return self.data.get_id(version) def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str return transform(self.data) def describe_signature(self, signode, mode, env, symbol): - # type: (addnodes.desc_signature, unicode, BuildEnvironment, Symbol) -> None + # type: (addnodes.desc_signature, str, BuildEnvironment, Symbol) -> None self.data.describe_signature(signode, mode, env, symbol) @@ -1612,10 +1681,11 @@ class ASTTemplateParamConstrainedTypeWithInit(ASTBase): @property def isPack(self): + # type: () -> bool return self.type.isPack def get_id(self, version, objectType=None, symbol=None): - # type: (int, unicode, Symbol) -> unicode + # type: (int, str, Symbol) -> str # this is not part of the normal name mangling in C++ assert version >= 2 if symbol: @@ -1625,6 +1695,7 @@ class ASTTemplateParamConstrainedTypeWithInit(ASTBase): return self.type.get_id(version) def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str res = transform(self.type) if self.init: res += " = " @@ -1632,7 +1703,7 @@ class ASTTemplateParamConstrainedTypeWithInit(ASTBase): return res def describe_signature(self, signode, mode, env, symbol): - # type: (addnodes.desc_signature, unicode, BuildEnvironment, Symbol) -> None + # type: (addnodes.desc_signature, str, BuildEnvironment, Symbol) -> None self.type.describe_signature(signode, mode, env, symbol) if self.init: signode += nodes.Text(" = ") @@ -1655,6 +1726,7 @@ class ASTTemplateParamTemplateType(ASTBase): @property def isPack(self): + # type: () -> bool return self.data.parameterPack def get_identifier(self): @@ -1662,7 +1734,7 @@ class ASTTemplateParamTemplateType(ASTBase): return self.data.get_identifier() def get_id(self, version, objectType=None, symbol=None): - # type: (int, unicode, Symbol) -> unicode + # type: (int, str, Symbol) -> str assert version >= 2 # this is not part of the normal name mangling in C++ if symbol: @@ -1672,10 +1744,11 @@ class ASTTemplateParamTemplateType(ASTBase): return self.nestedParams.get_id(version) + self.data.get_id(version) def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str return transform(self.nestedParams) + transform(self.data) def describe_signature(self, signode, mode, env, symbol): - # type: (addnodes.desc_signature, unicode, BuildEnvironment, Symbol) -> None + # type: (addnodes.desc_signature, str, BuildEnvironment, Symbol) -> None self.nestedParams.describe_signature(signode, 'noneIsName', env, symbol) signode += nodes.Text(' ') self.data.describe_signature(signode, mode, env, symbol) @@ -1695,6 +1768,7 @@ class ASTTemplateParamNonType(ASTBase): @property def isPack(self): + # type: () -> bool return self.param.isPack def get_identifier(self): @@ -1709,7 +1783,7 @@ class ASTTemplateParamNonType(ASTBase): return None def get_id(self, version, objectType=None, symbol=None): - # type: (int, unicode, Symbol) -> unicode + # type: (int, str, Symbol) -> str assert version >= 2 # this is not part of the normal name mangling in C++ if symbol: @@ -1719,10 +1793,11 @@ class ASTTemplateParamNonType(ASTBase): return '_' + self.param.get_id(version) def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str return transform(self.param) def describe_signature(self, signode, mode, env, symbol): - # type: (addnodes.desc_signature, unicode, BuildEnvironment, Symbol) -> None + # type: (addnodes.desc_signature, str, BuildEnvironment, Symbol) -> None self.param.describe_signature(signode, mode, env, symbol) @@ -1734,7 +1809,7 @@ class ASTTemplateParams(ASTBase): self.isNested = False # whether it's a template template param def get_id(self, version): - # type: (int) -> unicode + # type: (int) -> str assert version >= 2 res = [] res.append("I") @@ -1744,14 +1819,15 @@ class ASTTemplateParams(ASTBase): return ''.join(res) def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str res = [] - res.append(u"template<") - res.append(u", ".join(transform(a) for a in self.params)) - res.append(u"> ") + res.append("template<") + res.append(", ".join(transform(a) for a in self.params)) + res.append("> ") return ''.join(res) def describe_signature(self, parentNode, mode, env, symbol, lineSpec=None): - # type: (addnodes.desc_signature, unicode, BuildEnvironment, Symbol, bool) -> None + # type: (addnodes.desc_signature, str, BuildEnvironment, Symbol, bool) -> None # 'lineSpec' is defaulted becuase of template template parameters def makeLine(parentNode=parentNode): signode = addnodes.desc_signature_line() @@ -1790,6 +1866,7 @@ class ASTTemplateIntroductionParameter(ASTBase): @property def isPack(self): + # type: () -> bool return self.parameterPack def get_identifier(self): @@ -1797,7 +1874,7 @@ class ASTTemplateIntroductionParameter(ASTBase): return self.identifier def get_id(self, version, objectType=None, symbol=None): - # type: (int, unicode, Symbol) -> unicode + # type: (int, str, Symbol) -> str assert version >= 2 # this is not part of the normal name mangling in C++ if symbol: @@ -1810,24 +1887,25 @@ class ASTTemplateIntroductionParameter(ASTBase): return '0' # we need to put something def get_id_as_arg(self, version): - # type: (int) -> unicode + # type: (int) -> str assert version >= 2 # used for the implicit requires clause res = self.identifier.get_id(version) if self.parameterPack: - return u'sp' + res + return 'sp' + res else: return res def _stringify(self, transform): - res = [] # type: List[unicode] + # type: (Callable[[Any], str]) -> str + res = [] if self.parameterPack: res.append('...') res.append(transform(self.identifier)) return ''.join(res) def describe_signature(self, signode, mode, env, symbol): - # type: (addnodes.desc_signature, unicode, BuildEnvironment, Symbol) -> None + # type: (addnodes.desc_signature, str, BuildEnvironment, Symbol) -> None if self.parameterPack: signode += nodes.Text('...') self.identifier.describe_signature(signode, mode, env, '', '', symbol) @@ -1841,7 +1919,7 @@ class ASTTemplateIntroduction(ASTBase): self.params = params def get_id(self, version): - # type: (int) -> unicode + # type: (int) -> str assert version >= 2 # first do the same as a normal template parameter list res = [] @@ -1860,6 +1938,7 @@ class ASTTemplateIntroduction(ASTBase): return ''.join(res) def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str res = [] res.append(transform(self.concept)) res.append('{') @@ -1868,7 +1947,7 @@ class ASTTemplateIntroduction(ASTBase): return ''.join(res) def describe_signature(self, parentNode, mode, env, symbol, lineSpec): - # type: (addnodes.desc_signature, unicode, BuildEnvironment, Symbol, bool) -> None + # type: (addnodes.desc_signature, str, BuildEnvironment, Symbol, bool) -> None # Note: 'lineSpec' has no effect on template introductions. signode = addnodes.desc_signature_line() parentNode += signode @@ -1891,22 +1970,23 @@ class ASTTemplateDeclarationPrefix(ASTBase): self.templates = templates def get_id(self, version): - # type: (int) -> unicode + # type: (int) -> str assert version >= 2 # this is not part of a normal name mangling system res = [] for t in self.templates: res.append(t.get_id(version)) - return u''.join(res) + return ''.join(res) def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str res = [] for t in self.templates: res.append(transform(t)) - return u''.join(res) + return ''.join(res) def describe_signature(self, signode, mode, env, symbol, lineSpec): - # type: (addnodes.desc_signature, unicode, BuildEnvironment, Symbol, bool) -> None + # type: (addnodes.desc_signature, str, BuildEnvironment, Symbol, bool) -> None _verify_description_mode(mode) for t in self.templates: t.describe_signature(signode, 'lastIsName', env, symbol, lineSpec) @@ -1924,13 +2004,13 @@ class ASTOperator(ASTBase): return True def get_id(self, version): - # type: (int) -> unicode + # type: (int) -> str raise NotImplementedError() def describe_signature(self, signode, mode, env, prefix, templateArgs, symbol): - # type: (addnodes.desc_signature, unicode, Any, unicode, unicode, Symbol) -> None + # type: (addnodes.desc_signature, str, Any, str, str, Symbol) -> None _verify_description_mode(mode) - identifier = text_type(self) + identifier = str(self) if mode == 'lastIsName': signode += addnodes.desc_name(identifier, identifier) else: @@ -1939,11 +2019,11 @@ class ASTOperator(ASTBase): class ASTOperatorBuildIn(ASTOperator): def __init__(self, op): - # type: (unicode) -> None + # type: (str) -> None self.op = op def get_id(self, version): - # type: (int) -> unicode + # type: (int) -> str if version == 1: ids = _id_operator_v1 else: @@ -1954,10 +2034,11 @@ class ASTOperatorBuildIn(ASTOperator): return ids[self.op] def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str if self.op in ('new', 'new[]', 'delete', 'delete[]'): - return u'operator ' + self.op + return 'operator ' + self.op else: - return u'operator' + self.op + return 'operator' + self.op class ASTOperatorType(ASTOperator): @@ -1966,18 +2047,19 @@ class ASTOperatorType(ASTOperator): self.type = type def get_id(self, version): - # type: (int) -> unicode + # type: (int) -> str if version == 1: - return u'castto-%s-operator' % self.type.get_id(version) + return 'castto-%s-operator' % self.type.get_id(version) else: - return u'cv' + self.type.get_id(version) + return 'cv' + self.type.get_id(version) def _stringify(self, transform): - return u''.join(['operator ', transform(self.type)]) + # type: (Callable[[Any], str]) -> str + return ''.join(['operator ', transform(self.type)]) def get_name_no_template(self): - # type: () -> unicode - return text_type(self) + # type: () -> str + return str(self) class ASTOperatorLiteral(ASTOperator): @@ -1986,14 +2068,15 @@ class ASTOperatorLiteral(ASTOperator): self.identifier = identifier def get_id(self, version): - # type: (int) -> unicode + # type: (int) -> str if version == 1: raise NoOldIdError() else: - return u'li' + self.identifier.get_id(version) + return 'li' + self.identifier.get_id(version) def _stringify(self, transform): - return u'operator""' + transform(self.identifier) + # type: (Callable[[Any], str]) -> str + return 'operator""' + transform(self.identifier) ############################################################################################## @@ -2005,18 +2088,19 @@ class ASTTemplateArgConstant(ASTBase): self.value = value def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str return transform(self.value) def get_id(self, version): - # type: (int) -> unicode + # type: (int) -> str if version == 1: - return text_type(self).replace(u' ', u'-') + return str(self).replace(' ', '-') if version == 2: - return u'X' + text_type(self) + u'E' - return u'X' + self.value.get_id(version) + u'E' + return 'X' + str(self) + 'E' + return 'X' + self.value.get_id(version) + 'E' def describe_signature(self, signode, mode, env, symbol): - # type: (addnodes.desc_signature, unicode, BuildEnvironment, Symbol) -> None + # type: (addnodes.desc_signature, str, BuildEnvironment, Symbol) -> None _verify_description_mode(mode) self.value.describe_signature(signode, mode, env, symbol) @@ -2028,27 +2112,28 @@ class ASTTemplateArgs(ASTBase): self.args = args def get_id(self, version): - # type: (int) -> unicode + # type: (int) -> str if version == 1: - res = [] # type: List[unicode] + res = [] res.append(':') - res.append(u'.'.join(a.get_id(version) for a in self.args)) + res.append('.'.join(a.get_id(version) for a in self.args)) res.append(':') - return u''.join(res) + return ''.join(res) res = [] res.append('I') for a in self.args: res.append(a.get_id(version)) res.append('E') - return u''.join(res) + return ''.join(res) def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str res = ', '.join(transform(a) for a in self.args) return '<' + res + '>' def describe_signature(self, signode, mode, env, symbol): - # type: (addnodes.desc_signature, unicode, BuildEnvironment, Symbol) -> None + # type: (addnodes.desc_signature, str, BuildEnvironment, Symbol) -> None _verify_description_mode(mode) signode += nodes.Text('<') first = True @@ -2071,21 +2156,22 @@ class ASTNestedNameElement(ASTBase): return False def get_id(self, version): - # type: (int) -> unicode + # type: (int) -> str res = self.identOrOp.get_id(version) if self.templateArgs: res += self.templateArgs.get_id(version) return res def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str res = transform(self.identOrOp) if self.templateArgs: res += transform(self.templateArgs) return res def describe_signature(self, signode, mode, env, prefix, symbol): - # type: (addnodes.desc_signature, unicode, BuildEnvironment, unicode, Symbol) -> None - tArgs = text_type(self.templateArgs) if self.templateArgs is not None else '' + # type: (addnodes.desc_signature, str, BuildEnvironment, str, Symbol) -> None + tArgs = str(self.templateArgs) if self.templateArgs is not None else '' self.identOrOp.describe_signature(signode, mode, env, prefix, tArgs, symbol) if self.templateArgs is not None: self.templateArgs.describe_signature(signode, mode, env, symbol) @@ -2116,14 +2202,15 @@ class ASTNestedName(ASTBase): return count def get_id(self, version, modifiers=''): - # type: (int, unicode) -> unicode + # type: (int, str) -> str if version == 1: - tt = text_type(self) + tt = str(self) if tt in _id_shorthands_v1: return _id_shorthands_v1[tt] else: - return u'::'.join(n.get_id(version) for n in self.names) - res = [] # type: List[unicode] + return '::'.join(n.get_id(version) for n in self.names) + + res = [] if len(self.names) > 1 or len(modifiers) > 0: res.append('N') res.append(modifiers) @@ -2131,10 +2218,11 @@ class ASTNestedName(ASTBase): res.append(n.get_id(version)) if len(self.names) > 1 or len(modifiers) > 0: res.append('E') - return u''.join(res) + return ''.join(res) def _stringify(self, transform): - res = [] # type: List[unicode] + # type: (Callable[[Any], str]) -> str + res = [] if self.rooted: res.append('') for i in range(len(self.names)): @@ -2147,15 +2235,15 @@ class ASTNestedName(ASTBase): return '::'.join(res) def describe_signature(self, signode, mode, env, symbol): - # type: (addnodes.desc_signature, unicode, BuildEnvironment, Symbol) -> None + # type: (addnodes.desc_signature, str, BuildEnvironment, Symbol) -> None _verify_description_mode(mode) # just print the name part, with template args, not template params if mode == 'noneIsName': - signode += nodes.Text(text_type(self)) + signode += nodes.Text(str(self)) elif mode == 'param': - name = text_type(self) + name = str(self) signode += nodes.emphasis(name, name) - elif mode == 'markType' or mode == 'lastIsName': + elif mode == 'markType' or mode == 'lastIsName' or mode == 'markName': # Each element should be a pending xref targeting the complete # prefix. however, only the identifier part should be a link, such # that template args can be a link as well. @@ -2166,8 +2254,8 @@ class ASTNestedName(ASTBase): if symbol.declaration.templatePrefix is not None: templateParams = symbol.declaration.templatePrefix.templates iTemplateParams = 0 - templateParamsPrefix = u'' - prefix = '' # type: unicode + templateParamsPrefix = '' + prefix = '' first = True names = self.names[:-1] if mode == 'lastIsName' else self.names # If lastIsName, then wrap all of the prefix in a desc_addname, @@ -2176,7 +2264,7 @@ class ASTNestedName(ASTBase): # so it can remove it in inner declarations. dest = signode if mode == 'lastIsName': - dest = addnodes.desc_addname() + dest = addnodes.desc_addname() # type: ignore for i in range(len(names)): nne = names[i] template = self.templates[i] @@ -2186,10 +2274,10 @@ class ASTNestedName(ASTBase): if template: dest += nodes.Text("template ") first = False - txt_nne = text_type(nne) + txt_nne = str(nne) if txt_nne != '': if nne.templateArgs and iTemplateParams < len(templateParams): - templateParamsPrefix += text_type(templateParams[iTemplateParams]) + templateParamsPrefix += str(templateParams[iTemplateParams]) iTemplateParams += 1 nne.describe_signature(dest, 'markType', env, templateParamsPrefix + prefix, symbol) @@ -2207,14 +2295,15 @@ class ASTNestedName(ASTBase): class ASTTrailingTypeSpecFundamental(ASTBase): def __init__(self, name): - # type: (unicode) -> None + # type: (str) -> None self.name = name def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str return self.name def get_id(self, version): - # type: (int) -> unicode + # type: (int) -> str if version == 1: res = [] for a in self.name.split(' '): @@ -2222,7 +2311,7 @@ class ASTTrailingTypeSpecFundamental(ASTBase): res.append(_id_fundamental_v1[a]) else: res.append(a) - return u'-'.join(res) + return '-'.join(res) if self.name not in _id_fundamental_v2: raise Exception( @@ -2232,13 +2321,13 @@ class ASTTrailingTypeSpecFundamental(ASTBase): return _id_fundamental_v2[self.name] def describe_signature(self, signode, mode, env, symbol): - # type: (addnodes.desc_signature, unicode, BuildEnvironment, Symbol) -> None - signode += nodes.Text(text_type(self.name)) + # type: (addnodes.desc_signature, str, BuildEnvironment, Symbol) -> None + signode += nodes.Text(str(self.name)) class ASTTrailingTypeSpecName(ASTBase): def __init__(self, prefix, nestedName): - # type: (unicode, Any) -> None + # type: (str, Any) -> None self.prefix = prefix self.nestedName = nestedName @@ -2248,19 +2337,20 @@ class ASTTrailingTypeSpecName(ASTBase): return self.nestedName def get_id(self, version): - # type: (int) -> unicode + # type: (int) -> str return self.nestedName.get_id(version) def _stringify(self, transform): - res = [] # type: List[unicode] + # type: (Callable[[Any], str]) -> str + res = [] if self.prefix: res.append(self.prefix) res.append(' ') res.append(transform(self.nestedName)) - return u''.join(res) + return ''.join(res) def describe_signature(self, signode, mode, env, symbol): - # type: (addnodes.desc_signature, unicode, BuildEnvironment, Symbol) -> None + # type: (addnodes.desc_signature, str, BuildEnvironment, Symbol) -> None if self.prefix: signode += addnodes.desc_annotation(self.prefix, self.prefix) signode += nodes.Text(' ') @@ -2269,16 +2359,18 @@ class ASTTrailingTypeSpecName(ASTBase): class ASTTrailingTypeSpecDecltypeAuto(ASTBase): def _stringify(self, transform): - return u'decltype(auto)' + # type: (Callable[[Any], str]) -> str + return 'decltype(auto)' def get_id(self, version): + # type: (int) -> str if version == 1: raise NoOldIdError() return 'Dc' def describe_signature(self, signode, mode, env, symbol): - # type: (addnodes.desc_signature, unicode, BuildEnvironment, Symbol) -> None - signode.append(nodes.Text(text_type(self))) + # type: (addnodes.desc_signature, str, BuildEnvironment, Symbol) -> None + signode.append(nodes.Text(str(self))) class ASTTrailingTypeSpecDecltype(ASTBase): @@ -2286,14 +2378,17 @@ class ASTTrailingTypeSpecDecltype(ASTBase): self.expr = expr def _stringify(self, transform): - return u'decltype(' + transform(self.expr) + ')' + # type: (Callable[[Any], str]) -> str + return 'decltype(' + transform(self.expr) + ')' def get_id(self, version): + # type: (int) -> str if version == 1: raise NoOldIdError() return 'DT' + self.expr.get_id(version) + "E" def describe_signature(self, signode, mode, env, symbol): + # type: (addnodes.desc_signature, str, BuildEnvironment, Symbol) -> None signode.append(nodes.Text('decltype(')) self.expr.describe_signature(signode, mode, env, symbol) signode.append(nodes.Text(')')) @@ -2306,7 +2401,7 @@ class ASTFunctionParameter(ASTBase): self.ellipsis = ellipsis def get_id(self, version, objectType=None, symbol=None): - # type: (int, unicode, Symbol) -> unicode + # type: (int, str, Symbol) -> str # this is not part of the normal name mangling in C++ if symbol: # the anchor will be our parent @@ -2318,13 +2413,14 @@ class ASTFunctionParameter(ASTBase): return self.arg.get_id(version) def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str if self.ellipsis: return '...' else: return transform(self.arg) def describe_signature(self, signode, mode, env, symbol): - # type: (addnodes.desc_signature, unicode, BuildEnvironment, Symbol) -> None + # type: (addnodes.desc_signature, str, BuildEnvironment, Symbol) -> None _verify_description_mode(mode) if self.ellipsis: signode += nodes.Text('...') @@ -2335,7 +2431,7 @@ class ASTFunctionParameter(ASTBase): class ASTParametersQualifiers(ASTBase): def __init__(self, args, volatile, const, refQual, exceptionSpec, override, final, initializer): - # type: (List[Any], bool, bool, unicode, unicode, bool, bool, unicode) -> None + # type: (List[Any], bool, bool, str, str, bool, bool, str) -> None self.args = args self.volatile = volatile self.const = const @@ -2351,7 +2447,7 @@ class ASTParametersQualifiers(ASTBase): return self.args def get_modifiers_id(self, version): - # type: (int) -> unicode + # type: (int) -> str res = [] if self.volatile: res.append('V') @@ -2364,29 +2460,30 @@ class ASTParametersQualifiers(ASTBase): res.append('O') elif self.refQual == '&': res.append('R') - return u''.join(res) + return ''.join(res) def get_param_id(self, version): - # type: (int) -> unicode + # type: (int) -> str if version == 1: if len(self.args) == 0: return '' else: - return u'__' + u'.'.join(a.get_id(version) for a in self.args) + return '__' + '.'.join(a.get_id(version) for a in self.args) if len(self.args) == 0: return 'v' else: - return u''.join(a.get_id(version) for a in self.args) + return ''.join(a.get_id(version) for a in self.args) def _stringify(self, transform): - res = [] # type: List[unicode] + # type: (Callable[[Any], str]) -> str + res = [] res.append('(') first = True for a in self.args: if not first: res.append(', ') first = False - res.append(text_type(a)) + res.append(str(a)) res.append(')') if self.volatile: res.append(' volatile') @@ -2397,7 +2494,7 @@ class ASTParametersQualifiers(ASTBase): res.append(self.refQual) if self.exceptionSpec: res.append(' ') - res.append(text_type(self.exceptionSpec)) + res.append(str(self.exceptionSpec)) if self.final: res.append(' final') if self.override: @@ -2405,10 +2502,10 @@ class ASTParametersQualifiers(ASTBase): if self.initializer: res.append(' = ') res.append(self.initializer) - return u''.join(res) + return ''.join(res) def describe_signature(self, signode, mode, env, symbol): - # type: (addnodes.desc_signature, unicode, BuildEnvironment, Symbol) -> None + # type: (addnodes.desc_signature, str, BuildEnvironment, Symbol) -> None _verify_description_mode(mode) paramlist = addnodes.desc_parameterlist() for arg in self.args: @@ -2434,19 +2531,19 @@ class ASTParametersQualifiers(ASTBase): if self.refQual: _add_text(signode, self.refQual) if self.exceptionSpec: - _add_anno(signode, text_type(self.exceptionSpec)) + _add_anno(signode, str(self.exceptionSpec)) if self.final: _add_anno(signode, 'final') if self.override: _add_anno(signode, 'override') if self.initializer: - _add_text(signode, '= ' + text_type(self.initializer)) + _add_text(signode, '= ' + str(self.initializer)) class ASTDeclSpecsSimple(ASTBase): def __init__(self, storage, threadLocal, inline, virtual, explicit, constexpr, volatile, const, friend, attrs): - # type: (unicode, bool, bool, bool, bool, bool, bool, bool, bool, List[Any]) -> None + # type: (str, bool, bool, bool, bool, bool, bool, bool, bool, List[Any]) -> None self.storage = storage self.threadLocal = threadLocal self.inline = inline @@ -2474,7 +2571,8 @@ class ASTDeclSpecsSimple(ASTBase): self.attrs + other.attrs) def _stringify(self, transform): - res = [] # type: List[unicode] + # type: (Callable[[Any], str]) -> str + res = [] # type: List[str] res.extend(transform(attr) for attr in self.attrs) if self.storage: res.append(self.storage) @@ -2494,7 +2592,7 @@ class ASTDeclSpecsSimple(ASTBase): res.append('volatile') if self.const: res.append('const') - return u' '.join(res) + return ' '.join(res) def describe_signature(self, modifiers): # type: (List[nodes.Node]) -> None @@ -2528,6 +2626,7 @@ class ASTDeclSpecsSimple(ASTBase): class ASTDeclSpecs(ASTBase): def __init__(self, outer, leftSpecs, rightSpecs, trailing): + # type: (Any, ASTDeclSpecsSimple, ASTDeclSpecsSimple, Any) -> None # leftSpecs and rightSpecs are used for output # allSpecs are used for id generation self.outer = outer @@ -2542,7 +2641,7 @@ class ASTDeclSpecs(ASTBase): return self.trailingTypeSpec.name def get_id(self, version): - # type: (int) -> unicode + # type: (int) -> str if version == 1: res = [] res.append(self.trailingTypeSpec.get_id(version)) @@ -2550,7 +2649,7 @@ class ASTDeclSpecs(ASTBase): res.append('V') if self.allSpecs.const: res.append('C') - return u''.join(res) + return ''.join(res) res = [] if self.allSpecs.volatile: res.append('V') @@ -2558,20 +2657,19 @@ class ASTDeclSpecs(ASTBase): res.append('K') if self.trailingTypeSpec is not None: res.append(self.trailingTypeSpec.get_id(version)) - return u''.join(res) + return ''.join(res) def _stringify(self, transform): - res = [] # type: List[unicode] + # type: (Callable[[Any], str]) -> str + res = [] # type: List[str] l = transform(self.leftSpecs) if len(l) > 0: - if len(res) > 0: - res.append(" ") res.append(l) if self.trailingTypeSpec: if len(res) > 0: res.append(" ") res.append(transform(self.trailingTypeSpec)) - r = text_type(self.rightSpecs) + r = str(self.rightSpecs) if len(r) > 0: if len(res) > 0: res.append(" ") @@ -2579,7 +2677,7 @@ class ASTDeclSpecs(ASTBase): return "".join(res) def describe_signature(self, signode, mode, env, symbol): - # type: (addnodes.desc_signature, unicode, BuildEnvironment, Symbol) -> None + # type: (addnodes.desc_signature, str, BuildEnvironment, Symbol) -> None _verify_description_mode(mode) modifiers = [] # type: List[nodes.Node] @@ -2610,24 +2708,25 @@ class ASTArray(ASTBase): self.size = size def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str if self.size: - return u'[' + transform(self.size) + ']' + return '[' + transform(self.size) + ']' else: - return u'[]' + return '[]' def get_id(self, version): - # type: (int) -> unicode + # type: (int) -> str if version == 1: - return u'A' + return 'A' if version == 2: if self.size: - return u'A' + text_type(self.size) + u'_' + return 'A' + str(self.size) + '_' else: - return u'A_' + return 'A_' if self.size: - return u'A' + self.size.get_id(version) + u'_' + return 'A' + self.size.get_id(version) + '_' else: - return u'A_' + return 'A_' def describe_signature(self, signode, mode, env, symbol): _verify_description_mode(mode) @@ -2662,7 +2761,8 @@ class ASTDeclaratorPtr(ASTBase): return True def _stringify(self, transform): - res = ['*'] # type: List[unicode] + # type: (Callable[[Any], str]) -> str + res = ['*'] for a in self.attrs: res.append(transform(a)) if len(self.attrs) > 0 and (self.volatile or self.const): @@ -2677,18 +2777,18 @@ class ASTDeclaratorPtr(ASTBase): if self.next.require_space_after_declSpecs: res.append(' ') res.append(transform(self.next)) - return u''.join(res) + return ''.join(res) def get_modifiers_id(self, version): - # type: (int) -> unicode + # type: (int) -> str return self.next.get_modifiers_id(version) def get_param_id(self, version): - # type: (int) -> unicode + # type: (int) -> str return self.next.get_param_id(version) def get_ptr_suffix_id(self, version): - # type: (int) -> unicode + # type: (int) -> str if version == 1: res = ['P'] if self.volatile: @@ -2696,7 +2796,7 @@ class ASTDeclaratorPtr(ASTBase): if self.const: res.append('C') res.append(self.next.get_ptr_suffix_id(version)) - return u''.join(res) + return ''.join(res) res = [self.next.get_ptr_suffix_id(version)] res.append('P') @@ -2704,25 +2804,25 @@ class ASTDeclaratorPtr(ASTBase): res.append('V') if self.const: res.append('C') - return u''.join(res) + return ''.join(res) def get_type_id(self, version, returnTypeId): - # type: (int, unicode) -> unicode + # type: (int, str) -> str # ReturnType *next, so we are part of the return type of 'next - res = ['P'] # type: List[unicode] + res = ['P'] if self.volatile: res.append('V') if self.const: res.append('C') res.append(returnTypeId) - return self.next.get_type_id(version, returnTypeId=u''.join(res)) + return self.next.get_type_id(version, returnTypeId=''.join(res)) def is_function_type(self): # type: () -> bool return self.next.is_function_type() def describe_signature(self, signode, mode, env, symbol): - # type: (addnodes.desc_signature, unicode, BuildEnvironment, Symbol) -> None + # type: (addnodes.desc_signature, str, BuildEnvironment, Symbol) -> None _verify_description_mode(mode) signode += nodes.Text("*") for a in self.attrs: @@ -2758,6 +2858,7 @@ class ASTDeclaratorRef(ASTBase): @property def isPack(self): + # type: () -> bool return True @property @@ -2770,41 +2871,42 @@ class ASTDeclaratorRef(ASTBase): return self.next.require_space_after_declSpecs() def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str res = ['&'] for a in self.attrs: res.append(transform(a)) if len(self.attrs) > 0 and self.next.require_space_after_declSpecs: res.append(' ') res.append(transform(self.next)) - return u''.join(res) + return ''.join(res) def get_modifiers_id(self, version): - # type: (int) -> unicode + # type: (int) -> str return self.next.get_modifiers_id(version) def get_param_id(self, version): # only the parameters (if any) - # type: (int) -> unicode + # type: (int) -> str return self.next.get_param_id(version) def get_ptr_suffix_id(self, version): - # type: (int) -> unicode + # type: (int) -> str if version == 1: - return u'R' + self.next.get_ptr_suffix_id(version) + return 'R' + self.next.get_ptr_suffix_id(version) else: - return self.next.get_ptr_suffix_id(version) + u'R' + return self.next.get_ptr_suffix_id(version) + 'R' def get_type_id(self, version, returnTypeId): - # type: (int, unicode) -> unicode + # type: (int, str) -> str assert version >= 2 # ReturnType &next, so we are part of the return type of 'next - return self.next.get_type_id(version, returnTypeId=u'R' + returnTypeId) + return self.next.get_type_id(version, returnTypeId='R' + returnTypeId) def is_function_type(self): # type: () -> bool return self.next.is_function_type() def describe_signature(self, signode, mode, env, symbol): - # type: (addnodes.desc_signature, unicode, BuildEnvironment, Symbol) -> None + # type: (addnodes.desc_signature, str, BuildEnvironment, Symbol) -> None _verify_description_mode(mode) signode += nodes.Text("&") for a in self.attrs: @@ -2835,38 +2937,39 @@ class ASTDeclaratorParamPack(ASTBase): return False def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str res = transform(self.next) if self.next.name: res = ' ' + res return '...' + res def get_modifiers_id(self, version): - # type: (int) -> unicode + # type: (int) -> str return self.next.get_modifiers_id(version) def get_param_id(self, version): # only the parameters (if any) - # type: (int) -> unicode + # type: (int) -> str return self.next.get_param_id(version) def get_ptr_suffix_id(self, version): - # type: (int) -> unicode + # type: (int) -> str if version == 1: return 'Dp' + self.next.get_ptr_suffix_id(version) else: - return self.next.get_ptr_suffix_id(version) + u'Dp' + return self.next.get_ptr_suffix_id(version) + 'Dp' def get_type_id(self, version, returnTypeId): - # type: (int, unicode) -> unicode + # type: (int, str) -> str assert version >= 2 # ReturnType... next, so we are part of the return type of 'next - return self.next.get_type_id(version, returnTypeId=u'Dp' + returnTypeId) + return self.next.get_type_id(version, returnTypeId='Dp' + returnTypeId) def is_function_type(self): # type: () -> bool return self.next.is_function_type() def describe_signature(self, signode, mode, env, symbol): - # type: (addnodes.desc_signature, unicode, BuildEnvironment, Symbol) -> None + # type: (addnodes.desc_signature, str, BuildEnvironment, Symbol) -> None _verify_description_mode(mode) signode += nodes.Text("...") if self.next.name: @@ -2899,6 +3002,7 @@ class ASTDeclaratorMemPtr(ASTBase): return True def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str res = [] res.append(transform(self.className)) res.append('::*') @@ -2912,32 +3016,32 @@ class ASTDeclaratorMemPtr(ASTBase): return ''.join(res) def get_modifiers_id(self, version): - # type: (int) -> unicode + # type: (int) -> str if version == 1: raise NoOldIdError() else: return self.next.get_modifiers_id(version) def get_param_id(self, version): # only the parameters (if any) - # type: (int) -> unicode + # type: (int) -> str if version == 1: raise NoOldIdError() else: return self.next.get_param_id(version) def get_ptr_suffix_id(self, version): - # type: (int) -> unicode + # type: (int) -> str if version == 1: raise NoOldIdError() else: raise NotImplementedError() - return self.next.get_ptr_suffix_id(version) + u'Dp' + return self.next.get_ptr_suffix_id(version) + 'Dp' def get_type_id(self, version, returnTypeId): - # type: (int, unicode) -> unicode + # type: (int, str) -> str assert version >= 2 # ReturnType name::* next, so we are part of the return type of next - nextReturnTypeId = '' # type: unicode + nextReturnTypeId = '' if self.volatile: nextReturnTypeId += 'V' if self.const: @@ -2952,7 +3056,7 @@ class ASTDeclaratorMemPtr(ASTBase): return self.next.is_function_type() def describe_signature(self, signode, mode, env, symbol): - # type: (addnodes.desc_signature, unicode, BuildEnvironment, Symbol) -> None + # type: (addnodes.desc_signature, str, BuildEnvironment, Symbol) -> None _verify_description_mode(mode) self.className.describe_signature(signode, mode, env, symbol) signode += nodes.Text('::*') @@ -2995,22 +3099,23 @@ class ASTDeclaratorParen(ASTBase): return True def _stringify(self, transform): - res = ['('] # type: List[unicode] + # type: (Callable[[Any], str]) -> str + res = ['('] res.append(transform(self.inner)) res.append(')') res.append(transform(self.next)) return ''.join(res) def get_modifiers_id(self, version): - # type: (int) -> unicode + # type: (int) -> str return self.inner.get_modifiers_id(version) def get_param_id(self, version): # only the parameters (if any) - # type: (int) -> unicode + # type: (int) -> str return self.inner.get_param_id(version) def get_ptr_suffix_id(self, version): - # type: (int) -> unicode + # type: (int) -> str if version == 1: raise NoOldIdError() # TODO: was this implemented before? return self.next.get_ptr_suffix_id(version) + \ @@ -3020,7 +3125,7 @@ class ASTDeclaratorParen(ASTBase): self.next.get_ptr_suffix_id(version) def get_type_id(self, version, returnTypeId): - # type: (int, unicode) -> unicode + # type: (int, str) -> str assert version >= 2 # ReturnType (inner)next, so 'inner' returns everything outside nextId = self.next.get_type_id(version, returnTypeId) @@ -3031,7 +3136,7 @@ class ASTDeclaratorParen(ASTBase): return self.inner.is_function_type() def describe_signature(self, signode, mode, env, symbol): - # type: (addnodes.desc_signature, unicode, BuildEnvironment, Symbol) -> None + # type: (addnodes.desc_signature, str, BuildEnvironment, Symbol) -> None _verify_description_mode(mode) signode += nodes.Text('(') self.inner.describe_signature(signode, mode, env, symbol) @@ -3053,6 +3158,7 @@ class ASTDeclaratorNameParamQual(ASTBase): @property def isPack(self): + # type: () -> bool return False @property @@ -3061,26 +3167,25 @@ class ASTDeclaratorNameParamQual(ASTBase): return self.paramQual.function_params def get_modifiers_id(self, version): # only the modifiers for a function, e.g., - # type: (int) -> unicode + # type: (int) -> str # cv-qualifiers if self.paramQual: return self.paramQual.get_modifiers_id(version) - raise Exception( - "This should only be called on a function: %s" % text_type(self)) + raise Exception("This should only be called on a function: %s" % self) def get_param_id(self, version): # only the parameters (if any) - # type: (int) -> unicode + # type: (int) -> str if self.paramQual: return self.paramQual.get_param_id(version) else: return '' def get_ptr_suffix_id(self, version): # only the array specifiers - # type: (int) -> unicode - return u''.join(a.get_id(version) for a in self.arrayOps) + # type: (int) -> str + return ''.join(a.get_id(version) for a in self.arrayOps) def get_type_id(self, version, returnTypeId): - # type: (int, unicode) -> unicode + # type: (int, str) -> str assert version >= 2 res = [] # TOOD: can we actually have both array ops and paramQual? @@ -3093,7 +3198,7 @@ class ASTDeclaratorNameParamQual(ASTBase): res.append('E') else: res.append(returnTypeId) - return u''.join(res) + return ''.join(res) # ------------------------------------------------------------------------ @@ -3106,6 +3211,7 @@ class ASTDeclaratorNameParamQual(ASTBase): return self.paramQual is not None def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str res = [] if self.declId: res.append(transform(self.declId)) @@ -3113,10 +3219,10 @@ class ASTDeclaratorNameParamQual(ASTBase): res.append(transform(op)) if self.paramQual: res.append(transform(self.paramQual)) - return u''.join(res) + return ''.join(res) def describe_signature(self, signode, mode, env, symbol): - # type: (addnodes.desc_signature, unicode, BuildEnvironment, Symbol) -> None + # type: (addnodes.desc_signature, str, BuildEnvironment, Symbol) -> None _verify_description_mode(mode) if self.declId: self.declId.describe_signature(signode, mode, env, symbol) @@ -3131,10 +3237,11 @@ class ASTInitializer(ASTBase): self.value = value def _stringify(self, transform): - return u' = ' + transform(self.value) + # type: (Callable[[Any], str]) -> str + return ' = ' + transform(self.value) def describe_signature(self, signode, mode, env, symbol): - # type: (addnodes.desc_signature, unicode, BuildEnvironment, Symbol) -> None + # type: (addnodes.desc_signature, str, BuildEnvironment, Symbol) -> None _verify_description_mode(mode) signode.append(nodes.Text(' = ')) self.value.describe_signature(signode, 'markType', env, symbol) @@ -3155,6 +3262,7 @@ class ASTType(ASTBase): @property def isPack(self): + # type: () -> bool return self.decl.isPack @property @@ -3163,7 +3271,7 @@ class ASTType(ASTBase): return self.decl.function_params def get_id(self, version, objectType=None, symbol=None): - # type: (int, unicode, Symbol) -> unicode + # type: (int, str, Symbol) -> str if version == 1: res = [] if objectType: # needs the name @@ -3186,7 +3294,7 @@ class ASTType(ASTBase): res.append(self.declSpecs.get_id(version)) res.append(self.decl.get_ptr_suffix_id(version)) res.append(self.decl.get_param_id(version)) - return u''.join(res) + return ''.join(res) # other versions res = [] if objectType: # needs the name @@ -3213,30 +3321,31 @@ class ASTType(ASTBase): returnTypeId = self.declSpecs.get_id(version) typeId = self.decl.get_type_id(version, returnTypeId) res.append(typeId) - return u''.join(res) + return ''.join(res) def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str res = [] declSpecs = transform(self.declSpecs) res.append(declSpecs) if self.decl.require_space_after_declSpecs() and len(declSpecs) > 0: - res.append(u' ') + res.append(' ') res.append(transform(self.decl)) - return u''.join(res) + return ''.join(res) def get_type_declaration_prefix(self): - # type: () -> unicode + # type: () -> str if self.declSpecs.trailingTypeSpec: return 'typedef' else: return 'type' def describe_signature(self, signode, mode, env, symbol): - # type: (addnodes.desc_signature, unicode, BuildEnvironment, Symbol) -> None + # type: (addnodes.desc_signature, str, BuildEnvironment, Symbol) -> None _verify_description_mode(mode) self.declSpecs.describe_signature(signode, 'markType', env, symbol) if (self.decl.require_space_after_declSpecs() and - len(text_type(self.declSpecs)) > 0): + len(str(self.declSpecs)) > 0): signode += nodes.Text(' ') # for parameters that don't really declare new names we get 'markType', # this should not be propagated, but be 'noneIsName'. @@ -3258,26 +3367,28 @@ class ASTTypeWithInit(ASTBase): @property def isPack(self): + # type: () -> bool return self.type.isPack def get_id(self, version, objectType=None, symbol=None): - # type: (int, unicode, Symbol) -> unicode + # type: (int, str, Symbol) -> str if objectType != 'member': return self.type.get_id(version, objectType) if version == 1: - return symbol.get_full_nested_name().get_id(version) + u'__' \ - + self.type.get_id(version) + return (symbol.get_full_nested_name().get_id(version) + '__' + + self.type.get_id(version)) return symbol.get_full_nested_name().get_id(version) def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str res = [] res.append(transform(self.type)) if self.init: res.append(transform(self.init)) - return u''.join(res) + return ''.join(res) def describe_signature(self, signode, mode, env, symbol): - # type: (addnodes.desc_signature, unicode, BuildEnvironment, Symbol) -> None + # type: (addnodes.desc_signature, str, BuildEnvironment, Symbol) -> None _verify_description_mode(mode) self.type.describe_signature(signode, mode, env, symbol) if self.init: @@ -3291,25 +3402,26 @@ class ASTTypeUsing(ASTBase): self.type = type def get_id(self, version, objectType=None, symbol=None): - # type: (int, unicode, Symbol) -> unicode + # type: (int, str, Symbol) -> str if version == 1: raise NoOldIdError() return symbol.get_full_nested_name().get_id(version) def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str res = [] res.append(transform(self.name)) if self.type: res.append(' = ') res.append(transform(self.type)) - return u''.join(res) + return ''.join(res) def get_type_declaration_prefix(self): - # type: () -> unicode + # type: () -> str return 'using' def describe_signature(self, signode, mode, env, symbol): - # type: (addnodes.desc_signature, unicode, BuildEnvironment, Symbol) -> None + # type: (addnodes.desc_signature, str, BuildEnvironment, Symbol) -> None _verify_description_mode(mode) self.name.describe_signature(signode, mode, env, symbol=symbol) if self.type: @@ -3329,19 +3441,20 @@ class ASTConcept(ASTBase): return self.nestedName def get_id(self, version, objectType=None, symbol=None): - # type: (int, unicode, Symbol) -> unicode + # type: (int, str, Symbol) -> str if version == 1: raise NoOldIdError() return symbol.get_full_nested_name().get_id(version) def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str res = transform(self.nestedName) if self.initializer: res += transform(self.initializer) return res def describe_signature(self, signode, mode, env, symbol): - # type: (addnodes.desc_signature, unicode, BuildEnvironment, Symbol) -> None + # type: (addnodes.desc_signature, str, BuildEnvironment, Symbol) -> None self.nestedName.describe_signature(signode, mode, env, symbol) if self.initializer: self.initializer.describe_signature(signode, mode, env, symbol) @@ -3349,15 +3462,17 @@ class ASTConcept(ASTBase): class ASTBaseClass(ASTBase): def __init__(self, name, visibility, virtual, pack): - # type: (Any, unicode, bool, bool) -> None + # type: (ASTNestedName, str, bool, bool) -> None self.name = name self.visibility = visibility self.virtual = virtual self.pack = pack def _stringify(self, transform): - res = [] # type: List[unicode] - if self.visibility != 'private': + # type: (Callable[[Any], str]) -> str + res = [] + + if self.visibility is not None: res.append(self.visibility) res.append(' ') if self.virtual: @@ -3365,12 +3480,12 @@ class ASTBaseClass(ASTBase): res.append(transform(self.name)) if self.pack: res.append('...') - return u''.join(res) + return ''.join(res) def describe_signature(self, signode, mode, env, symbol): - # type: (addnodes.desc_signature, unicode, BuildEnvironment, Symbol) -> None + # type: (addnodes.desc_signature, str, BuildEnvironment, Symbol) -> None _verify_description_mode(mode) - if self.visibility != 'private': + if self.visibility is not None: signode += addnodes.desc_annotation(self.visibility, self.visibility) signode += nodes.Text(' ') @@ -3384,16 +3499,17 @@ class ASTBaseClass(ASTBase): class ASTClass(ASTBase): def __init__(self, name, final, bases): - # type: (Any, bool, List[Any]) -> None + # type: (ASTNestedName, bool, List[ASTBaseClass]) -> None self.name = name self.final = final self.bases = bases def get_id(self, version, objectType, symbol): - # type: (int, unicode, Symbol) -> unicode + # type: (int, str, Symbol) -> str return symbol.get_full_nested_name().get_id(version) def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str res = [] res.append(transform(self.name)) if self.final: @@ -3406,10 +3522,10 @@ class ASTClass(ASTBase): res.append(', ') first = False res.append(transform(b)) - return u''.join(res) + return ''.join(res) def describe_signature(self, signode, mode, env, symbol): - # type: (addnodes.desc_signature, unicode, BuildEnvironment, Symbol) -> None + # type: (addnodes.desc_signature, str, BuildEnvironment, Symbol) -> None _verify_description_mode(mode) self.name.describe_signature(signode, mode, env, symbol=symbol) if self.final: @@ -3429,35 +3545,37 @@ class ASTUnion(ASTBase): self.name = name def get_id(self, version, objectType, symbol): - # type: (int, unicode, Symbol) -> unicode + # type: (int, str, Symbol) -> str if version == 1: raise NoOldIdError() return symbol.get_full_nested_name().get_id(version) def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str return transform(self.name) def describe_signature(self, signode, mode, env, symbol): - # type: (addnodes.desc_signature, unicode, BuildEnvironment, Symbol) -> None + # type: (addnodes.desc_signature, str, BuildEnvironment, Symbol) -> None _verify_description_mode(mode) self.name.describe_signature(signode, mode, env, symbol=symbol) class ASTEnum(ASTBase): def __init__(self, name, scoped, underlyingType): - # type: (Any, unicode, Any) -> None + # type: (Any, str, Any) -> None self.name = name self.scoped = scoped self.underlyingType = underlyingType def get_id(self, version, objectType, symbol): - # type: (int, unicode, Symbol) -> unicode + # type: (int, str, Symbol) -> str if version == 1: raise NoOldIdError() return symbol.get_full_nested_name().get_id(version) def _stringify(self, transform): - res = [] # type: List[unicode] + # type: (Callable[[Any], str]) -> str + res = [] if self.scoped: res.append(self.scoped) res.append(' ') @@ -3465,10 +3583,10 @@ class ASTEnum(ASTBase): if self.underlyingType: res.append(' : ') res.append(transform(self.underlyingType)) - return u''.join(res) + return ''.join(res) def describe_signature(self, signode, mode, env, symbol): - # type: (addnodes.desc_signature, unicode, BuildEnvironment, Symbol) -> None + # type: (addnodes.desc_signature, str, BuildEnvironment, Symbol) -> None _verify_description_mode(mode) # self.scoped has been done by the CPPEnumObject self.name.describe_signature(signode, mode, env, symbol=symbol) @@ -3485,20 +3603,21 @@ class ASTEnumerator(ASTBase): self.init = init def get_id(self, version, objectType, symbol): - # type: (int, unicode, Symbol) -> unicode + # type: (int, str, Symbol) -> str if version == 1: raise NoOldIdError() return symbol.get_full_nested_name().get_id(version) def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str res = [] res.append(transform(self.name)) if self.init: res.append(transform(self.init)) - return u''.join(res) + return ''.join(res) def describe_signature(self, signode, mode, env, symbol): - # type: (addnodes.desc_signature, unicode, BuildEnvironment, Symbol) -> None + # type: (addnodes.desc_signature, str, BuildEnvironment, Symbol) -> None _verify_description_mode(mode) self.name.describe_signature(signode, mode, env, symbol) if self.init: @@ -3506,16 +3625,17 @@ class ASTEnumerator(ASTBase): class ASTDeclaration(ASTBase): - def __init__(self, objectType, visibility, templatePrefix, declaration): - # type: (unicode, unicode, Any, Any) -> None + def __init__(self, objectType, directiveType, visibility, templatePrefix, declaration): + # type: (str, str, str, ASTTemplateDeclarationPrefix, Any) -> None self.objectType = objectType + self.directiveType = directiveType self.visibility = visibility self.templatePrefix = templatePrefix self.declaration = declaration self.symbol = None # type: Symbol # set by CPPObject._add_enumerator_to_parent - self.enumeratorScopedSymbol = None # type: Any + self.enumeratorScopedSymbol = None # type: Symbol def clone(self): # type: () -> ASTDeclaration @@ -3523,8 +3643,8 @@ class ASTDeclaration(ASTBase): templatePrefixClone = self.templatePrefix.clone() else: templatePrefixClone = None - return ASTDeclaration(self.objectType, self.visibility, - templatePrefixClone, + return ASTDeclaration(self.objectType, self.directiveType, + self.visibility, templatePrefixClone, self.declaration.clone()) @property @@ -3540,7 +3660,7 @@ class ASTDeclaration(ASTBase): return self.declaration.function_params def get_id(self, version, prefixed=True): - # type: (int, bool) -> unicode + # type: (int, bool) -> str if version == 1: if self.templatePrefix: raise NoOldIdError() @@ -3557,24 +3677,25 @@ class ASTDeclaration(ASTBase): if self.templatePrefix: res.append(self.templatePrefix.get_id(version)) res.append(self.declaration.get_id(version, self.objectType, self.symbol)) - return u''.join(res) + return ''.join(res) def get_newest_id(self): - # type: () -> unicode + # type: () -> str return self.get_id(_max_id, True) def _stringify(self, transform): - res = [] # type: List[unicode] + # type: (Callable[[Any], str]) -> str + res = [] if self.visibility and self.visibility != "public": res.append(self.visibility) - res.append(u' ') + res.append(' ') if self.templatePrefix: res.append(transform(self.templatePrefix)) res.append(transform(self.declaration)) - return u''.join(res) + return ''.join(res) def describe_signature(self, signode, mode, env, options): - # type: (addnodes.desc_signature, unicode, BuildEnvironment, Dict) -> None + # type: (addnodes.desc_signature, str, BuildEnvironment, Dict) -> None _verify_description_mode(mode) assert self.symbol # The caller of the domain added a desc_signature node. @@ -3604,14 +3725,20 @@ class ASTDeclaration(ASTBase): elif self.objectType == 'function': pass elif self.objectType == 'class': - mainDeclNode += addnodes.desc_annotation('class ', 'class ') + assert self.directiveType in ('class', 'struct') + prefix = self.directiveType + ' ' + mainDeclNode += addnodes.desc_annotation(prefix, prefix) elif self.objectType == 'union': mainDeclNode += addnodes.desc_annotation('union ', 'union ') elif self.objectType == 'enum': - prefix = 'enum ' - if self.scoped: # type: ignore - prefix += self.scoped # type: ignore - prefix += ' ' + if self.directiveType == 'enum': + prefix = 'enum ' + elif self.directiveType == 'enum-class': + prefix = 'enum class ' + elif self.directiveType == 'enum-struct': + prefix = 'enum struct ' + else: + assert False # wrong directiveType used mainDeclNode += addnodes.desc_annotation(prefix, prefix) elif self.objectType == 'enumerator': mainDeclNode += addnodes.desc_annotation('enumerator ', 'enumerator ') @@ -3626,8 +3753,16 @@ class ASTNamespace(ASTBase): self.nestedName = nestedName self.templatePrefix = templatePrefix + def _stringify(self, transform): + # type: (Callable[[Any], str]) -> str + res = [] + if self.templatePrefix: + res.append(transform(self.templatePrefix)) + res.append(transform(self.nestedName)) + return ''.join(res) + -class SymbolLookupResult(object): +class SymbolLookupResult: def __init__(self, symbols, parentSymbol, identOrOp, templateParams, templateArgs): # type: (Iterator[Symbol], Symbol, Union[ASTIdentifier, ASTOperator], Any, ASTTemplateArgs) -> None # NOQA self.symbols = symbols @@ -3637,7 +3772,7 @@ class SymbolLookupResult(object): self.templateArgs = templateArgs -class Symbol(object): +class Symbol: debug_lookup = False debug_show_tree = False @@ -3658,7 +3793,7 @@ class Symbol(object): if key == "children": assert False else: - return object.__setattr__(self, key, value) + return super().__setattr__(key, value) def __init__(self, parent, # type: Symbol @@ -3666,7 +3801,7 @@ class Symbol(object): templateParams, # type: Any templateArgs, # type: Any declaration, # type: ASTDeclaration - docname # type: unicode + docname # type: str ): # type: (...) -> None self.parent = parent @@ -3691,7 +3826,7 @@ class Symbol(object): self._add_template_and_function_params() def _fill_empty(self, declaration, docname): - # type: (ASTDeclaration, unicode) -> None + # type: (ASTDeclaration, str) -> None self._assert_invariants() assert not self.declaration assert not self.docname @@ -3715,7 +3850,7 @@ class Symbol(object): continue # only add a declaration if we our self are from a declaration if self.declaration: - decl = ASTDeclaration('templateParam', None, None, p) + decl = ASTDeclaration('templateParam', None, None, None, p) else: decl = None nne = ASTNestedNameElement(p.get_identifier(), None) @@ -3730,7 +3865,7 @@ class Symbol(object): if nn is None: continue # (comparing to the template params: we have checked that we are a declaration) - decl = ASTDeclaration('functionParam', None, None, p) + decl = ASTDeclaration('functionParam', None, None, None, p) assert not nn.rooted assert len(nn.names) == 1 self._add_symbols(nn, [], decl, self.docname) @@ -3743,7 +3878,7 @@ class Symbol(object): self.parent = None def clear_doc(self, docname): - # type: (unicode) -> None + # type: (str) -> None newChildren = [] for sChild in self._children: sChild.clear_doc(docname) @@ -3766,9 +3901,8 @@ class Symbol(object): yield c if not c.identOrOp.is_anon(): continue - # TODO: change to 'yield from' when Python 2 support is dropped - for nested in c.children_recurse_anon: - yield nested + + yield from c.children_recurse_anon def get_lookup_key(self): # type: () -> List[Tuple[ASTNestedNameElement, Any]] @@ -3835,8 +3969,8 @@ class Symbol(object): param = templateParams.params[i] arg = templateArgs.args[i] # TODO: doing this by string manipulation is probably not the most efficient - paramName = text_type(param.name) - argTxt = text_type(arg) + paramName = str(param.name) + argTxt = str(arg) isArgPackExpansion = argTxt.endswith('...') if param.isPack != isArgPackExpansion: return True @@ -3864,13 +3998,13 @@ class Symbol(object): return False if templateParams: # TODO: do better comparison - if text_type(s.templateParams) != text_type(templateParams): + if str(s.templateParams) != str(templateParams): return False if (s.templateArgs is None) != (templateArgs is None): return False if s.templateArgs: # TODO: do better comparison - if text_type(s.templateArgs) != text_type(templateArgs): + if str(s.templateArgs) != str(templateArgs): return False return True if matchSelf and matches(self): @@ -3887,7 +4021,7 @@ class Symbol(object): onMissingQualifiedSymbol, # type: Callable[[Symbol, Union[ASTIdentifier, ASTOperator], Any, ASTTemplateArgs], Symbol] # NOQA strictTemplateParamArgLists, # type: bool - ancestorLookupType, # type: unicode + ancestorLookupType, # type: str templateShorthand, # type: bool matchSelf, # type: bool recurseInAnon, # type: bool @@ -3988,7 +4122,7 @@ class Symbol(object): identOrOp, templateParams, templateArgs) def _add_symbols(self, nestedName, templateDecls, declaration, docname): - # type: (ASTNestedName, List[Any], ASTDeclaration, unicode) -> Symbol + # type: (ASTNestedName, List[Any], ASTDeclaration, str) -> Symbol # Used for adding a whole path of symbols, where the last may or may not # be an actual declaration. @@ -4140,7 +4274,7 @@ class Symbol(object): return symbol def merge_with(self, other, docnames, env): - # type: (Symbol, List[unicode], BuildEnvironment) -> None + # type: (Symbol, List[str], BuildEnvironment) -> None assert other is not None for otherChild in other._children: ourChild = self._find_first_named_symbol( @@ -4159,7 +4293,7 @@ class Symbol(object): if not ourChild.declaration: ourChild._fill_empty(otherChild.declaration, otherChild.docname) elif ourChild.docname != otherChild.docname: - name = text_type(ourChild.declaration) + name = str(ourChild.declaration) msg = __("Duplicate declaration, also defined in '%s'.\n" "Declaration is '%s'.") msg = msg % (ourChild.docname, name) @@ -4181,7 +4315,7 @@ class Symbol(object): declaration=None, docname=None) def add_declaration(self, declaration, docname): - # type: (ASTDeclaration, unicode) -> Symbol + # type: (ASTDeclaration, str) -> Symbol assert declaration assert docname nestedName = declaration.name @@ -4219,7 +4353,7 @@ class Symbol(object): def find_name(self, nestedName, templateDecls, typ, templateShorthand, matchSelf, recurseInAnon): - # type: (ASTNestedName, List[Any], unicode, bool, bool, bool) -> Symbol + # type: (ASTNestedName, List[Any], str, bool, bool, bool) -> List[Symbol] # templateShorthand: missing template parameter lists for templates is ok def onMissingQualifiedSymbol(parentSymbol, identOrOp, templateParams, templateArgs): @@ -4242,22 +4376,23 @@ class Symbol(object): # if it was a part of the qualification that could not be found return None - # TODO: hmm, what if multiple symbols match? - try: - return next(lookupResult.symbols) - except StopIteration: - pass + res = list(lookupResult.symbols) + if len(res) != 0: + return res # try without template params and args symbol = lookupResult.parentSymbol._find_first_named_symbol( lookupResult.identOrOp, None, None, templateShorthand=templateShorthand, matchSelf=matchSelf, recurseInAnon=recurseInAnon, correctPrimaryTemplateArgs=False) - return symbol + if symbol is not None: + return [symbol] + else: + return None def find_declaration(self, declaration, typ, templateShorthand, matchSelf, recurseInAnon): - # type: (ASTDeclaration, unicode, bool, bool, bool) -> Symbol + # type: (ASTDeclaration, str, bool, bool, bool) -> Symbol # templateShorthand: missing template parameter lists for templates is ok nestedName = declaration.name if declaration.templatePrefix: @@ -4293,6 +4428,8 @@ class Symbol(object): docname='fakeDocnameForQuery') queryId = declaration.get_newest_id() for symbol in symbols: + if symbol.declaration is None: + continue candId = symbol.declaration.get_newest_id() if candId == queryId: querySymbol.remove() @@ -4301,26 +4438,26 @@ class Symbol(object): return None def to_string(self, indent): - # type: (int) -> unicode - res = ['\t' * indent] # type: List[unicode] + # type: (int) -> str + res = ['\t' * indent] if not self.parent: res.append('::') else: if self.templateParams: - res.append(text_type(self.templateParams)) + res.append(str(self.templateParams)) res.append('\n') res.append('\t' * indent) if self.identOrOp: - res.append(text_type(self.identOrOp)) + res.append(str(self.identOrOp)) else: - res.append(text_type(self.declaration)) + res.append(str(self.declaration)) if self.templateArgs: - res.append(text_type(self.templateArgs)) + res.append(str(self.templateArgs)) if self.declaration: res.append(": ") if self.isRedeclaration: res.append('!!duplicate!! ') - res.append(text_type(self.declaration)) + res.append(str(self.declaration)) if self.docname: res.append('\t(') res.append(self.docname) @@ -4329,16 +4466,16 @@ class Symbol(object): return ''.join(res) def dump(self, indent): - # type: (int) -> unicode + # type: (int) -> str res = [self.to_string(indent)] for c in self._children: res.append(c.dump(indent + 1)) return ''.join(res) -class DefinitionParser(object): +class DefinitionParser: # those without signedness and size modifiers - # see http://en.cppreference.com/w/cpp/language/types + # see https://en.cppreference.com/w/cpp/language/types _simple_fundemental_types = ( 'void', 'bool', 'char', 'wchar_t', 'char16_t', 'char32_t', 'int', 'float', 'double', 'auto' @@ -4361,36 +4498,36 @@ class DefinitionParser(object): self.config = config def _make_multi_error(self, errors, header): - # type: (List[Any], unicode) -> DefinitionError + # type: (List[Any], str) -> DefinitionError if len(errors) == 1: if len(header) > 0: - return DefinitionError(header + '\n' + errors[0][0].description) + return DefinitionError(header + '\n' + str(errors[0][0])) else: - return DefinitionError(errors[0][0].description) + return DefinitionError(str(errors[0][0])) result = [header, '\n'] for e in errors: if len(e[1]) > 0: ident = ' ' result.append(e[1]) result.append(':\n') - for line in e[0].description.split('\n'): + for line in str(e[0]).split('\n'): if len(line) == 0: continue result.append(ident) result.append(line) result.append('\n') else: - result.append(e[0].description) + result.append(str(e[0])) return DefinitionError(''.join(result)) def status(self, msg): - # type: (unicode) -> None + # type: (str) -> None # for debugging indicator = '-' * self.pos + '^' print("%s\n%s\n%s" % (msg, self.definition, indicator)) def fail(self, msg): - # type: (unicode) -> None + # type: (str) -> None errors = [] indicator = '-' * self.pos + '^' exMain = DefinitionError( @@ -4403,7 +4540,7 @@ class DefinitionParser(object): raise self._make_multi_error(errors, '') def warn(self, msg): - # type: (unicode) -> None + # type: (str) -> None if self.warnEnv: self.warnEnv.warn(msg) else: @@ -4424,7 +4561,7 @@ class DefinitionParser(object): self.pos, self.last_match = self._previous_state def skip_string(self, string): - # type: (unicode) -> bool + # type: (str) -> bool strlen = len(string) if self.definition[self.pos:self.pos + strlen] == string: self.pos += strlen @@ -4432,7 +4569,7 @@ class DefinitionParser(object): return False def skip_word(self, word): - # type: (unicode) -> bool + # type: (str) -> bool return self.match(re.compile(r'\b%s\b' % re.escape(word))) def skip_ws(self): @@ -4440,14 +4577,14 @@ class DefinitionParser(object): return self.match(_whitespace_re) def skip_word_and_ws(self, word): - # type: (unicode) -> bool + # type: (str) -> bool if self.skip_word(word): self.skip_ws() return True return False def skip_string_and_ws(self, string): - # type: (unicode) -> bool + # type: (str) -> bool if self.skip_string(string): self.skip_ws() return True @@ -4460,7 +4597,7 @@ class DefinitionParser(object): @property def current_char(self): - # type: () -> unicode + # type: () -> str try: return self.definition[self.pos] except IndexError: @@ -4468,14 +4605,14 @@ class DefinitionParser(object): @property def matched_text(self): - # type: () -> unicode + # type: () -> str if self.last_match is not None: return self.last_match.group() else: return None def read_rest(self): - # type: () -> unicode + # type: () -> str rv = self.definition[self.pos:] self.pos = self.end return rv @@ -4506,11 +4643,11 @@ class DefinitionParser(object): return self.definition[startPos:self.pos] def _parse_balanced_token_seq(self, end): - # type: (List[unicode]) -> unicode + # type: (List[str]) -> str # TODO: add handling of string literals and similar - brackets = {'(': ')', '[': ']', '{': '}'} # type: Dict[unicode, unicode] + brackets = {'(': ')', '[': ']', '{': '}'} startPos = self.pos - symbols = [] # type: List[unicode] + symbols = [] # type: List[str] while not self.eof: if len(symbols) == 0 and self.current_char in end: break @@ -4708,7 +4845,7 @@ class DefinitionParser(object): return self._parse_nested_name() def _parse_expression_list_or_braced_init_list(self): - # type: () -> Tuple[List[Any], unicode] + # type: () -> Tuple[List[Any], str] self.skip_ws() if self.skip_string_and_ws('('): close = ')' @@ -5120,7 +5257,7 @@ class DefinitionParser(object): if not allow or not self.allowFallbackExpressionParsing: raise self.warn("Parsing of expression failed. Using fallback parser." - " Error was:\n%s" % e.description) + " Error was:\n%s" % e) self.pos = prevPos # and then the fallback scanning assert end is not None @@ -5130,8 +5267,8 @@ class DefinitionParser(object): value = self.matched_text else: # TODO: add handling of more bracket-like things, and quote handling - brackets = {'(': ')', '[': ']', '<': '>'} # type: Dict[unicode, unicode] - symbols = [] # type: List[unicode] + brackets = {'(': ')', '[': ']', '<': '>'} + symbols = [] # type: List[str] while not self.eof: if (len(symbols) == 0 and self.current_char in end): break @@ -5304,7 +5441,7 @@ class DefinitionParser(object): elif self.skip_word_and_ws('double'): elements.append('double') if len(elements) > 0: - return ASTTrailingTypeSpecFundamental(u' '.join(elements)) + return ASTTrailingTypeSpecFundamental(' '.join(elements)) # decltype self.skip_ws() @@ -5333,7 +5470,7 @@ class DefinitionParser(object): return ASTTrailingTypeSpecName(prefix, nestedName) def _parse_parameters_and_qualifiers(self, paramMode): - # type: (unicode) -> ASTParametersQualifiers + # type: (str) -> ASTParametersQualifiers if paramMode == 'new': return None self.skip_ws() @@ -5418,14 +5555,14 @@ class DefinitionParser(object): if not initializer: self.fail( 'Expected "%s" in initializer-specifier.' - % u'" or "'.join(valid)) + % '" or "'.join(valid)) return ASTParametersQualifiers( args, volatile, const, refQual, exceptionSpec, override, final, initializer) def _parse_decl_specs_simple(self, outer, typed): - # type: (unicode, bool) -> ASTDeclSpecsSimple + # type: (str, bool) -> ASTDeclSpecsSimple """Just parse the simple ones.""" storage = None threadLocal = None @@ -5500,7 +5637,7 @@ class DefinitionParser(object): friend, attrs) def _parse_decl_specs(self, outer, typed=True): - # type: (unicode, bool) -> ASTDeclSpecs + # type: (str, bool) -> ASTDeclSpecs if outer: if outer not in ('type', 'member', 'function', 'templateParam'): raise Exception('Internal error, unknown outer "%s".' % outer) @@ -5528,7 +5665,7 @@ class DefinitionParser(object): return ASTDeclSpecs(outer, leftSpecs, rightSpecs, trailing) def _parse_declarator_name_param_qual(self, named, paramMode, typed): - # type: (Union[bool, unicode], unicode, bool) -> ASTDeclaratorNameParamQual + # type: (Union[bool, str], str, bool) -> ASTDeclaratorNameParamQual # now we should parse the name, and then suffixes if named == 'maybe': pos = self.pos @@ -5575,7 +5712,7 @@ class DefinitionParser(object): paramQual=paramQual) def _parse_declarator(self, named, paramMode, typed=True): - # type: (Union[bool, unicode], unicode, bool) -> Any + # type: (Union[bool, str], str, bool) -> Any # 'typed' here means 'parse return type stuff' if paramMode not in ('type', 'function', 'operatorCast', 'new'): raise Exception( @@ -5687,7 +5824,7 @@ class DefinitionParser(object): raise self._make_multi_error(prevErrors, header) def _parse_initializer(self, outer=None, allowFallback=True): - # type: (unicode, bool) -> ASTInitializer + # type: (str, bool) -> ASTInitializer self.skip_ws() # TODO: support paren and brace initialization for memberObject if not self.skip_string('='): @@ -5714,7 +5851,7 @@ class DefinitionParser(object): return ASTInitializer(value) def _parse_type(self, named, outer=None): - # type: (Union[bool, unicode], unicode) -> ASTType + # type: (Union[bool, str], str) -> ASTType """ named=False|'maybe'|True: 'maybe' is e.g., for function objects which doesn't need to name the arguments @@ -5797,7 +5934,7 @@ class DefinitionParser(object): return ASTType(declSpecs, decl) def _parse_type_with_init(self, named, outer): - # type: (Union[bool, unicode], unicode) -> Any + # type: (Union[bool, str], str) -> Any if outer: assert outer in ('type', 'member', 'function', 'templateParam') type = self._parse_type(outer=outer, named=named) @@ -5866,7 +6003,7 @@ class DefinitionParser(object): if self.skip_string(':'): while 1: self.skip_ws() - visibility = 'private' # type: unicode + visibility = None virtual = False pack = False if self.skip_word_and_ws('virtual'): @@ -5894,7 +6031,7 @@ class DefinitionParser(object): def _parse_enum(self): # type: () -> ASTEnum - scoped = None # type: unicode # is set by CPPEnumObject + scoped = None # is set by CPPEnumObject self.skip_ws() name = self._parse_nested_name() self.skip_ws() @@ -6029,8 +6166,8 @@ class DefinitionParser(object): return ASTTemplateIntroduction(concept, params) def _parse_template_declaration_prefix(self, objectType): - # type: (unicode) -> ASTTemplateDeclarationPrefix - templates = [] # type: List + # type: (str) -> ASTTemplateDeclarationPrefix + templates = [] # type: List[str] while 1: self.skip_ws() # the saved position is only used to provide a better error message @@ -6085,11 +6222,11 @@ class DefinitionParser(object): msg = "Too many template argument lists compared to parameter" \ " lists. Argument lists: %d, Parameter lists: %d," \ " Extra empty parameters lists prepended: %d." \ - % (numArgs, numParams, numExtra) # type: unicode + % (numArgs, numParams, numExtra) msg += " Declaration:\n\t" if templatePrefix: - msg += "%s\n\t" % text_type(templatePrefix) - msg += text_type(nestedName) + msg += "%s\n\t" % templatePrefix + msg += str(nestedName) self.warn(msg) newTemplates = [] @@ -6100,11 +6237,15 @@ class DefinitionParser(object): templatePrefix = ASTTemplateDeclarationPrefix(newTemplates) return templatePrefix - def parse_declaration(self, objectType): - # type: (unicode) -> ASTDeclaration - if objectType not in ('type', 'concept', 'member', - 'function', 'class', 'union', 'enum', 'enumerator'): + def parse_declaration(self, objectType, directiveType): + # type: (str, str) -> ASTDeclaration + if objectType not in ('class', 'union', 'function', 'member', 'type', + 'concept', 'enum', 'enumerator'): raise Exception('Internal error, unknown objectType "%s".' % objectType) + if directiveType not in ('class', 'struct', 'union', 'function', 'member', 'var', + 'type', 'concept', + 'enum', 'enum-struct', 'enum-class', 'enumerator'): + raise Exception('Internal error, unknown directiveType "%s".' % directiveType) visibility = None templatePrefix = None declaration = None # type: Any @@ -6154,7 +6295,7 @@ class DefinitionParser(object): templatePrefix, fullSpecShorthand=False, isMember=objectType == 'member') - return ASTDeclaration(objectType, visibility, + return ASTDeclaration(objectType, directiveType, visibility, templatePrefix, declaration) def parse_namespace_object(self): @@ -6184,7 +6325,7 @@ class DefinitionParser(object): except DefinitionError as e1: try: self.pos = pos - res2 = self.parse_declaration('function') + res2 = self.parse_declaration('function', 'function') # if there are '()' left, just skip them self.skip_ws() self.skip_string('()') @@ -6244,11 +6385,11 @@ class CPPObject(ObjectDescription): option_spec['tparam-line-spec'] = directives.flag def warn(self, msg): - # type: (unicode) -> None + # type: (Union[str, Exception]) -> None self.state_machine.reporter.warning(msg, line=self.lineno) def _add_enumerator_to_parent(self, ast): - # type: (Any) -> None + # type: (ASTDeclaration) -> None assert ast.objectType == 'enumerator' # find the parent, if it exists && is an enum # && it's unscoped, @@ -6274,7 +6415,7 @@ class CPPObject(ObjectDescription): # TODO: maybe issue a warning, enumerators in non-enums is weird, # but it is somewhat equivalent to unscoped enums, without the enum return - if parentDecl.scoped: + if parentDecl.directiveType != 'enum': return targetSymbol = parentSymbol.parent @@ -6290,7 +6431,7 @@ class CPPObject(ObjectDescription): docname=self.env.docname) def add_target_and_index(self, ast, sig, signode): - # type: (Any, unicode, addnodes.desc_signature) -> None + # type: (ASTDeclaration, str, addnodes.desc_signature) -> None # general note: name must be lstrip(':')'ed, to remove "::" ids = [] for i in range(1, _max_id + 1): @@ -6305,7 +6446,7 @@ class CPPObject(ObjectDescription): assert newestId # shouldn't be None if not re.compile(r'^[a-zA-Z0-9_]*$').match(newestId): self.warn('Index id generation for C++ object "%s" failed, please ' - 'report as bug (id=%s).' % (text_type(ast), newestId)) + 'report as bug (id=%s).' % (ast, newestId)) name = ast.symbol.get_full_nested_name().get_display_string().lstrip(':') # Add index entry, but not if it's a declaration inside a concept @@ -6349,10 +6490,24 @@ class CPPObject(ObjectDescription): signode['first'] = (not self.names) # hmm, what is this about? self.state.document.note_explicit_target(signode) - def parse_definition(self, parser): - # type: (Any) -> Any + @property + def object_type(self): + # type: () -> str raise NotImplementedError() + @property + def display_object_type(self): + # type: () -> str + return self.object_type + + def get_index_text(self, name): + # type: (str) -> str + return _('%s (C++ %s)') % (name, self.display_object_type) + + def parse_definition(self, parser): + # type: (DefinitionParser) -> ASTDeclaration + return parser.parse_declaration(self.object_type, self.objtype) + def describe_signature(self, signode, ast, options): # type: (addnodes.desc_signature, Any, Dict) -> None ast.describe_signature(signode, 'lastIsName', self.env, options) @@ -6379,15 +6534,15 @@ class CPPObject(ObjectDescription): parentDecl = parentSymbol.declaration if parentDecl is not None and parentDecl.objectType == 'function': self.warn("C++ declarations inside functions are not supported." + - " Parent function is " + text_type(parentSymbol.get_full_nested_name())) + " Parent function is " + str(parentSymbol.get_full_nested_name())) name = _make_phony_error_name() symbol = parentSymbol.add_name(name) env.temp_data['cpp:last_symbol'] = symbol return [] - return ObjectDescription.run(self) + return super().run() def handle_signature(self, sig, signode): - # type: (unicode, addnodes.desc_signature) -> Any + # type: (str, addnodes.desc_signature) -> ASTDeclaration parentSymbol = self.env.temp_data['cpp:parent_symbol'] parser = DefinitionParser(sig, self, self.env.config) @@ -6395,7 +6550,7 @@ class CPPObject(ObjectDescription): ast = self.parse_definition(parser) parser.assert_end() except DefinitionError as e: - self.warn(e.description) + self.warn(e) # It is easier to assume some phony name than handling the error in # the possibly inner declarations. name = _make_phony_error_name() @@ -6438,93 +6593,41 @@ class CPPObject(ObjectDescription): class CPPTypeObject(CPPObject): - def get_index_text(self, name): - # type: (unicode) -> unicode - return _('%s (C++ type)') % name - - def parse_definition(self, parser): - # type: (Any) -> Any - return parser.parse_declaration("type") + object_type = 'type' class CPPConceptObject(CPPObject): - def get_index_text(self, name): - # type: (unicode) -> unicode - return _('%s (C++ concept)') % name - - def parse_definition(self, parser): - # type: (Any) -> Any - return parser.parse_declaration("concept") + object_type = 'concept' class CPPMemberObject(CPPObject): - def get_index_text(self, name): - # type: (unicode) -> unicode - return _('%s (C++ member)') % name - - def parse_definition(self, parser): - # type: (Any) -> Any - return parser.parse_declaration("member") + object_type = 'member' class CPPFunctionObject(CPPObject): - def get_index_text(self, name): - # type: (unicode) -> unicode - return _('%s (C++ function)') % name - - def parse_definition(self, parser): - # type: (Any) -> Any - return parser.parse_declaration("function") + object_type = 'function' class CPPClassObject(CPPObject): - def get_index_text(self, name): - # type: (unicode) -> unicode - return _('%s (C++ class)') % name + object_type = 'class' - def parse_definition(self, parser): - # type: (Any) -> Any - return parser.parse_declaration("class") + @property + def display_object_type(self): + # the distinction between class and struct is only cosmetic + assert self.objtype in ('class', 'struct') + return self.objtype class CPPUnionObject(CPPObject): - def get_index_text(self, name): - # type: (unicode) -> unicode - return _('%s (C++ union)') % name - - def parse_definition(self, parser): - # type: (Any) -> Any - return parser.parse_declaration("union") + object_type = 'union' class CPPEnumObject(CPPObject): - def get_index_text(self, name): - # type: (unicode) -> unicode - return _('%s (C++ enum)') % name - - def parse_definition(self, parser): - # type: (Any) -> Any - ast = parser.parse_declaration("enum") - # self.objtype is set by ObjectDescription in run() - if self.objtype == "enum": - ast.scoped = None - elif self.objtype == "enum-struct": - ast.scoped = "struct" - elif self.objtype == "enum-class": - ast.scoped = "class" - else: - assert False - return ast + object_type = 'enum' class CPPEnumeratorObject(CPPObject): - def get_index_text(self, name): - # type: (unicode) -> unicode - return _('%s (C++ enumerator)') % name - - def parse_definition(self, parser): - # type: (Any) -> Any - return parser.parse_declaration("enumerator") + object_type = 'enumerator' class CPPNamespaceObject(SphinxDirective): @@ -6540,7 +6643,7 @@ class CPPNamespaceObject(SphinxDirective): option_spec = {} # type: Dict def warn(self, msg): - # type: (unicode) -> None + # type: (Union[str, Exception]) -> None self.state_machine.reporter.warning(msg, line=self.lineno) def run(self): @@ -6555,7 +6658,7 @@ class CPPNamespaceObject(SphinxDirective): ast = parser.parse_namespace_object() parser.assert_end() except DefinitionError as e: - self.warn(e.description) + self.warn(e) name = _make_phony_error_name() ast = ASTNamespace(name, None) symbol = rootSymbol.add_name(ast.nestedName, ast.templatePrefix) @@ -6574,7 +6677,7 @@ class CPPNamespacePushObject(SphinxDirective): option_spec = {} # type: Dict def warn(self, msg): - # type: (unicode) -> None + # type: (Union[str, Exception]) -> None self.state_machine.reporter.warning(msg, line=self.lineno) def run(self): @@ -6586,7 +6689,7 @@ class CPPNamespacePushObject(SphinxDirective): ast = parser.parse_namespace_object() parser.assert_end() except DefinitionError as e: - self.warn(e.description) + self.warn(e) name = _make_phony_error_name() ast = ASTNamespace(name, None) oldParent = self.env.temp_data.get('cpp:parent_symbol', None) @@ -6609,7 +6712,7 @@ class CPPNamespacePopObject(SphinxDirective): option_spec = {} # type: Dict def warn(self, msg): - # type: (unicode) -> None + # type: (Union[str, Exception]) -> None self.state_machine.reporter.warning(msg, line=self.lineno) def run(self): @@ -6630,9 +6733,146 @@ class CPPNamespacePopObject(SphinxDirective): return [] +class AliasNode(nodes.Element): + def __init__(self, sig, warnEnv): + """ + :param sig: The name or function signature to alias. + :param warnEnv: An object which must have the following attributes: + env: a Sphinx environment + whatever DefinitionParser requires of warnEnv + """ + super().__init__() + self.sig = sig + env = warnEnv.env + if 'cpp:parent_symbol' not in env.temp_data: + root = env.domaindata['cpp']['root_symbol'] + env.temp_data['cpp:parent_symbol'] = root + self.parentKey = env.temp_data['cpp:parent_symbol'].get_lookup_key() + try: + parser = DefinitionParser(sig, warnEnv, warnEnv.env.config) + self.ast, self.isShorthand = parser.parse_xref_object() + parser.assert_end() + except DefinitionError as e: + warnEnv.warn(e) + self.ast = None + + +class AliasTransform(SphinxTransform): + default_priority = ReferencesResolver.default_priority - 1 + + def apply(self, **kwargs): + # type: (Any) -> None + for node in self.document.traverse(AliasNode): + sig = node.sig + ast = node.ast + if ast is None: + # could not be parsed, so stop here + signode = addnodes.desc_signature(sig, '') + signode['first'] = False + signode.clear() + signode += addnodes.desc_name(sig, sig) + node.replace_self(signode) + continue + + isShorthand = node.isShorthand + parentKey = node.parentKey + rootSymbol = self.env.domains['cpp'].data['root_symbol'] + parentSymbol = rootSymbol.direct_lookup(parentKey) + if not parentSymbol: + print("Target: ", sig) + print("ParentKey: ", parentKey) + print(rootSymbol.dump(1)) + assert parentSymbol # should be there + + symbols = [] # type: List[Symbol] + if isShorthand: + ns = ast # type: ASTNamespace + name = ns.nestedName + if ns.templatePrefix: + templateDecls = ns.templatePrefix.templates + else: + templateDecls = [] + symbols = parentSymbol.find_name(name, templateDecls, 'any', + templateShorthand=True, + matchSelf=True, recurseInAnon=True) + if symbols is None: + symbols = [] + else: + decl = ast # type: ASTDeclaration + name = decl.name + s = parentSymbol.find_declaration(decl, 'any', + templateShorthand=True, + matchSelf=True, recurseInAnon=True) + if s is not None: + symbols.append(s) + + symbols = [s for s in symbols if s.declaration is not None] + + if len(symbols) == 0: + signode = addnodes.desc_signature(sig, '') + signode['first'] = False + node.append(signode) + signode.clear() + signode += addnodes.desc_name(sig, sig) + + logger.warning("Could not find C++ declaration for alias '%s'." % ast, + location=node) + node.replace_self(signode) + else: + nodes = [] + options = dict() + options['tparam-line-spec'] = False + for s in symbols: + signode = addnodes.desc_signature(sig, '') + signode['first'] = False + nodes.append(signode) + s.declaration.describe_signature(signode, 'markName', self.env, options) + node.replace_self(nodes) + + +class CPPAliasObject(ObjectDescription): + option_spec = {} # type: Dict + + def warn(self, msg): + # type: (Union[str, Exception]) -> None + self.state_machine.reporter.warning(msg, line=self.lineno) + + def run(self): + # type: () -> List[nodes.Node] + """ + On purpose this doesn't call the ObjectDescription version, but is based on it. + Each alias signature may expand into multiple real signatures (an overload set). + The code is therefore based on the ObjectDescription version. + """ + if ':' in self.name: + self.domain, self.objtype = self.name.split(':', 1) + else: + self.domain, self.objtype = '', self.name + + node = addnodes.desc() + node.document = self.state.document + node['domain'] = self.domain + # 'desctype' is a backwards compatible attribute + node['objtype'] = node['desctype'] = self.objtype + node['noindex'] = True + + self.names = [] # type: List[str] + signatures = self.get_signatures() + for i, sig in enumerate(signatures): + node.append(AliasNode(sig, self)) + + contentnode = addnodes.desc_content() + node.append(contentnode) + self.before_content() + self.state.nested_parse(self.content, self.content_offset, contentnode) + self.env.temp_data['object'] = None + self.after_content() + return [node] + + class CPPXRefRole(XRefRole): def process_link(self, env, refnode, has_explicit_title, title, target): - # type: (BuildEnvironment, nodes.Node, bool, unicode, unicode) -> Tuple[unicode, unicode] # NOQA + # type: (BuildEnvironment, nodes.Element, bool, str, str) -> Tuple[str, str] refnode.attributes.update(env.ref_context) if not has_explicit_title: @@ -6660,7 +6900,7 @@ class CPPXRefRole(XRefRole): return title, target -class CPPExprRole(object): +class CPPExprRole: def __init__(self, asCode): if asCode: # render the expression as inline code @@ -6672,7 +6912,7 @@ class CPPExprRole(object): self.node_type = nodes.inline def __call__(self, typ, rawtext, text, lineno, inliner, options={}, content=[]): - class Warner(object): + class Warner: def warn(self, msg): inliner.reporter.warning(msg, line=lineno) text = utils.unescape(text).replace('\n', ' ') @@ -6683,8 +6923,7 @@ class CPPExprRole(object): try: ast = parser.parse_expression() except DefinitionError as ex: - Warner().warn('Unparseable C++ expression: %r\n%s' - % (text, text_type(ex.description))) + Warner().warn('Unparseable C++ expression: %r\n%s' % (text, ex)) # see below return [self.node_type(text, text, classes=classes)], [] parentSymbol = env.temp_data.get('cpp:parent_symbol', None) @@ -6698,7 +6937,17 @@ class CPPExprRole(object): class CPPDomain(Domain): - """C++ language domain.""" + """C++ language domain. + + There are two 'object type' attributes being used:: + + - Each object created from directives gets an assigned .objtype from ObjectDescription.run. + This is simply the directive name. + - Each declaration (see the distinction in the directives dict below) has a nested .ast of + type ASTDeclaration. That object has .objectType which corresponds to the keys in the + object_types dict below. They are the core different types of declarations in C++ that + one can document. + """ name = 'cpp' label = 'C++' object_types = { @@ -6713,7 +6962,9 @@ class CPPDomain(Domain): } directives = { + # declarations 'class': CPPClassObject, + 'struct': CPPClassObject, 'union': CPPUnionObject, 'function': CPPFunctionObject, 'member': CPPMemberObject, @@ -6724,13 +6975,17 @@ class CPPDomain(Domain): 'enum-struct': CPPEnumObject, 'enum-class': CPPEnumObject, 'enumerator': CPPEnumeratorObject, + # scope control 'namespace': CPPNamespaceObject, 'namespace-push': CPPNamespacePushObject, - 'namespace-pop': CPPNamespacePopObject + 'namespace-pop': CPPNamespacePopObject, + # other + 'alias': CPPAliasObject } roles = { 'any': CPPXRefRole(), 'class': CPPXRefRole(), + 'struct': CPPXRefRole(), 'union': CPPXRefRole(), 'func': CPPXRefRole(fix_parens=True), 'member': CPPXRefRole(), @@ -6748,7 +7003,7 @@ class CPPDomain(Domain): } def clear_doc(self, docname): - # type: (unicode) -> None + # type: (str) -> None if Symbol.debug_show_tree: print("clear_doc:", docname) print("\tbefore:") @@ -6768,18 +7023,18 @@ class CPPDomain(Domain): del self.data['names'][name] def process_doc(self, env, docname, document): - # type: (BuildEnvironment, unicode, nodes.Node) -> None + # type: (BuildEnvironment, str, nodes.document) -> None if Symbol.debug_show_tree: print("process_doc:", docname) print(self.data['root_symbol'].dump(0)) print("process_doc end:", docname) def process_field_xref(self, pnode): - # type: (nodes.Node) -> None + # type: (addnodes.pending_xref) -> None pnode.attributes.update(self.env.ref_context) def merge_domaindata(self, docnames, otherdata): - # type: (List[unicode], Dict) -> None + # type: (List[str], Dict) -> None if Symbol.debug_show_tree: print("merge_domaindata:") print("\tself:") @@ -6805,9 +7060,8 @@ class CPPDomain(Domain): def _resolve_xref_inner(self, env, fromdocname, builder, typ, target, node, contnode, emitWarnings=True): - # type: (BuildEnvironment, unicode, Builder, unicode, unicode, nodes.Node, nodes.Node, bool) -> nodes.Node # NOQA - - class Warner(object): + # type: (BuildEnvironment, str, Builder, str, str, addnodes.pending_xref, nodes.Element, bool) -> Tuple[nodes.Element, str] # NOQA + class Warner: def warn(self, msg): if emitWarnings: logger.warning(msg, location=node) @@ -6833,8 +7087,7 @@ class CPPDomain(Domain): # strange, that we don't get the error now, use the original return target, e t, ex = findWarning(e) - warner.warn('Unparseable C++ cross-reference: %r\n%s' - % (t, text_type(ex.description))) + warner.warn('Unparseable C++ cross-reference: %r\n%s' % (t, ex)) return None, None parentKey = node.get("cpp:parent_key", None) rootSymbol = self.data['root_symbol'] @@ -6855,9 +7108,11 @@ class CPPDomain(Domain): templateDecls = ns.templatePrefix.templates else: templateDecls = [] - s = parentSymbol.find_name(name, templateDecls, typ, - templateShorthand=True, - matchSelf=True, recurseInAnon=True) + symbols = parentSymbol.find_name(name, templateDecls, typ, + templateShorthand=True, + matchSelf=True, recurseInAnon=True) + # just refer to the arbitrarily first symbol + s = None if symbols is None else symbols[0] else: decl = ast # type: ASTDeclaration name = decl.name @@ -6865,7 +7120,7 @@ class CPPDomain(Domain): templateShorthand=True, matchSelf=True, recurseInAnon=True) if s is None or s.declaration is None: - txtName = text_type(name) + txtName = str(name) if txtName.startswith('std::') or txtName == 'std': raise NoUri() return None, None @@ -6903,7 +7158,7 @@ class CPPDomain(Domain): # the non-identifier refs are cross-references, which should be processed: # - fix parenthesis due to operator() and add_function_parentheses if typ != "identifier": - title = contnode.pop(0).astext() + title = contnode.pop(0).astext() # type: ignore # If it's operator(), we need to add '()' if explicit function parens # are requested. Then the Sphinx machinery will add another pair. # Also, if it's an 'any' ref that resolves to a function, we need to add @@ -6943,34 +7198,32 @@ class CPPDomain(Domain): declaration.get_newest_id(), contnode, displayName ), declaration.objectType - def resolve_xref(self, env, fromdocname, builder, - typ, target, node, contnode): - # type: (BuildEnvironment, unicode, Builder, unicode, unicode, nodes.Node, nodes.Node) -> nodes.Node # NOQA + def resolve_xref(self, env, fromdocname, builder, typ, target, node, contnode): + # type: (BuildEnvironment, str, Builder, str, str, addnodes.pending_xref, nodes.Element) -> nodes.Element # NOQA return self._resolve_xref_inner(env, fromdocname, builder, typ, target, node, contnode)[0] - def resolve_any_xref(self, env, fromdocname, builder, target, - node, contnode): - # type: (BuildEnvironment, unicode, Builder, unicode, nodes.Node, nodes.Node) -> List[Tuple[unicode, nodes.Node]] # NOQA - node, objtype = self._resolve_xref_inner(env, fromdocname, builder, - 'any', target, node, contnode, - emitWarnings=False) - if node: + def resolve_any_xref(self, env, fromdocname, builder, target, node, contnode): + # type: (BuildEnvironment, str, Builder, str, addnodes.pending_xref, nodes.Element) -> List[Tuple[str, nodes.Element]] # NOQA + retnode, objtype = self._resolve_xref_inner(env, fromdocname, builder, + 'any', target, node, contnode, + emitWarnings=False) + if retnode: if objtype == 'templateParam': - return [('cpp:templateParam', node)] + return [('cpp:templateParam', retnode)] else: - return [('cpp:' + self.role_for_objtype(objtype), node)] + return [('cpp:' + self.role_for_objtype(objtype), retnode)] return [] def get_objects(self): - # type: () -> Iterator[Tuple[unicode, unicode, unicode, unicode, unicode, int]] + # type: () -> Iterator[Tuple[str, str, str, str, str, int]] rootSymbol = self.data['root_symbol'] for symbol in rootSymbol.get_all_symbols(): if symbol.declaration is None: continue assert symbol.docname fullNestedName = symbol.get_full_nested_name() - name = text_type(fullNestedName).lstrip(':') + name = str(fullNestedName).lstrip(':') dispname = fullNestedName.get_display_string().lstrip(':') objectType = symbol.declaration.objectType docname = symbol.docname @@ -6978,7 +7231,7 @@ class CPPDomain(Domain): yield (name, dispname, objectType, docname, newestId, 1) def get_full_qualified_name(self, node): - # type: (nodes.Node) -> unicode + # type: (nodes.Element) -> str target = node.get('reftarget', None) if target is None: return None @@ -6989,15 +7242,16 @@ class CPPDomain(Domain): rootSymbol = self.data['root_symbol'] parentSymbol = rootSymbol.direct_lookup(parentKey) parentName = parentSymbol.get_full_nested_name() - return '::'.join([text_type(parentName), target]) + return '::'.join([str(parentName), target]) def setup(app): - # type: (Sphinx) -> Dict[unicode, Any] + # type: (Sphinx) -> Dict[str, Any] app.add_domain(CPPDomain) app.add_config_value("cpp_index_common_prefix", [], 'env') app.add_config_value("cpp_id_attributes", [], 'env') app.add_config_value("cpp_paren_attributes", [], 'env') + app.add_post_transform(AliasTransform) return { 'version': 'builtin', |
