summaryrefslogtreecommitdiffhomepage
path: root/flowgraph/graph.cpp
blob: 229ded8b1a3795b433701bd68fced8e4a5a29950 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "graph.h"

#include "minicc.h"
#include "node.h"

#include <string>

using namespace std::string_literals;

FlowGraph::Graph::Graph()
{
}

FlowGraph::Graph::Graph(const std::deque<std::shared_ptr<Node>>& nodes): std::deque<std::shared_ptr<Node>>(nodes)
{
 auto createLocalScope {std::make_shared<FlowGraph::CreateScopeOp>()};
 this->push_front(createLocalScope);

 auto destroyLocalScope {std::make_shared<FlowGraph::DestroyScopeOp>(createLocalScope->scope())};
 this->push_back(destroyLocalScope);
}

FlowGraph::Graph::~Graph()
{
}

// Assume first node of graph to be CreateScopeOp
std::shared_ptr<FlowGraph::LocalScope> FlowGraph::Graph::scope() const
{
 if (this->empty())
  throw std::runtime_error("ICE: FlowGraph expected to be non-empty!");

 auto& front_deref {*front()};

 if (typeid(front_deref) != typeid(FlowGraph::CreateScopeOp))
  throw std::runtime_error("ICE: Bad type of first graph element: "s + demangle(typeid(front_deref)));

 FlowGraph::CreateScopeOp& createScope {dynamic_cast<FlowGraph::CreateScopeOp&>(front_deref)};

 return createScope.scope();
}

void FlowGraph::Graph::append(const FlowGraph::Graph& other)
{
 // move graph nodes
 this->insert(this->end() - 1, other.begin() + 1, other.end() - 1);

 // move scope nodes to new scope
 this->scope()->append(*other.scope());
}

void FlowGraph::Graph::append(std::shared_ptr<FlowGraph::Node> node)
{
 this->insert(this->end() - 1, node);
}

std::shared_ptr<FlowGraph::Node> FlowGraph::Graph::lastOp() const
{
 if (size() >= 3)
  return *(end() - 2);

 throw std::runtime_error("ICE: No last operation found in graph");
}