|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#include "KeyCapture.h" |
| 17 | + |
| 18 | +#include "TypeGraph.h" |
| 19 | + |
| 20 | +namespace oi::detail::type_graph { |
| 21 | + |
| 22 | +Pass KeyCapture::createPass( |
| 23 | + const std::vector<OICodeGen::Config::KeyToCapture>& keysToCapture, |
| 24 | + std::vector<std::unique_ptr<ContainerInfo>>& containerInfos) { |
| 25 | + auto fn = [&keysToCapture, &containerInfos](TypeGraph& typeGraph, |
| 26 | + NodeTracker& tracker) { |
| 27 | + KeyCapture pass{tracker, typeGraph, keysToCapture, containerInfos}; |
| 28 | + pass.insertCaptureDataNodes(typeGraph.rootTypes()); |
| 29 | + }; |
| 30 | + |
| 31 | + return Pass("KeyCapture", fn); |
| 32 | +} |
| 33 | + |
| 34 | +/* |
| 35 | + * This function should be used as the main entry point to this pass, to add |
| 36 | + * special handling of top-level types. |
| 37 | + */ |
| 38 | +void KeyCapture::insertCaptureDataNodes( |
| 39 | + std::vector<std::reference_wrapper<Type>>& types) { |
| 40 | + for (const auto& keyToCapture : keysToCapture_) { |
| 41 | + if (!keyToCapture.topLevel) |
| 42 | + continue; |
| 43 | + |
| 44 | + // Capture keys from all top-level types |
| 45 | + for (size_t i = 0; i < types.size(); i++) { |
| 46 | + types[i] = captureKey(types[i]); |
| 47 | + } |
| 48 | + break; |
| 49 | + } |
| 50 | + |
| 51 | + for (const auto& type : types) { |
| 52 | + accept(type); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +void KeyCapture::accept(Type& type) { |
| 57 | + if (tracker_.visit(type)) |
| 58 | + return; |
| 59 | + |
| 60 | + type.accept(*this); |
| 61 | +} |
| 62 | + |
| 63 | +void KeyCapture::visit(Class& c) { |
| 64 | + for (const auto& keyToCapture : keysToCapture_) { |
| 65 | + if (!keyToCapture.type.has_value() || c.name() != *keyToCapture.type) |
| 66 | + continue; |
| 67 | + if (!keyToCapture.member.has_value()) |
| 68 | + continue; |
| 69 | + for (size_t i = 0; i < c.members.size(); i++) { |
| 70 | + auto& member = c.members[i]; |
| 71 | + if (member.name != *keyToCapture.member) |
| 72 | + continue; |
| 73 | + |
| 74 | + member = Member{captureKey(member.type()), member}; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + RecursiveVisitor::visit(c); |
| 79 | +} |
| 80 | + |
| 81 | +/* |
| 82 | + * captureKey |
| 83 | + * |
| 84 | + * If the given type is a container, insert a CaptureKey node above it. |
| 85 | + * Otherwise, just return the container node unchanged. |
| 86 | + * |
| 87 | + * Before: |
| 88 | + * Container: std::map |
| 89 | + * Param |
| 90 | + * [KEY] |
| 91 | + * Param |
| 92 | + * [VAL] |
| 93 | + * |
| 94 | + * After: |
| 95 | + * CaptureKeys |
| 96 | + * Container: std::map |
| 97 | + * Param |
| 98 | + * [KEY] |
| 99 | + * Param |
| 100 | + * [VAL] |
| 101 | + */ |
| 102 | +Type& KeyCapture::captureKey(Type& type) { |
| 103 | + auto* container = dynamic_cast<Container*>(&type); |
| 104 | + if (!container) // We only want to capture keys from containers |
| 105 | + return type; |
| 106 | + |
| 107 | + /* |
| 108 | + * Create a copy of the container info for capturing keys. |
| 109 | + * CodeGen and other places may deduplicate containers based on the container |
| 110 | + * info object, so it is necessary to create a new one when we want different |
| 111 | + * behaviour. |
| 112 | + */ |
| 113 | + auto newContainerInfo = container->containerInfo_.clone(); |
| 114 | + newContainerInfo.captureKeys = true; |
| 115 | + auto infoPtr = std::make_unique<ContainerInfo>(std::move(newContainerInfo)); |
| 116 | + const auto& info = containerInfos_.emplace_back(std::move(infoPtr)); |
| 117 | + |
| 118 | + auto& captureKeysNode = typeGraph_.makeType<CaptureKeys>(*container, *info); |
| 119 | + return captureKeysNode; |
| 120 | +} |
| 121 | + |
| 122 | +} // namespace oi::detail::type_graph |
0 commit comments