-
Notifications
You must be signed in to change notification settings - Fork 1
/
sorter.cpp
131 lines (101 loc) · 3.26 KB
/
sorter.cpp
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "model.h"
#include "builder.h"
namespace fm = franca::model;
namespace {
void collectTypes(std::vector<const fm::type*>& typelist, const fm::typecollection& c)
{
std::for_each(c.types_.begin(), c.types_.end(), [&typelist](const fm::type* t){
typelist.push_back(t);
});
}
void collectTypes(std::vector<const fm::type*>& typelist, const fm::package& p)
{
// all packages
std::for_each(p.packages_.begin(), p.packages_.end(), [&typelist](const std::shared_ptr<fm::package>& p){
collectTypes(typelist, *p);
});
// typecollections...
std::for_each(p.collections_.begin(), p.collections_.end(), [&typelist](const fm::typecollection& tc){
collectTypes(typelist, tc);
});
// ...and interaces
std::for_each(p.interfaces_.begin(), p.interfaces_.end(), [&typelist](const fm::interface& i){
collectTypes(typelist, i);
});
}
// ---------------------------------------------------------------------
void clearTypes(fm::typecollection& c)
{
c.types_.clear();
}
void clearTypes(fm::package& p)
{
// all packages
std::for_each(p.packages_.begin(), p.packages_.end(), [](std::shared_ptr<fm::package>& p){
clearTypes(*p);
});
// typecollections...
std::for_each(p.collections_.begin(), p.collections_.end(), [](fm::typecollection& tc){
clearTypes(tc);
});
// ...and interaces
std::for_each(p.interfaces_.begin(), p.interfaces_.end(), [](fm::interface& i){
clearTypes(i);
});
}
} // anonymous namespace
// ---------------------------------------------------------------------
/*static*/
void franca::builder::sort_types(fm::package& pkg)
{
std::vector<const fm::type*> typelist;
std::vector<const fm::type*> sorted_typelist;
// collect all franca defined types in a single list for dependency sorting
collectTypes(typelist, pkg);
for (auto iter = typelist.begin(); iter != typelist.end(); ++iter)
{
if (sorted_typelist.empty())
{
sorted_typelist.push_back(*iter);
}
else
{
auto target = sorted_typelist.begin();
while(target != sorted_typelist.end())
{
bool r1 = (*iter)->depends(**target);
bool r2 = (*target)->depends(**iter);
if (r1)
{
if (r2)
{
assert(true); // circular reference encountered
}
else
{
// move on, try next
}
}
else
{
if (r2)
{
break; // jump out, need to insert it here
}
else
{
// completely independent
}
}
++target;
}
sorted_typelist.insert(target, *iter);
}
}
// clear all type collections!
clearTypes(pkg);
// reinsert types into their type collections in correct order...
std::for_each(sorted_typelist.begin(), sorted_typelist.end(), [](const fm::type* t){
t->parent_->types_.push_back(const_cast<fm::type*>(t));
});
}