source: icGREP/icgrep-devel/icgrep/re/collect_ccs.cpp @ 5997

Last change on this file since 5997 was 5935, checked in by cameron, 19 months ago

Direct CC builder work

File size: 2.0 KB
Line 
1#include "collect_ccs.h"
2#include <cc/alphabet.h>
3#include <re/re_name.h>
4#include <re/re_any.h>
5#include <re/re_alt.h>
6#include <re/re_cc.h>
7#include <re/re_seq.h>
8#include <re/re_rep.h>
9#include <re/re_range.h>
10#include <re/re_diff.h>
11#include <re/re_intersect.h>
12#include <re/re_assertion.h>
13#include <cc/alphabet.h>
14#include <re/re_memoizer.hpp>
15
16#include <boost/container/flat_set.hpp>
17#include <llvm/Support/raw_ostream.h>
18
19using namespace llvm;
20
21namespace re {
22   
23struct SetCollector : private Memoizer {
24    void collect(RE * const re);
25public:
26    const cc::Alphabet * alphabet;
27    std::vector<CC *> theSets;
28    std::set<Name *> ignoredExternals;
29};
30
31void SetCollector::collect(RE * const re) {
32    assert ("RE object cannot be null!" && re);
33    if (CC * cc = dyn_cast<CC>(re)) {
34        if (cc->getAlphabet() == alphabet) {
35            if (find(cc) == end()) {
36                cc = memoize(cc);
37                theSets.push_back(cc);
38            }
39        }
40    } else if (isa<Name>(re)) {
41        if (ignoredExternals.find(cast<Name>(re)) != ignoredExternals.end()) return;
42        auto def = cast<Name>(re)->getDefinition();
43        if (def != nullptr)
44            collect(def);
45    } else if (isa<Seq>(re)) {
46        for (auto item : *cast<Seq>(re)) {
47            collect(item);
48        }
49    } else if (isa<Alt>(re)) {
50        for (auto item : *cast<Alt>(re)) {
51            collect(item);
52        }
53    } else if (isa<Rep>(re)) {
54        collect(cast<Rep>(re)->getRE());
55    } else if (isa<Assertion>(re)) {
56        collect(cast<Assertion>(re)->getAsserted());
57    } else if (isa<Diff>(re)) {
58        collect(cast<Diff>(re)->getLH());
59        collect(cast<Diff>(re)->getRH());
60    } else if (isa<Intersect>(re)) {
61        collect(cast<Intersect>(re)->getLH());
62        collect(cast<Intersect>(re)->getRH());
63    }
64}
65
66std::vector<CC *> collectCCs(RE * const re, const cc::Alphabet * a, std::set<Name *> external) {
67    SetCollector collector;
68    collector.alphabet = a;
69    collector.ignoredExternals = external;
70    collector.collect(re);
71    return collector.theSets;
72}
73
74
75
76}
Note: See TracBrowser for help on using the repository browser.