| 1 | //===--- Disambiguate.cpp - Find the best tree in the forest --------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "clang-pseudo/Disambiguate.h" |
| 10 | |
| 11 | namespace clang::pseudo { |
| 12 | |
| 13 | Disambiguation disambiguate(const ForestNode *Root, |
| 14 | const DisambiguateParams &Params) { |
| 15 | // FIXME: this is a dummy placeholder strategy, implement a real one! |
| 16 | Disambiguation Result; |
| 17 | for (const ForestNode &N : Root->descendants()) { |
| 18 | if (N.kind() == ForestNode::Ambiguous) |
| 19 | Result.try_emplace(&N, 1); |
| 20 | } |
| 21 | return Result; |
| 22 | } |
| 23 | |
| 24 | void removeAmbiguities(ForestNode *&Root, const Disambiguation &D) { |
| 25 | std::vector<ForestNode **> Queue = {&Root}; |
| 26 | while (!Queue.empty()) { |
| 27 | ForestNode **Next = Queue.back(); |
| 28 | Queue.pop_back(); |
| 29 | switch ((*Next)->kind()) { |
| 30 | case ForestNode::Sequence: |
| 31 | for (ForestNode *&Child : (*Next)->elements()) |
| 32 | Queue.push_back(&Child); |
| 33 | break; |
| 34 | case ForestNode::Ambiguous: { |
| 35 | assert(D.count(*Next) != 0 && "disambiguation is incomplete!"); |
| 36 | ForestNode *ChosenChild = (*Next)->alternatives()[D.lookup(*Next)]; |
| 37 | *Next = ChosenChild; |
| 38 | Queue.push_back(Next); |
| 39 | break; |
| 40 | } |
| 41 | case ForestNode::Terminal: |
| 42 | case ForestNode::Opaque: |
| 43 | break; |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | } // namespace clang::pseudo |