Extracts chain of field names and depth from expression tree
| 41 | |
| 42 | // Extracts chain of field names and depth from expression tree |
| 43 | std::optional<std::pair<std::vector<std::string>, size_t>> |
| 44 | SingleSubfieldExtractor::extract(const core::ITypedExpr* root) { |
| 45 | multipleChainsFound_ = false; |
| 46 | |
| 47 | auto resultOpt = parseSingleChain(root); |
| 48 | if (!resultOpt || multipleChainsFound_) { |
| 49 | return std::nullopt; |
| 50 | } |
| 51 | |
| 52 | auto& [chain, totalDepth] = *resultOpt; |
| 53 | std::vector<std::string> names; |
| 54 | names.reserve(chain.size()); |
| 55 | |
| 56 | for (const auto* node : chain) { |
| 57 | switch (node->typedExprKind()) { |
| 58 | case core::kDereference: |
| 59 | names.push_back( |
| 60 | static_cast<const core::DereferenceTypedExpr*>(node)->name()); |
| 61 | break; |
| 62 | case core::kFieldAccess: |
| 63 | names.push_back( |
| 64 | static_cast<const core::FieldAccessTypedExpr*>(node)->name()); |
| 65 | break; |
| 66 | default: |
| 67 | BOLT_UNREACHABLE(); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | size_t depth = 0; |
| 72 | if (totalDepth >= names.size()) { |
| 73 | depth = totalDepth - names.size() + 1; |
| 74 | } |
| 75 | |
| 76 | VLOG(3) << "Extracted " << names.size() << " names with depth " << depth; |
| 77 | return std::make_pair(names, depth); |
| 78 | } |
| 79 | |
| 80 | // Main recursive function to parse expression chains |
| 81 | std::optional<std::pair<std::vector<const core::ITypedExpr*>, size_t>> |