Debian Codegraph @

include/ruby/internal/core/rregexp.h

raw
1 #ifndef RBIMPL_RREGEXP_H /*-*-C++-*-vi:se ft=cpp:*/
2 #define RBIMPL_RREGEXP_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 Defines struct ::RRegexp.
22 */
23 #include "ruby/internal/attr/artificial.h"
24 #include "ruby/internal/attr/pure.h"
25 #include "ruby/internal/cast.h"
26 #include "ruby/internal/core/rbasic.h"
27 #include "ruby/internal/core/rstring.h"
28 #include "ruby/internal/value.h"
29 #include "ruby/internal/value_type.h"
30
31 /**
32 * Convenient casting macro.
33 *
34 * @param obj An object, which is in fact an ::RRegexp.
35 * @return The passed object casted to ::RRegexp.
36 */
37 #define RREGEXP(obj) RBIMPL_CAST((struct RRegexp *)(obj))
38
39 /**
40 * Convenient accessor macro.
41 *
42 * @param obj An object, which is in fact an ::RRegexp.
43 * @return The passed object's pattern buffer.
44 */
45 #define RREGEXP_PTR(obj) (RREGEXP(obj)->ptr)
46 /** @cond INTERNAL_MACRO */
47 #define RREGEXP_SRC RREGEXP_SRC
48 #define RREGEXP_SRC_PTR RREGEXP_SRC_PTR
49 #define RREGEXP_SRC_LEN RREGEXP_SRC_LEN
50 #define RREGEXP_SRC_END RREGEXP_SRC_END
51 /** @endcond */
52
53 struct re_patter_buffer; /* a.k.a. OnigRegexType, defined in onigmo.h */
54
55 /**
56 * Ruby's regular expression. A regexp is compiled into its own intermediate
57 * representation. This one holds that info. Regexp "match" operation then
58 * executes that IR.
59 */
60 struct RRegexp {
61
62 /** Basic part, including flags and class. */
63 struct RBasic basic;
64
65 /**
66 * The pattern buffer. This is a quasi-opaque struct that holds compiled
67 * intermediate representation of the regular expression.
68 *
69 * @note Compilation of a regexp could be delayed until actual match.
70 */
71 struct re_pattern_buffer *ptr;
72
73 /** Source code of this expression. */
74 const VALUE src;
75
76 /**
77 * Reference count. A regexp match can take extraordinarily long time to
78 * run. Ruby's regular expression is heavily extended and not a regular
79 * language any longer; runs in NP-time in practice. Now, Ruby also has
80 * threads and GVL. In order to prevent long GVL lockup, our regexp engine
81 * can release it on occasions. This means that multiple threads can touch
82 * a regular expressions at once. That itself is okay. But their cleanup
83 * phase shall wait for all the concurrent runs, to prevent use-after-free
84 * situation. This field is used to count such threads that are executing
85 * this particular pattern buffer.
86 *
87 * @warning Of course, touching this field from extension libraries causes
88 * catastrophic effects. Just leave it.
89 */
90 unsigned long usecnt;
91 };
92
93 RBIMPL_ATTR_PURE_UNLESS_DEBUG()
94 RBIMPL_ATTR_ARTIFICIAL()
95 /**
96 * Convenient getter function.
97 *
98 * @param[in] rexp The regular expression in question.
99 * @return The source code of the regular expression.
100 * @pre `rexp` must be of ::RRegexp.
101 */
102 static inline VALUE
103 RREGEXP_SRC(VALUE rexp)
104 {
105 RBIMPL_ASSERT_TYPE(rexp, RUBY_T_REGEXP);
106 VALUE ret = RREGEXP(rexp)->src;
107 RBIMPL_ASSERT_TYPE(ret, RUBY_T_STRING);
108 return ret;
109 }
110
111 RBIMPL_ATTR_PURE_UNLESS_DEBUG()
112 RBIMPL_ATTR_ARTIFICIAL()
113 /**
114 * Convenient getter function.
115 *
116 * @param[in] rexp The regular expression in question.
117 * @return The source code of the regular expression, in C's string.
118 * @pre `rexp` must be of ::RRegexp.
119 *
120 * @internal
121 *
122 * It seems nobody uses this function in the wild. Subject to hide?
123 */
124 static inline char *
125 RREGEXP_SRC_PTR(VALUE rexp)
126 {
127 return RSTRING_PTR(RREGEXP_SRC(rexp));
128 }
129
130 RBIMPL_ATTR_PURE_UNLESS_DEBUG()
131 RBIMPL_ATTR_ARTIFICIAL()
132 /**
133 * Convenient getter function.
134 *
135 * @param[in] rexp The regular expression in question.
136 * @return The length of the source code of the regular expression.
137 * @pre `rexp` must be of ::RRegexp.
138 *
139 * @internal
140 *
141 * It seems nobody uses this function in the wild. Subject to hide?
142 */
143 static inline long
144 RREGEXP_SRC_LEN(VALUE rexp)
145 {
146 return RSTRING_LEN(RREGEXP_SRC(rexp));
147 }
148
149 RBIMPL_ATTR_PURE_UNLESS_DEBUG()
150 RBIMPL_ATTR_ARTIFICIAL()
151 /**
152 * Convenient getter function.
153 *
154 * @param[in] rexp The regular expression in question.
155 * @return The end of the source code of the regular expression.
156 * @pre `rexp` must be of ::RRegexp.
157 *
158 * @internal
159 *
160 * It seems nobody uses this function in the wild. Subject to hide?
161 */
162 static inline char *
163 RREGEXP_SRC_END(VALUE rexp)
164 {
165 return RSTRING_END(RREGEXP_SRC(rexp));
166 }
167
168 #endif /* RBIMPL_RREGEXP_H */