/home/andy/git/oilshell/oil/mycpp/gc_builtins.h
Line | Count | Source (jump to first uncovered line) |
1 | | // gc_builtins.h: Statically typed Python builtins. |
2 | | // |
3 | | // Builtin types: tuples, NotImplementedError, AssertionError |
4 | | // Builtin functions: print(), repr(), ord() |
5 | | // Builtin operators: str_concat(), str_repeat(), list_repeat() |
6 | | |
7 | | #ifndef GC_BUILTINS_H |
8 | | #define GC_BUILTINS_H |
9 | | |
10 | | #include "mycpp/common.h" |
11 | | #include "mycpp/gc_obj.h" |
12 | | |
13 | | class Str; |
14 | | |
15 | | class _ExceptionOpaque { |
16 | | public: |
17 | 15 | _ExceptionOpaque() : header_(obj_header()) { |
18 | 15 | } |
19 | 15 | static constexpr ObjHeader obj_header() { |
20 | 15 | return ObjHeader::ClassFixed(kZeroMask, sizeof(_ExceptionOpaque)); |
21 | 15 | } |
22 | | |
23 | | GC_OBJ(header_); |
24 | | }; |
25 | | |
26 | | // mycpp removes constructor arguments |
27 | | class Exception : public _ExceptionOpaque {}; |
28 | | |
29 | | class IndexError : public _ExceptionOpaque {}; |
30 | | |
31 | | class KeyError : public _ExceptionOpaque {}; |
32 | | |
33 | | class EOFError : public _ExceptionOpaque {}; |
34 | | |
35 | | class KeyboardInterrupt : public _ExceptionOpaque {}; |
36 | | |
37 | | class StopIteration : public _ExceptionOpaque {}; |
38 | | |
39 | | class ValueError { |
40 | | public: |
41 | 15 | ValueError() : header_(obj_header()), message(nullptr) { |
42 | 15 | } |
43 | 2 | explicit ValueError(Str* message) : header_(obj_header()), message(message) { |
44 | 2 | } |
45 | | |
46 | 17 | static constexpr ObjHeader obj_header() { |
47 | 17 | return ObjHeader::ClassFixed(field_mask(), sizeof(ValueError)); |
48 | 17 | } |
49 | | |
50 | | GC_OBJ(header_); |
51 | | Str* message; |
52 | | |
53 | 17 | static constexpr uint16_t field_mask() { |
54 | 17 | return maskbit(offsetof(ValueError, message)); |
55 | 17 | } |
56 | | }; |
57 | | |
58 | | // Note these translations by mycpp: |
59 | | // - AssertionError -> assert(0); |
60 | | // - NotImplementedError -> FAIL(kNotImplemented); |
61 | | |
62 | | // libc::regex_match and other bindings raise RuntimeError |
63 | | class RuntimeError { |
64 | | public: |
65 | | explicit RuntimeError(Str* message) |
66 | 4 | : header_(obj_header()), message(message) { |
67 | 4 | } |
68 | | |
69 | 4 | static constexpr ObjHeader obj_header() { |
70 | 4 | return ObjHeader::ClassFixed(field_mask(), sizeof(RuntimeError)); |
71 | 4 | } |
72 | | |
73 | | GC_OBJ(header_); |
74 | | Str* message; |
75 | | |
76 | 4 | static constexpr uint16_t field_mask() { |
77 | 4 | return maskbit(offsetof(RuntimeError, message)); |
78 | 4 | } |
79 | | }; |
80 | | |
81 | | // libc::wcswidth raises UnicodeError on invalid UTF-8 |
82 | | class UnicodeError : public RuntimeError { |
83 | | public: |
84 | 2 | explicit UnicodeError(Str* message) : RuntimeError(message) { |
85 | 2 | } |
86 | | }; |
87 | | |
88 | | // Python 2 has a dubious distinction between IOError and OSError, so mycpp |
89 | | // generates this base class to catch both. |
90 | | class IOError_OSError : public _ExceptionOpaque { |
91 | | public: |
92 | 10 | explicit IOError_OSError(int err_num) : _ExceptionOpaque(), errno_(err_num) { |
93 | 10 | } |
94 | | int errno_; |
95 | | }; |
96 | | |
97 | | class IOError : public IOError_OSError { |
98 | | public: |
99 | 3 | explicit IOError(int err_num) : IOError_OSError(err_num) { |
100 | 3 | } |
101 | | }; |
102 | | |
103 | | class OSError : public IOError_OSError { |
104 | | public: |
105 | 6 | explicit OSError(int err_num) : IOError_OSError(err_num) { |
106 | 6 | } |
107 | | }; |
108 | | |
109 | | class SystemExit : public _ExceptionOpaque { |
110 | | public: |
111 | 0 | explicit SystemExit(int code) : _ExceptionOpaque(), code(code) { |
112 | 0 | } |
113 | | int code; |
114 | | }; |
115 | | |
116 | | void print(Str* s); |
117 | | |
118 | | Str* repr(Str* s); |
119 | | |
120 | | Str* str(int i); |
121 | | |
122 | | // Helper function: returns whether the string is a valid integer, and |
123 | | // populates the result. (Also used by marksweep_heap.cc; could be moved |
124 | | // there) |
125 | | bool StringToInteger(const char* s, int len, int base, int* result); |
126 | | |
127 | | // String to integer, raising ValueError if invalid |
128 | | int to_int(Str* s); |
129 | | int to_int(Str* s, int base); |
130 | | |
131 | | Str* chr(int i); |
132 | | int ord(Str* s); |
133 | | |
134 | 4 | inline int to_int(bool b) { |
135 | 4 | return b; |
136 | 4 | } |
137 | | |
138 | | bool to_bool(Str* s); |
139 | | |
140 | | // Used for floating point flags like read -t 0.1 |
141 | | double to_float(Str* s); |
142 | | |
143 | 9 | inline bool to_bool(int i) { |
144 | 9 | return i != 0; |
145 | 9 | } |
146 | | |
147 | | bool str_contains(Str* haystack, Str* needle); |
148 | | |
149 | | // Only used by unit tests |
150 | | bool str_equals0(const char* c_string, Str* s); |
151 | | |
152 | | Str* str_concat(Str* a, Str* b); // a + b when a and b are strings |
153 | | Str* str_concat3(Str* a, Str* b, Str* c); // for os_path::join() |
154 | | Str* str_repeat(Str* s, int times); // e.g. ' ' * 3 |
155 | | |
156 | | extern Str* kEmptyString; |
157 | | |
158 | | int hash(Str* s); |
159 | | |
160 | | int max(int a, int b); |
161 | | |
162 | | Str* raw_input(Str* prompt); |
163 | | |
164 | | #endif // GC_BUILTINS_H |