|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +#include "SetRemovers.hpp" |
| 3 | +#include <slang/ast/ASTVisitor.h> |
| 4 | +#include <unordered_map> |
| 5 | + |
| 6 | +class FunctionArgMapper : public ASTVisitor<FunctionArgMapper, true, true, true> { |
| 7 | + // Builds vector that maps the argument in definition to all calls |
| 8 | + public: |
| 9 | + std::vector<SetRemover::RemovalSet> removals; |
| 10 | + |
| 11 | + void handle(const SubroutineSymbol& subroutine) { |
| 12 | + if (shouldProcess(subroutine)) { |
| 13 | + for (const FormalArgumentSymbol* arg : subroutine.getArguments()) { |
| 14 | + registerFormal(arg); |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + visitDefault(subroutine); |
| 19 | + } |
| 20 | + |
| 21 | + void handle(const MethodPrototypeSymbol& prototype) { |
| 22 | + if (shouldProcess(prototype)) { |
| 23 | + for (const FormalArgumentSymbol* arg : prototype.getArguments()) { |
| 24 | + registerFormal(arg); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + visitDefault(prototype); |
| 29 | + } |
| 30 | + |
| 31 | + void handle(const CallExpression& call) { |
| 32 | + const SubroutineSymbol* subroutine = getSubroutine(call); |
| 33 | + if (!subroutine || !shouldProcess(*subroutine)) { |
| 34 | + visitDefault(call); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + auto formals = subroutine->getArguments(); |
| 39 | + auto actuals = call.arguments(); |
| 40 | + size_t count = std::min(formals.size(), actuals.size()); |
| 41 | + for (size_t idx = 0; idx < count; idx++) { |
| 42 | + size_t removalIdx = registerFormal(formals[idx]); |
| 43 | + if (removalIdx == invalidIndex) { |
| 44 | + continue; |
| 45 | + } |
| 46 | + |
| 47 | + if (const SyntaxNode* argNode = getArgumentNode(actuals[idx])) { |
| 48 | + removals[removalIdx].push_back(argNode); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + visitDefault(call); |
| 53 | + } |
| 54 | + |
| 55 | + private: |
| 56 | + static constexpr size_t invalidIndex = std::numeric_limits<size_t>::max(); |
| 57 | + std::unordered_map<const FormalArgumentSymbol*, size_t> indexByFormal; |
| 58 | + |
| 59 | + size_t registerFormal(const FormalArgumentSymbol* formal) { |
| 60 | + if (!formal) { |
| 61 | + return invalidIndex; |
| 62 | + } |
| 63 | + |
| 64 | + if (auto it = indexByFormal.find(formal); it != indexByFormal.end()) { |
| 65 | + return it->second; |
| 66 | + } |
| 67 | + |
| 68 | + const SyntaxNode* node = getFormalNode(*formal); |
| 69 | + if (!node) { |
| 70 | + indexByFormal.emplace(formal, invalidIndex); |
| 71 | + return invalidIndex; |
| 72 | + } |
| 73 | + |
| 74 | + SetRemover::RemovalSet set; |
| 75 | + set.push_back(node); |
| 76 | + removals.push_back(std::move(set)); |
| 77 | + size_t idx = removals.size() - 1; |
| 78 | + indexByFormal.emplace(formal, idx); |
| 79 | + return idx; |
| 80 | + } |
| 81 | + |
| 82 | + static const SyntaxNode* getFormalNode(const FormalArgumentSymbol& formal) { |
| 83 | + if (auto syntax = formal.getSyntax()) { |
| 84 | + if (syntax->parent) { |
| 85 | + return syntax->parent; |
| 86 | + } |
| 87 | + return syntax; |
| 88 | + } |
| 89 | + return nullptr; |
| 90 | + } |
| 91 | + |
| 92 | + static const SyntaxNode* getArgumentNode(const Expression* expr) { |
| 93 | + if (!expr || !expr->syntax) { |
| 94 | + return nullptr; |
| 95 | + } |
| 96 | + |
| 97 | + const SyntaxNode* node = expr->syntax; |
| 98 | + while (node) { |
| 99 | + if (node->kind == SyntaxKind::OrderedArgument || |
| 100 | + node->kind == SyntaxKind::NamedArgument) { |
| 101 | + return node; |
| 102 | + } |
| 103 | + node = node->parent; |
| 104 | + } |
| 105 | + return nullptr; |
| 106 | + } |
| 107 | + |
| 108 | + static const SubroutineSymbol* getSubroutine(const CallExpression& call) { |
| 109 | + if (call.isSystemCall()) { |
| 110 | + return nullptr; |
| 111 | + } |
| 112 | + if (std::holds_alternative<const SubroutineSymbol*>(call.subroutine)) { |
| 113 | + return std::get<const SubroutineSymbol*>(call.subroutine); |
| 114 | + } |
| 115 | + return nullptr; |
| 116 | + } |
| 117 | + |
| 118 | + static bool shouldProcess(const SubroutineSymbol& subroutine) { |
| 119 | + return subroutine.subroutineKind == SubroutineKind::Function || |
| 120 | + subroutine.subroutineKind == SubroutineKind::Task; |
| 121 | + } |
| 122 | + |
| 123 | + static bool shouldProcess(const MethodPrototypeSymbol& prototype) { |
| 124 | + return prototype.subroutineKind == SubroutineKind::Function || |
| 125 | + prototype.subroutineKind == SubroutineKind::Task; |
| 126 | + } |
| 127 | +}; |
| 128 | + |
| 129 | +SetRemover makeFunctionArgRemover(std::shared_ptr<SyntaxTree> tree) { |
| 130 | + Compilation compilation; |
| 131 | + compilation.addSyntaxTree(tree); |
| 132 | + compilation.getAllDiagnostics(); |
| 133 | + FunctionArgMapper mapper; |
| 134 | + compilation.getRoot().visit(mapper); |
| 135 | + return SetRemover(std::move(mapper.removals)); |
| 136 | +} |
0 commit comments