Debian Codegraph @

include/ruby/internal/intern/error.h

raw
1 #ifndef RBIMPL_INTERN_ERROR_H /*-*-C++-*-vi:se ft=cpp:*/
2 #define RBIMPL_INTERN_ERROR_H
3 /**
4 * @file
5 * @author Ruby developers <[email protected]>
6 * @copyright This file is a part of the programming language Ruby.
7 * Permission is hereby granted, to either redistribute and/or
8 * modify this file, provided that the conditions mentioned in the
9 * file COPYING are met. Consult the file for details.
10 * @warning Symbols prefixed with either `RBIMPL` or `rbimpl` are
11 * implementation details. Don't take them as canon. They could
12 * rapidly appear then vanish. The name (path) of this header file
13 * is also an implementation detail. Do not expect it to persist
14 * at the place it is now. Developers are free to move it anywhere
15 * anytime at will.
16 * @note To ruby-core: remember that this header can be possibly
17 * recursively included from extension libraries written in C++.
18 * Do not expect for instance `__VA_ARGS__` is always available.
19 * We assume C99 for ruby itself but we don't assume languages of
20 * extension libraries. They could be written in C++98.
21 * @brief Public APIs related to ::rb_eException.
22 */
23 #include "ruby/internal/attr/format.h"
24 #include "ruby/internal/attr/noreturn.h"
25 #include "ruby/internal/dllexport.h"
26 #include "ruby/internal/value.h"
27 #include "ruby/internal/fl_type.h"
28 #include "ruby/backward/2/assume.h"
29
30 /**
31 * This macro is used in conjunction with rb_check_arity(). If you pass it to
32 * the function's last (max) argument, that means the function does not check
33 * upper limit.
34 */
35 #define UNLIMITED_ARGUMENTS (-1)
36
37 #define rb_exc_new2 rb_exc_new_cstr /**< @old{rb_exc_new_cstr} */
38 #define rb_exc_new3 rb_exc_new_str /**< @old{rb_exc_new_str} */
39
40 /** @cond INTERNAL_MACRO */
41 #define rb_check_arity rb_check_arity
42 /** @endcond */
43
44 RBIMPL_SYMBOL_EXPORT_BEGIN()
45
46 /* error.c */
47
48 /**
49 * Creates an instance of the passed exception class.
50 *
51 * @param[in] etype A subclass of ::rb_eException.
52 * @param[in] ptr Buffer contains error message.
53 * @param[in] len Length of `ptr`, in bytes, not including the
54 * terminating NUL character.
55 * @exception rb_eTypeError `etype` is not a class.
56 * @exception rb_eArgError `len` is negative.
57 * @return An instance of `etype`.
58 * @pre At least `len` bytes of continuous memory region shall be
59 * accessible via `ptr`.
60 *
61 * @internal
62 *
63 * This function works for non-exception classes as well, as long as they take
64 * one string argument.
65 */
66 VALUE rb_exc_new(VALUE etype, const char *ptr, long len);
67
68 RBIMPL_ATTR_NONNULL(())
69 /**
70 * Identical to rb_exc_new(), except it assumes the passed pointer is a pointer
71 * to a C string.
72 *
73 * @param[in] etype A subclass of ::rb_eException.
74 * @param[in] str A C string (becomes an error message).
75 * @exception rb_eTypeError `etype` is not a class.
76 * @return An instance of `etype`.
77 */
78 VALUE rb_exc_new_cstr(VALUE etype, const char *str);
79
80 /**
81 * Identical to rb_exc_new_cstr(), except it takes a Ruby's string instead of
82 * C's.
83 *
84 * @param[in] etype A subclass of ::rb_eException.
85 * @param[in] str An instance of ::rb_cString.
86 * @exception rb_eTypeError `etype` is not a class.
87 * @return An instance of `etype`.
88 */
89 VALUE rb_exc_new_str(VALUE etype, VALUE str);
90
91 RBIMPL_ATTR_NORETURN()
92 RBIMPL_ATTR_NONNULL((1))
93 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 1, 2)
94 /**
95 * Raises an instance of ::rb_eLoadError.
96 *
97 * @param[in] fmt Format specifier string compatible with rb_sprintf().
98 * @exception rb_eLoadError Always raises this.
99 * @note It never returns.
100 *
101 * @internal
102 *
103 * Who needs this? Except ruby itself?
104 */
105 void rb_loaderror(const char *fmt, ...);
106
107 RBIMPL_ATTR_NORETURN()
108 RBIMPL_ATTR_NONNULL((2))
109 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 2, 3)
110 /**
111 * Identical to rb_loaderror(), except it additionally takes which file is
112 * unable to load. The path can be obtained later using `LoadError#path` of
113 * the raising exception.
114 *
115 * @param[in] path What failed.
116 * @param[in] fmt Format specifier string compatible with rb_sprintf().
117 * @exception rb_eLoadError Always raises this.
118 * @note It never returns.
119 */
120 void rb_loaderror_with_path(VALUE path, const char *fmt, ...);
121
122 RBIMPL_ATTR_NORETURN()
123 RBIMPL_ATTR_NONNULL((2))
124 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 2, 3)
125 /**
126 * Raises an instance of ::rb_eNameError. The name can be obtained later using
127 * `NameError#name` of the raising exception.
128 *
129 * @param[in] name What failed.
130 * @param[in] fmt Format specifier string compatible with rb_sprintf().
131 * @exception rb_eNameError Always raises this.
132 * @note It never returns.
133 */
134 void rb_name_error(ID name, const char *fmt, ...);
135
136 RBIMPL_ATTR_NORETURN()
137 RBIMPL_ATTR_NONNULL((2))
138 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 2, 3)
139 /**
140 * Identical to rb_name_error(), except it takes a ::VALUE instead of ::ID.
141 *
142 * @param[in] name What failed.
143 * @param[in] fmt Format specifier string compatible with rb_sprintf().
144 * @exception rb_eNameError Always raises this.
145 * @note It never returns.
146 */
147 void rb_name_error_str(VALUE name, const char *fmt, ...);
148
149 RBIMPL_ATTR_NORETURN()
150 RBIMPL_ATTR_NONNULL((2))
151 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 2, 3)
152 /**
153 * Raises an instance of ::rb_eFrozenError. The object can be obtained later
154 * using `FrozenError#receiver` of the raising exception.
155 *
156 * @param[in] recv What is frozen.
157 * @param[in] fmt Format specifier string compatible with rb_sprintf().
158 * @exception rb_eFrozenError Always raises this.
159 * @note It never returns.
160 *
161 * @internal
162 *
163 * Note however, that it is often not possible to inspect a frozen object,
164 * because the inspection itself could be forbidden by the frozen-ness.
165 */
166 void rb_frozen_error_raise(VALUE recv, const char *fmt, ...);
167
168 RBIMPL_ATTR_NORETURN()
169 RBIMPL_ATTR_NONNULL(())
170 /**
171 * Honestly I don't understand the name, but it raises an instance of
172 * ::rb_eArgError.
173 *
174 * @param[in] str A message.
175 * @param[in] type Another message.
176 * @exception rb_eArgError Always raises this.
177 * @note It never returns.
178 */
179 void rb_invalid_str(const char *str, const char *type);
180
181 RBIMPL_ATTR_NORETURN()
182 RBIMPL_ATTR_NONNULL(())
183 /**
184 * Identical to rb_frozen_error_raise(), except its raising exception has a
185 * message like "can't modify frozen /what/".
186 *
187 * @param[in] what What was frozen.
188 * @exception rb_eFrozenError Always raises this.
189 * @note It never returns.
190 */
191 void rb_error_frozen(const char *what);
192
193 RBIMPL_ATTR_NORETURN()
194 /**
195 * Identical to rb_error_frozen(), except it takes arbitrary Ruby object
196 * instead of C's string.
197 *
198 * @param[in] what What was frozen.
199 * @exception rb_eFrozenError Always raises this.
200 * @note It never returns.
201 */
202 void rb_error_frozen_object(VALUE what);
203
204 /**
205 * Queries if the passed object is frozen.
206 *
207 * @param[in] obj Target object to test frozen-ness.
208 * @exception rb_eFrozenError It is frozen.
209 * @post Upon successful return it is guaranteed _not_ frozen.
210 */
211 void rb_check_frozen(VALUE obj);
212
213 /**
214 * Ensures that the passed object can be `initialize_copy` relationship. When
215 * you implement your own one you would better call this at the right beginning
216 * of your implementation.
217 *
218 * @param[in] obj Destination object.
219 * @param[in] orig Source object.
220 * @exception rb_eFrozenError `obj` is frozen.
221 * @post Upon successful return obj is guaranteed safe to copy orig.
222 */
223 void rb_check_copyable(VALUE obj, VALUE orig);
224
225 RBIMPL_ATTR_NORETURN()
226 /**
227 * @private
228 *
229 * This is an implementation detail of rb_scan_args(). You don't have to
230 * bother.
231 *
232 * @pre `argc` is out of range of `min`..`max`, both inclusive.
233 * @param[in] argc Arbitrary integer.
234 * @param[in] min Minimum allowed `argc`.
235 * @param[in] max Maximum allowed `argc`.
236 * @exception rb_eArgError Always.
237 */
238 void rb_error_arity(int argc, int min, int max);
239
240 RBIMPL_SYMBOL_EXPORT_END()
241
242 /**
243 * @deprecated
244 *
245 * Does anyone use this? Remain not deleted for compatibility.
246 */
247 #define rb_check_frozen_internal(obj) do { \
248 VALUE frozen_obj = (obj); \
249 if (RB_UNLIKELY(RB_OBJ_FROZEN(frozen_obj))) { \
250 rb_error_frozen_object(frozen_obj); \
251 } \
252 } while (0)
253
254 /** @alias{rb_check_frozen} */
255 static inline void
256 rb_check_frozen_inline(VALUE obj)
257 {
258 if (RB_UNLIKELY(RB_OBJ_FROZEN(obj))) {
259 rb_error_frozen_object(obj);
260 }
261 }
262
263 /** @alias{rb_check_frozen} */
264 #define rb_check_frozen rb_check_frozen_inline
265
266 /**
267 * Ensures that the passed integer is in the passed range. When you can use
268 * rb_scan_args() that is preferred over this one (powerful, descriptive). But
269 * it can have its own application area.
270 *
271 * @param[in] argc Arbitrary integer.
272 * @param[in] min Minimum allowed `argv`.
273 * @param[in] max Maximum allowed `argv`, or `UNLIMITED_ARGUMENTS`.
274 * @exception rb_eArgError `argc` out of range.
275 * @return The passed `argc`.
276 * @post Upon successful return `argc` is in range of `min`..`max`, both
277 * inclusive.
278 */
279 static inline int
280 rb_check_arity(int argc, int min, int max)
281 {
282 if ((argc < min) || (max != UNLIMITED_ARGUMENTS && argc > max))
283 rb_error_arity(argc, min, max);
284 return argc;
285 }
286
287 #endif /* RBIMPL_INTERN_ERROR_H */