|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# |
| 3 | +# This source code is licensed under the MIT license found in the |
| 4 | +# LICENSE file in the root directory of this source tree. |
| 5 | + |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +from typing import TYPE_CHECKING |
| 9 | + |
| 10 | +from ..utils import ( |
| 11 | + Argument, |
| 12 | + format_arguments, |
| 13 | + parse_arg_string, |
| 14 | + qualify_arguments, |
| 15 | + qualify_template_args_only, |
| 16 | + qualify_type_str, |
| 17 | +) |
| 18 | +from .base import Member, MemberKind |
| 19 | + |
| 20 | +if TYPE_CHECKING: |
| 21 | + from ..scope import Scope |
| 22 | + |
| 23 | + |
| 24 | +class FunctionMember(Member): |
| 25 | + def __init__( |
| 26 | + self, |
| 27 | + name: str, |
| 28 | + type: str, |
| 29 | + visibility: str, |
| 30 | + arg_string: str, |
| 31 | + is_virtual: bool, |
| 32 | + is_pure_virtual: bool, |
| 33 | + is_static: bool, |
| 34 | + doxygen_params: list[Argument] | None = None, |
| 35 | + is_constexpr: bool = False, |
| 36 | + ) -> None: |
| 37 | + super().__init__(name, visibility) |
| 38 | + self.type: str = type |
| 39 | + self.is_virtual: bool = is_virtual |
| 40 | + self.is_static: bool = is_static |
| 41 | + self.is_constexpr: bool = is_constexpr |
| 42 | + parsed_arguments, self.modifiers = parse_arg_string(arg_string) |
| 43 | + self.arguments = ( |
| 44 | + doxygen_params if doxygen_params is not None else parsed_arguments |
| 45 | + ) |
| 46 | + |
| 47 | + # Doxygen signals pure-virtual via the virt attribute, but the arg string |
| 48 | + # may not contain "= 0" (e.g. trailing return type syntax), so the |
| 49 | + # modifiers parsed from the arg string may miss it. Propagate the flag. |
| 50 | + if is_pure_virtual: |
| 51 | + self.modifiers.is_pure_virtual = True |
| 52 | + |
| 53 | + self.is_const = self.modifiers.is_const |
| 54 | + self.is_override = self.modifiers.is_override |
| 55 | + |
| 56 | + @property |
| 57 | + def member_kind(self) -> MemberKind: |
| 58 | + if self.name.startswith("operator"): |
| 59 | + return MemberKind.OPERATOR |
| 60 | + return MemberKind.FUNCTION |
| 61 | + |
| 62 | + def close(self, scope: Scope): |
| 63 | + self.type = qualify_type_str(self.type, scope) |
| 64 | + self.arguments = qualify_arguments(self.arguments, scope) |
| 65 | + # Qualify template arguments in function name for explicit specializations |
| 66 | + # e.g., "convert<MyType>" -> "convert<ns::MyType>" |
| 67 | + if "<" in self.name: |
| 68 | + self.name = qualify_template_args_only(self.name, scope) |
| 69 | + |
| 70 | + def to_string( |
| 71 | + self, |
| 72 | + indent: int = 0, |
| 73 | + qualification: str | None = None, |
| 74 | + hide_visibility: bool = False, |
| 75 | + ) -> str: |
| 76 | + name = self._get_qualified_name(qualification) |
| 77 | + result = "" |
| 78 | + |
| 79 | + if self.template_list is not None: |
| 80 | + result += " " * indent + self.template_list.to_string() + "\n" |
| 81 | + |
| 82 | + result += " " * indent |
| 83 | + |
| 84 | + if not hide_visibility: |
| 85 | + result += self.visibility + " " |
| 86 | + |
| 87 | + if self.is_virtual: |
| 88 | + result += "virtual " |
| 89 | + |
| 90 | + if self.is_static: |
| 91 | + result += "static " |
| 92 | + |
| 93 | + if self.is_constexpr: |
| 94 | + result += "constexpr " |
| 95 | + |
| 96 | + if self.type: |
| 97 | + result += f"{self.type} " |
| 98 | + |
| 99 | + result += f"{name}({format_arguments(self.arguments)})" |
| 100 | + |
| 101 | + if self.modifiers.is_const: |
| 102 | + result += " const" |
| 103 | + |
| 104 | + if self.modifiers.is_noexcept: |
| 105 | + if self.modifiers.noexcept_expr: |
| 106 | + result += f" noexcept({self.modifiers.noexcept_expr})" |
| 107 | + else: |
| 108 | + result += " noexcept" |
| 109 | + |
| 110 | + if self.modifiers.is_override: |
| 111 | + result += " override" |
| 112 | + |
| 113 | + if self.modifiers.is_final: |
| 114 | + result += " final" |
| 115 | + |
| 116 | + if self.modifiers.is_pure_virtual: |
| 117 | + result += " = 0" |
| 118 | + elif self.modifiers.is_default: |
| 119 | + result += " = default" |
| 120 | + elif self.modifiers.is_delete: |
| 121 | + result += " = delete" |
| 122 | + |
| 123 | + result += ";" |
| 124 | + return result |
0 commit comments