cpp

Coverage Report

Created: 2023-09-13 01:07

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