| 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 | #include "mycpp/gc_str.h"  // Str
 | 
| 13 | 
 | 
| 14 | class BigStr;
 | 
| 15 | 
 | 
| 16 | class _ExceptionOpaque {
 | 
| 17 |  public:
 | 
| 18 |   _ExceptionOpaque() {
 | 
| 19 |   }
 | 
| 20 |   static constexpr ObjHeader obj_header() {
 | 
| 21 |     return ObjHeader::ClassFixed(kZeroMask, sizeof(_ExceptionOpaque));
 | 
| 22 |   }
 | 
| 23 | };
 | 
| 24 | 
 | 
| 25 | // mycpp removes constructor arguments
 | 
| 26 | class Exception : public _ExceptionOpaque {};
 | 
| 27 | 
 | 
| 28 | class IndexError : public _ExceptionOpaque {};
 | 
| 29 | 
 | 
| 30 | class KeyError : public _ExceptionOpaque {};
 | 
| 31 | 
 | 
| 32 | class EOFError : public _ExceptionOpaque {};
 | 
| 33 | 
 | 
| 34 | class ZeroDivisionError : public _ExceptionOpaque {};
 | 
| 35 | 
 | 
| 36 | class KeyboardInterrupt : public _ExceptionOpaque {};
 | 
| 37 | 
 | 
| 38 | class StopIteration : public _ExceptionOpaque {};
 | 
| 39 | 
 | 
| 40 | class ValueError {
 | 
| 41 |  public:
 | 
| 42 |   ValueError() : message(nullptr) {
 | 
| 43 |   }
 | 
| 44 |   explicit ValueError(BigStr* message) : message(message) {
 | 
| 45 |   }
 | 
| 46 | 
 | 
| 47 |   static constexpr ObjHeader obj_header() {
 | 
| 48 |     return ObjHeader::ClassFixed(field_mask(), sizeof(ValueError));
 | 
| 49 |   }
 | 
| 50 | 
 | 
| 51 |   BigStr* message;
 | 
| 52 | 
 | 
| 53 |   static constexpr uint32_t field_mask() {
 | 
| 54 |     return maskbit(offsetof(ValueError, message));
 | 
| 55 |   }
 | 
| 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(BigStr* message) : message(message) {
 | 
| 66 |   }
 | 
| 67 | 
 | 
| 68 |   static constexpr ObjHeader obj_header() {
 | 
| 69 |     return ObjHeader::ClassFixed(field_mask(), sizeof(RuntimeError));
 | 
| 70 |   }
 | 
| 71 | 
 | 
| 72 |   BigStr* message;
 | 
| 73 | 
 | 
| 74 |   static constexpr uint32_t field_mask() {
 | 
| 75 |     return maskbit(offsetof(RuntimeError, message));
 | 
| 76 |   }
 | 
| 77 | };
 | 
| 78 | 
 | 
| 79 | // libc::wcswidth raises UnicodeError on invalid UTF-8
 | 
| 80 | class UnicodeError : public RuntimeError {
 | 
| 81 |  public:
 | 
| 82 |   explicit UnicodeError(BigStr* message) : RuntimeError(message) {
 | 
| 83 |   }
 | 
| 84 | };
 | 
| 85 | 
 | 
| 86 | // Python 2 has a dubious distinction between IOError and OSError, so mycpp
 | 
| 87 | // generates this base class to catch both.
 | 
| 88 | class IOError_OSError : public _ExceptionOpaque {
 | 
| 89 |  public:
 | 
| 90 |   explicit IOError_OSError(int err_num) : _ExceptionOpaque(), errno_(err_num) {
 | 
| 91 |   }
 | 
| 92 |   int errno_;
 | 
| 93 | };
 | 
| 94 | 
 | 
| 95 | class IOError : public IOError_OSError {
 | 
| 96 |  public:
 | 
| 97 |   explicit IOError(int err_num) : IOError_OSError(err_num) {
 | 
| 98 |   }
 | 
| 99 | };
 | 
| 100 | 
 | 
| 101 | class OSError : public IOError_OSError {
 | 
| 102 |  public:
 | 
| 103 |   explicit OSError(int err_num) : IOError_OSError(err_num) {
 | 
| 104 |   }
 | 
| 105 | };
 | 
| 106 | 
 | 
| 107 | class SystemExit : public _ExceptionOpaque {
 | 
| 108 |  public:
 | 
| 109 |   explicit SystemExit(int code) : _ExceptionOpaque(), code(code) {
 | 
| 110 |   }
 | 
| 111 |   int code;
 | 
| 112 | };
 | 
| 113 | 
 | 
| 114 | void print(BigStr* s);
 | 
| 115 | 
 | 
| 116 | inline void print(Str s) {
 | 
| 117 |   print(s.big_);
 | 
| 118 | }
 | 
| 119 | 
 | 
| 120 | BigStr* repr(BigStr* s);
 | 
| 121 | 
 | 
| 122 | BigStr* str(int i);
 | 
| 123 | 
 | 
| 124 | BigStr* str(double d);
 | 
| 125 | 
 | 
| 126 | BigStr* intern(BigStr* s);
 | 
| 127 | 
 | 
| 128 | // Used by mark_sweep_heap and StrFormat
 | 
| 129 | bool StringToInt(const char* s, int len, int base, int* result);
 | 
| 130 | 
 | 
| 131 | // Used by Oils integers
 | 
| 132 | bool StringToInt64(const char* s, int len, int base, int64_t* result);
 | 
| 133 | 
 | 
| 134 | // String to integer, raising ValueError if invalid
 | 
| 135 | int to_int(BigStr* s, int base = 10);
 | 
| 136 | 
 | 
| 137 | BigStr* chr(int i);
 | 
| 138 | int ord(BigStr* s);
 | 
| 139 | 
 | 
| 140 | inline int to_int(bool b) {
 | 
| 141 |   return b;
 | 
| 142 | }
 | 
| 143 | 
 | 
| 144 | bool to_bool(BigStr* s);
 | 
| 145 | 
 | 
| 146 | // Used by division operator
 | 
| 147 | double to_float(int i);
 | 
| 148 | 
 | 
| 149 | // Used for floating point flags like read -t 0.1
 | 
| 150 | double to_float(BigStr* s);
 | 
| 151 | 
 | 
| 152 | inline bool to_bool(int i) {
 | 
| 153 |   return i != 0;
 | 
| 154 | }
 | 
| 155 | 
 | 
| 156 | bool str_contains(BigStr* haystack, BigStr* needle);
 | 
| 157 | 
 | 
| 158 | // Used by 'with str_switch(s)'
 | 
| 159 | bool str_equals_c(BigStr* s, const char* c_string, int c_len);
 | 
| 160 | 
 | 
| 161 | // Only used by unit tests
 | 
| 162 | bool str_equals0(const char* c_string, BigStr* s);
 | 
| 163 | 
 | 
| 164 | BigStr* str_concat(BigStr* a, BigStr* b);  // a + b when a and b are strings
 | 
| 165 | BigStr* str_concat3(BigStr* a, BigStr* b, BigStr* c);  // for os_path::join()
 | 
| 166 | BigStr* str_repeat(BigStr* s, int times);              // e.g. ' ' * 3
 | 
| 167 | 
 | 
| 168 | extern BigStr* kEmptyString;
 | 
| 169 | 
 | 
| 170 | int hash(BigStr* s);
 | 
| 171 | 
 | 
| 172 | int max(int a, int b);
 | 
| 173 | int min(int a, int b);
 | 
| 174 | 
 | 
| 175 | #endif  // GC_BUILTINS_H
 |