| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #ifndef LYTHON_AST_VISITOR_HEADER | ||
| 2 | #define LYTHON_AST_VISITOR_HEADER | ||
| 3 | |||
| 4 | #include "ast/nodes.h" | ||
| 5 | #include "compatibility/compatibility.h" | ||
| 6 | #include "dependencies/coz_wrap.h" | ||
| 7 | #include "logging/logging.h" | ||
| 8 | |||
| 9 | #ifndef LY_MAX_VISITOR_RECURSION_DEPTH | ||
| 10 | # define LY_MAX_VISITOR_RECURSION_DEPTH 256 | ||
| 11 | #endif | ||
| 12 | |||
| 13 | namespace lython { | ||
| 14 | |||
| 15 | NEW_EXCEPTION(NullPointerError) | ||
| 16 | |||
| 17 | struct DefaultVisitorTrait { | ||
| 18 | using Trace = std::true_type; | ||
| 19 | using LimitRecursionDepth = std::true_type; | ||
| 20 | using StmtRet = StmtNode*; | ||
| 21 | using ExprRet = ExprNode*; | ||
| 22 | using ModRet = ModNode*; | ||
| 23 | using PatRet = Pattern*; | ||
| 24 | |||
| 25 | enum | ||
| 26 | { MaxRecursionDepth = LY_MAX_VISITOR_RECURSION_DEPTH }; | ||
| 27 | }; | ||
| 28 | |||
| 29 | /*! | ||
| 30 | * Visitor implemented using static polymorphism. | ||
| 31 | * This implementation has 2 advantages: | ||
| 32 | * 1. Enable better inlining & devirtualization than vtables | ||
| 33 | * - More cache friendly | ||
| 34 | * 2. Reduce the number of indirection from 2 to 1 | ||
| 35 | * - 2 because of the double vtable lookup (visitor vtable + node vtable) | ||
| 36 | * | ||
| 37 | * The main drawbacks is it uses macros to generate the vtable at compile time | ||
| 38 | * and the error messages can be a bit arcane | ||
| 39 | */ | ||
| 40 | template <typename Implementation, bool isConst, typename VisitorTrait, typename... Args> | ||
| 41 | struct BaseVisitor { | ||
| 42 | using Trace = typename VisitorTrait::Trace; | ||
| 43 | using StmtRet = typename VisitorTrait::StmtRet; | ||
| 44 | using ExprRet = typename VisitorTrait::ExprRet; | ||
| 45 | using ModRet = typename VisitorTrait::ModRet; | ||
| 46 | using PatRet = typename VisitorTrait::PatRet; | ||
| 47 | |||
| 48 | #define SELECT_TYPE(T) typename std::conditional<isConst, T const, T>::type; | ||
| 49 | |||
| 50 | using Node_t = SELECT_TYPE(Node); | ||
| 51 | using ModNode_t = SELECT_TYPE(ModNode); | ||
| 52 | using Pattern_t = SELECT_TYPE(Pattern); | ||
| 53 | using ExprNode_t = SELECT_TYPE(ExprNode); | ||
| 54 | using StmtNode_t = SELECT_TYPE(StmtNode); | ||
| 55 | |||
| 56 | #undef SELECT_TYPE | ||
| 57 | |||
| 58 | #define TYPE_GEN(rtype) \ | ||
| 59 | using rtype##_t = typename std::conditional<isConst, rtype const, rtype>::type; | ||
| 60 | |||
| 61 | #define X(name, _) | ||
| 62 | #define SSECTION(name) | ||
| 63 | #define EXPR(name, fun) TYPE_GEN(name) | ||
| 64 | #define STMT(name, fun) TYPE_GEN(name) | ||
| 65 | #define MOD(name, fun) TYPE_GEN(name) | ||
| 66 | #define MATCH(name, fun) TYPE_GEN(name) | ||
| 67 | |||
| 68 | NODEKIND_ENUM(X, SSECTION, EXPR, STMT, MOD, MATCH) | ||
| 69 | |||
| 70 | #undef X | ||
| 71 | #undef SSECTION | ||
| 72 | #undef EXPR | ||
| 73 | #undef STMT | ||
| 74 | #undef MOD | ||
| 75 | #undef MATCH | ||
| 76 | #undef TYPE_GEN | ||
| 77 | bool log_trace = false; | ||
| 78 | |||
| 79 | template <typename U, typename T> | ||
| 80 | Array<U> exec(Array<T>& body, int depth, Args... args) { | ||
| 81 | 298 | int k = 0; | |
| 82 | 298 | Array<U> types; | |
| 83 |
2/2✓ Branch 4 taken 345 times.
✓ Branch 5 taken 298 times.
|
643 | for (auto& stmt: body) { |
| 84 |
2/2✓ Branch 1 taken 345 times.
✓ Branch 4 taken 345 times.
|
345 | types.push_back(exec(stmt, depth, (args)...)); |
| 85 | 345 | k += 1; | |
| 86 | } | ||
| 87 | 298 | return types; | |
| 88 | ✗ | }; | |
| 89 | |||
| 90 | template <typename U, typename T> | ||
| 91 | Optional<U> exec(Optional<T>& maybe, int depth, Args... args) { | ||
| 92 |
2/2✓ Branch 1 taken 161 times.
✓ Branch 2 taken 26 times.
|
374 | if (maybe.has_value()) { |
| 93 |
2/2✓ Branch 2 taken 161 times.
✓ Branch 5 taken 161 times.
|
644 | return some<U>(exec(maybe.value(), depth, (args)...)); |
| 94 | } | ||
| 95 | 52 | return none<U>(); | |
| 96 | }; | ||
| 97 | |||
| 98 | template <typename T> | ||
| 99 | T exec(Node_t* n, int depth, Args... args) { | ||
| 100 |
4/5✓ Branch 1 taken 225 times.
✓ Branch 2 taken 178 times.
✓ Branch 3 taken 3214 times.
✓ Branch 4 taken 34 times.
✗ Branch 5 not taken.
|
3651 | switch (n->family()) { |
| 101 | 225 | case NodeFamily::Module: return exec(reinterpret_cast<ModNode_t*>(n), depth, (args)...); | |
| 102 | 178 | case NodeFamily::Statement: return exec(reinterpret_cast<StmtNode_t*>(n), depth, (args)...); | |
| 103 | 3214 | case NodeFamily::Expression: | |
| 104 | 3214 | return exec(reinterpret_cast<ExprNode_t*>(n), depth, (args)...); | |
| 105 | 34 | case NodeFamily::Pattern: return exec(reinterpret_cast<Pattern_t*>(n), depth, (args)...); | |
| 106 | } | ||
| 107 | ✗ | return T(); | |
| 108 | } | ||
| 109 | |||
| 110 | ModRet exec(ModNode_t* mod, int depth, Args... args) { | ||
| 111 | // clang-format off | ||
| 112 | // kwtrace(depth, "{}", mod->kind); | ||
| 113 | |||
| 114 | 228 | check_depth(depth); | |
| 115 | |||
| 116 |
1/5✓ Branch 0 taken 227 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
228 | switch (mod->kind) { |
| 117 | |||
| 118 | #define X(name, _) | ||
| 119 | #define PASS(a, b) | ||
| 120 | #define SSECTION(_) | ||
| 121 | #define MOD(name, fun)\ | ||
| 122 | case NodeKind::name: {\ | ||
| 123 | name##_t* m = reinterpret_cast<name##_t*>(mod);\ | ||
| 124 | return fun(m, depth + 1, (args)...);\ | ||
| 125 | } | ||
| 126 | |||
| 127 | 228 | NODEKIND_ENUM(X, SSECTION, PASS, PASS, MOD, PASS) | |
| 128 | |||
| 129 | #undef X | ||
| 130 | #undef PASS | ||
| 131 | #undef SSECTION | ||
| 132 | #undef MOD | ||
| 133 | |||
| 134 | ✗ | default: | |
| 135 | ✗ | return ModRet(); | |
| 136 | |||
| 137 | } | ||
| 138 | // clang-format on | ||
| 139 | return ModRet(); | ||
| 140 | } | ||
| 141 | |||
| 142 | PatRet exec(Pattern_t* pat, int depth, Args... args) { | ||
| 143 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 108 times.
|
108 | if (!pat) { |
| 144 | ✗ | return PatRet(); | |
| 145 | } | ||
| 146 | |||
| 147 | 108 | check_depth(depth); | |
| 148 | |||
| 149 | // kwtrace(depth, "{}", pat->kind); | ||
| 150 | // clang-format off | ||
| 151 |
8/9✓ Branch 0 taken 52 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 4 times.
✓ Branch 6 taken 6 times.
✓ Branch 7 taken 6 times.
✗ Branch 8 not taken.
|
108 | switch (pat->kind) { |
| 152 | |||
| 153 | #define X(name, _) | ||
| 154 | #define PASS(a, b) | ||
| 155 | #define SSECTION(_) | ||
| 156 | #define MATCH(name, fun)\ | ||
| 157 | case NodeKind::name: {\ | ||
| 158 | name##_t* p = reinterpret_cast<name##_t*>(pat);\ | ||
| 159 | return fun(p, depth + 1, (args)...);\ | ||
| 160 | } | ||
| 161 | |||
| 162 | 108 | NODEKIND_ENUM(X, SSECTION, PASS, PASS, PASS, MATCH) | |
| 163 | |||
| 164 | #undef X | ||
| 165 | #undef PASS | ||
| 166 | #undef SSECTION | ||
| 167 | #undef MATCH | ||
| 168 | |||
| 169 | ✗ | default: | |
| 170 | ✗ | return PatRet(); | |
| 171 | } | ||
| 172 | // clang-format on | ||
| 173 | return PatRet(); | ||
| 174 | } | ||
| 175 | |||
| 176 | ExprRet exec(ExprNode_t* expr, int depth, Args... args) { | ||
| 177 |
2/2✓ Branch 0 taken 195 times.
✓ Branch 1 taken 7575 times.
|
7770 | if (!expr) { |
| 178 | 195 | return ExprRet(); | |
| 179 | } | ||
| 180 | |||
| 181 | 7575 | check_depth(depth); | |
| 182 | |||
| 183 | // kwtrace(depth, "{}", expr->kind); | ||
| 184 | // clang-format off | ||
| 185 |
34/36✓ Branch 0 taken 23 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 199 times.
✓ Branch 3 taken 27 times.
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 10 times.
✓ Branch 6 taken 17 times.
✓ Branch 7 taken 15 times.
✓ Branch 8 taken 13 times.
✓ Branch 9 taken 13 times.
✓ Branch 10 taken 13 times.
✓ Branch 11 taken 8 times.
✓ Branch 12 taken 6 times.
✓ Branch 13 taken 15 times.
✓ Branch 14 taken 5 times.
✓ Branch 15 taken 164 times.
✓ Branch 16 taken 230 times.
✓ Branch 17 taken 9 times.
✓ Branch 18 taken 9 times.
✓ Branch 19 taken 935 times.
✓ Branch 20 taken 49 times.
✓ Branch 21 taken 22 times.
✓ Branch 22 taken 6 times.
✓ Branch 23 taken 4195 times.
✓ Branch 24 taken 16 times.
✓ Branch 25 taken 66 times.
✓ Branch 26 taken 16 times.
✓ Branch 27 taken 3 times.
✓ Branch 28 taken 3 times.
✓ Branch 29 taken 4 times.
✓ Branch 30 taken 449 times.
✗ Branch 31 not taken.
✓ Branch 32 taken 3 times.
✓ Branch 33 taken 744 times.
✓ Branch 34 taken 278 times.
✗ Branch 35 not taken.
|
7575 | switch (expr->kind) { |
| 186 | |||
| 187 | #define X(name, _) | ||
| 188 | #define PASS(a, b) | ||
| 189 | #define SSECTION(_) | ||
| 190 | #define EXPR(name, fun)\ | ||
| 191 | case NodeKind::name: {\ | ||
| 192 | name##_t* node = reinterpret_cast<name##_t*>(expr);\ | ||
| 193 | return fun(node, depth + 1, (args)...);\ | ||
| 194 | } | ||
| 195 | |||
| 196 | 7575 | NODEKIND_ENUM(X, SSECTION, EXPR, PASS, PASS, PASS) | |
| 197 | |||
| 198 | #undef X | ||
| 199 | #undef PASS | ||
| 200 | #undef SSECTION | ||
| 201 | #undef EXPR | ||
| 202 | |||
| 203 | ✗ | default: | |
| 204 | ✗ | return ExprRet(); | |
| 205 | } | ||
| 206 | // clang-format on | ||
| 207 | return ExprRet(); | ||
| 208 | } | ||
| 209 | |||
| 210 | void check_depth(int depth) { | ||
| 211 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7504 times.
|
7532 | if (VisitorTrait::MaxRecursionDepth > 0 && depth > VisitorTrait::MaxRecursionDepth) { |
| 212 | ✗ | throw std::runtime_error(""); | |
| 213 | } | ||
| 214 | 7532 | } | |
| 215 | |||
| 216 | StmtRet exec(StmtNode_t* stmt, int depth, Args... args) { | ||
| 217 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1185 times.
|
1185 | if (!stmt) { |
| 218 | kwdebug("Null statement"); | ||
| 219 | ✗ | return StmtRet(); | |
| 220 | } | ||
| 221 | |||
| 222 | 1185 | check_depth(depth); | |
| 223 | |||
| 224 | // clang-format off | ||
| 225 |
25/26✓ Branch 0 taken 4 times.
✓ Branch 1 taken 99 times.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 258 times.
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 147 times.
✓ Branch 6 taken 41 times.
✓ Branch 7 taken 33 times.
✓ Branch 8 taken 20 times.
✓ Branch 9 taken 10 times.
✓ Branch 10 taken 33 times.
✓ Branch 11 taken 12 times.
✓ Branch 12 taken 9 times.
✓ Branch 13 taken 5 times.
✓ Branch 14 taken 17 times.
✓ Branch 15 taken 11 times.
✓ Branch 16 taken 8 times.
✓ Branch 17 taken 3 times.
✓ Branch 18 taken 3 times.
✓ Branch 19 taken 329 times.
✓ Branch 20 taken 83 times.
✓ Branch 21 taken 4 times.
✓ Branch 22 taken 13 times.
✓ Branch 23 taken 14 times.
✓ Branch 24 taken 4 times.
✗ Branch 25 not taken.
|
1185 | switch (stmt->kind) { |
| 226 | |||
| 227 | #define X(name, _) | ||
| 228 | #define PASS(a, b) | ||
| 229 | #define SSECTION(_) | ||
| 230 | #define STMT(name, fun)\ | ||
| 231 | case NodeKind::name: {\ | ||
| 232 | name##_t* n = reinterpret_cast<name##_t*>(stmt);\ | ||
| 233 | return this->fun(n, depth + 1, (args)...);\ | ||
| 234 | } | ||
| 235 | |||
| 236 | 1185 | NODEKIND_ENUM(X, SSECTION, PASS, STMT, PASS, PASS) | |
| 237 | |||
| 238 | #undef X | ||
| 239 | #undef PASS | ||
| 240 | #undef SSECTION | ||
| 241 | #undef STMT | ||
| 242 | |||
| 243 | ✗ | default: | |
| 244 | ✗ | return StmtRet(); | |
| 245 | } | ||
| 246 | // clang-format on | ||
| 247 | return StmtRet(); | ||
| 248 | } | ||
| 249 | |||
| 250 | #define FUNCTION_GEN(name, fun, rtype) \ | ||
| 251 | LY_INLINE rtype fun(name##_t* node, int depth, Args... args) { \ | ||
| 252 | if (Trace::value) { \ | ||
| 253 | kwtrace(depth, #name); \ | ||
| 254 | } \ | ||
| 255 | return static_cast<Implementation*>(this)->fun(node, depth, (args)...); \ | ||
| 256 | } | ||
| 257 | |||
| 258 | #define X(name, _) | ||
| 259 | #define SSECTION(name) | ||
| 260 | #define EXPR(name, fun) FUNCTION_GEN(name, fun, ExprRet) | ||
| 261 | #define STMT(name, fun) FUNCTION_GEN(name, fun, StmtRet) | ||
| 262 | #define MOD(name, fun) FUNCTION_GEN(name, fun, ModRet) | ||
| 263 | #define MATCH(name, fun) FUNCTION_GEN(name, fun, PatRet) | ||
| 264 | |||
| 265 | NODEKIND_ENUM(X, SSECTION, EXPR, STMT, MOD, MATCH) | ||
| 266 | |||
| 267 | #undef X | ||
| 268 | #undef SSECTION | ||
| 269 | #undef EXPR | ||
| 270 | #undef STMT | ||
| 271 | #undef MOD | ||
| 272 | #undef MATCH | ||
| 273 | |||
| 274 | #undef FUNCTION_GEN | ||
| 275 | |||
| 276 | }; // namespace lython | ||
| 277 | |||
| 278 | } // namespace lython | ||
| 279 | #endif | ||
| 280 |