cpp

Coverage Report

Created: 2022-11-10 11:34

/home/andy/git/oilshell/oil/mycpp/builtins.h
Line
Count
Source (jump to first uncovered line)
1
// 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
class Str;
11
12
class _ExceptionOpaque : public Obj {
13
 public:
14
4
  _ExceptionOpaque() : Obj(Tag::Opaque, kZeroMask, kNoObjLen) {
15
4
  }
16
};
17
18
// mycpp removes constructor arguments
19
class AssertionError : public _ExceptionOpaque {};
20
21
class IndexError : public _ExceptionOpaque {};
22
23
class ValueError : public _ExceptionOpaque {};
24
25
class KeyError : public _ExceptionOpaque {};
26
27
class EOFError : public _ExceptionOpaque {};
28
29
class KeyboardInterrupt : public _ExceptionOpaque {};
30
31
// TODO: we could eliminate args to NotImplementedError, like we do for
32
// AssertionError
33
class NotImplementedError : public _ExceptionOpaque {
34
 public:
35
0
  NotImplementedError() : _ExceptionOpaque() {
36
0
  }
37
  // used in expr_to_ast
38
0
  explicit NotImplementedError(int i) : _ExceptionOpaque() {
39
0
  }
40
  // called with Id_str()
41
0
  explicit NotImplementedError(const char* s) : _ExceptionOpaque() {
42
0
  }
43
0
  explicit NotImplementedError(Str* s) : _ExceptionOpaque() {
44
0
  }
45
};
46
47
// libc::regex_match and other bindings raise RuntimeError
48
class RuntimeError : public Obj {
49
 public:
50
  explicit RuntimeError(Str* message);
51
  Str* message;
52
};
53
54
1
constexpr uint16_t maskof_RuntimeError() {
55
1
  return maskbit(offsetof(RuntimeError, message));
56
1
}
57
58
inline RuntimeError::RuntimeError(Str* message)
59
1
    : Obj(Tag::FixedSize, maskof_RuntimeError(), kNoObjLen), message(message) {
60
1
}
61
62
// Python 2 has a dubious distinction between IOError and OSError, so mycpp
63
// generates this base class to catch both.
64
class IOError_OSError : public _ExceptionOpaque {
65
 public:
66
1
  explicit IOError_OSError(int err_num) : _ExceptionOpaque(), errno_(err_num) {
67
1
  }
68
  int errno_;
69
};
70
71
class IOError : public IOError_OSError {
72
 public:
73
0
  explicit IOError(int err_num) : IOError_OSError(err_num) {
74
0
  }
75
};
76
77
class OSError : public IOError_OSError {
78
 public:
79
1
  explicit OSError(int err_num) : IOError_OSError(err_num) {
80
1
  }
81
};
82
83
void print(Str* s);
84
85
void println_stderr(Str* s);
86
87
Str* repr(Str* s);
88
89
// For hnode::External in asdl/format.py.  TODO: Remove this when that is
90
// removed.
91
0
inline Str* repr(void* obj) {
92
0
  InvalidCodePath();
93
0
}
Unexecuted instantiation: _Z4reprPv
Unexecuted instantiation: _Z4reprPv
94
95
0
inline Str* str(double f) {
96
0
  NotImplemented();
97
0
}
Unexecuted instantiation: _Z3strd
Unexecuted instantiation: _Z3strd
98
99
Str* str(int i);
100
101
// Helper function: returns whether the string is a valid integer, and
102
// populates the result.  (Also used by marksweep_heap.cc; could be moved
103
// there)
104
bool StringToInteger(char* s, int len, int base, int* result);
105
106
// String to integer, raising ValueError if invalid
107
int to_int(Str* s);
108
int to_int(Str* s, int base);
109
110
Str* chr(int i);
111
int ord(Str* s);
112
113
0
inline int to_int(bool b) {
114
0
  return b;
115
0
}
116
117
bool to_bool(Str* s);
118
double to_float(Str* s);
119
120
1
inline bool to_bool(int i) {
121
1
  return i != 0;
122
1
}
123
124
bool str_contains(Str* haystack, Str* needle);
125
126
extern Str* kEmptyString;
127
128
// Function that mycpp generates for non-constant format strings
129
// TODO: switch back to a printf interpreter
130
0
inline Str* dynamic_fmt_dummy() {
131
0
  return kEmptyString;
132
0
}
133
134
#endif  // GC_BUILTINS_H