Implement tp_richcompare for gdb.Block
[binutils-gdb.git] / gdb / gdbarch_types.py
blobb98e60b3111bf27fc6a980f553202b6131ab7c15
1 # Architecture commands for GDB, the GNU debugger.
3 # Copyright (C) 1998-2024 Free Software Foundation, Inc.
5 # This file is part of GDB.
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 from typing import List, Optional, Tuple, Union
23 def join_type_and_name(t: str, n: str):
24 "Combine the type T and the name N into a C declaration."
25 if t.endswith("*") or t.endswith("&"):
26 return t + n
27 else:
28 return t + " " + n
31 def join_params(params: List[Tuple[str, str]]):
32 """Given a sequence of (TYPE, NAME) pairs, generate a comma-separated
33 list of declarations."""
34 return ", ".join([join_type_and_name(p[0], p[1]) for p in params])
37 class Component:
38 "Base class for all components."
40 def __init__(
41 self,
42 name: str,
43 type: str,
44 printer: Optional[str] = None,
45 comment: Optional[str] = None,
46 predicate: bool = False,
47 predefault: Optional[str] = None,
48 postdefault: Optional[str] = None,
49 invalid: Union[bool, str] = True,
50 params: Optional[List[Tuple[str, str]]] = None,
51 param_checks: Optional[List[str]] = None,
52 result_checks: Optional[List[str]] = None,
53 implement: bool = True,
55 self.name = name
56 self.type = type
57 self.printer = printer
58 self.comment = comment
59 self.predicate = predicate
60 self.predefault = predefault
61 self.postdefault = postdefault
62 self.invalid = invalid
63 self.params = params or []
64 self.param_checks = param_checks
65 self.result_checks = result_checks
66 self.implement = implement
68 components.append(self)
70 # It doesn't make sense to have a check of the result value
71 # for a function or method with void return type.
72 if self.type == "void" and self.result_checks:
73 raise Exception("can't have result checks with a void return type")
75 def get_predicate(self):
76 "Return the expression used for validity checking."
77 if self.predefault:
78 predicate = f"gdbarch->{self.name} != {self.predefault}"
79 else:
80 predicate = f"gdbarch->{self.name} != NULL"
81 return predicate
84 class Info(Component):
85 "An Info component is copied from the gdbarch_info."
88 class Value(Component):
89 "A Value component is just a data member."
91 def __init__(
92 self,
94 name: str,
95 type: str,
96 comment: Optional[str] = None,
97 predicate: bool = False,
98 predefault: Optional[str] = None,
99 postdefault: Optional[str] = None,
100 invalid: Union[bool, str] = True,
101 printer: Optional[str] = None,
103 super().__init__(
104 comment=comment,
105 name=name,
106 type=type,
107 predicate=predicate,
108 predefault=predefault,
109 postdefault=postdefault,
110 invalid=invalid,
111 printer=printer,
115 class Function(Component):
116 "A Function component is a function pointer member."
118 def __init__(
119 self,
121 name: str,
122 type: str,
123 params: List[Tuple[str, str]],
124 comment: Optional[str] = None,
125 predicate: bool = False,
126 predefault: Optional[str] = None,
127 postdefault: Optional[str] = None,
128 invalid: Union[bool, str] = True,
129 printer: Optional[str] = None,
130 param_checks: Optional[List[str]] = None,
131 result_checks: Optional[List[str]] = None,
132 implement: bool = True,
134 super().__init__(
135 comment=comment,
136 name=name,
137 type=type,
138 predicate=predicate,
139 predefault=predefault,
140 postdefault=postdefault,
141 invalid=invalid,
142 printer=printer,
143 params=params,
144 param_checks=param_checks,
145 result_checks=result_checks,
146 implement=implement,
149 def ftype(self):
150 "Return the name of the function typedef to use."
151 return f"gdbarch_{self.name}_ftype"
153 def param_list(self):
154 "Return the formal parameter list as a string."
155 return join_params(self.params)
157 def set_list(self):
158 """Return the formal parameter list of the caller function,
159 as a string. This list includes the gdbarch."""
160 arch_arg = ("struct gdbarch *", "gdbarch")
161 arch_tuple = [arch_arg]
162 return join_params(arch_tuple + list(self.params))
164 def actuals(self):
165 "Return the actual parameters to forward, as a string."
166 return ", ".join([p[1] for p in self.params])
169 class Method(Function):
170 "A Method is like a Function but passes the gdbarch through."
172 def param_list(self):
173 "See superclass."
174 return self.set_list()
176 def actuals(self):
177 "See superclass."
178 result = ["gdbarch"] + [p[1] for p in self.params]
179 return ", ".join(result)
182 # All the components created in gdbarch-components.py.
183 components: List[Component] = []