-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdocument.cpp
More file actions
1121 lines (994 loc) · 34 KB
/
document.cpp
File metadata and controls
1121 lines (994 loc) · 34 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -*- mode: C++; c-file-style: "stroustrup"; c-basic-offset: 4; indent-tabs-mode: nil; -*-
/* libutap - Uppaal Timed Automata Parser.
Copyright (C) 2002-2006 Uppsala University and Aalborg University.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation; either version 2.1 of
the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
*/
#include "utap/document.h"
#include "utap/builder.h"
#include "utap/statement.h"
#include <functional> // std::mem_fn
#include <iostream>
#include <sstream>
#include <stack>
#include <utility> // declval
#include <cassert>
#ifdef __MINGW32__
#include <windows.h>
#elif defined(__linux__) || defined(__APPLE__)
#include <dlfcn.h>
#endif
using namespace UTAP;
using namespace Constants;
using std::list;
using std::stack;
using std::vector;
using std::map;
using std::pair;
using std::make_pair;
using std::min;
using std::max;
using std::set;
using std::string;
using std::ostream;
using std::deque;
static const char* const unsupported = "Internal error: Feature not supported in this mode.";
static const char* const invalid_type = "$Invalid_type";
template <typename Item>
std::string stringify_t<Item>::str() const
{
auto os = std::ostringstream{};
static_cast<const Item*>(this)->print(os);
return os.str();
}
template <typename Item>
std::string stringify_indent_t<Item>::str(const std::string& indent) const
{
auto os = std::ostringstream{};
static_cast<const Item*>(this)->print(os, indent);
return os.str();
}
namespace UTAP { // Explicit instantiations to generate implementation
template struct stringify_t<chan_priority_t>;
template struct stringify_t<variable_t>;
template struct stringify_t<location_t>;
template struct stringify_t<edge_t>;
template struct stringify_t<function_t>;
template struct stringify_t<declarations_t>;
template struct stringify_t<simregion_t>;
template struct stringify_t<cut_t>;
} // namespace UTAP
std::ostream& location_t::print(std::ostream& os) const
{
os << "LOCATION (" << uid.get_name() << ", ";
invariant.print(os) << ", ";
exp_rate.print(os) << ')';
return os;
}
std::ostream& edge_t::print(std::ostream& os) const
{
os << "EDGE (";
src->print(os) << ' ';
dst->print(os) << ")\n";
os << "\t";
guard.print(os) << ", ";
sync.print(os) << ", ";
assign.print(os);
return os;
}
std::ostream& function_t::print(std::ostream& os) const
{
auto type = uid.get_type(); // the function type/signature
type.get(0).print(os) << ' ' // return type
<< uid.get_name() << '('; // function name
if (type.size() > 1) { // has arguments
type.get(1).print_declaration(os) << ' ' << type.get_label(1); // first parameter type and name
for (uint32_t i = 2; i < type.size(); ++i) // remaining parameters
type.get(i).print_declaration(os << ", ") << ' ' << type.get_label(i);
}
os << ")\n{\n"; // open function body
for (auto& variable : variables)
variable.print(os << " ") << ";\n";
os << body->str(INDENT);
return os << "}";
}
std::ostream& variable_t::print(std::ostream& os) const
{
string type = uid.get_type().declaration();
if (uid.get_type().is_array()) {
auto i = type.find('[');
assert(i != std::string::npos);
os << type.substr(0, i) << ' ' << uid.get_name() << type.substr(i, type.length() - i);
} else {
os << type << " " << uid.get_name();
}
if (!init.empty())
init.print(os << " = ");
return os;
}
bool declarations_t::add_function(type_t type, string name, position_t pos, function_t*& fun)
{
bool duplicate = frame.contains(name);
fun = &functions.emplace_back();
fun->uid = frame.add_symbol(name, type, pos, fun); // Add symbol
return !duplicate;
}
std::ostream& declarations_t::print(std::ostream& os, bool global) const
{
print_constants(os) << "\n";
print_typedefs(os) << "\n";
print_variables(os, global) << "\n";
print_functions(os);
return os;
}
std::string declarations_t::str(bool global) const
{
auto os = std::ostringstream{};
print(os, global);
return os.str();
}
std::ostream& declarations_t::print_constants(std::ostream& os) const
{
if (!variables.empty()) {
bool first = true;
for (const auto& variable : variables) {
if (variable.uid.get_type().get_kind() == CONSTANT) {
if (first) {
os << "// constants\n";
first = false;
}
variable.print(os) << ";\n";
}
}
}
return os;
}
std::ostream& declarations_t::print_typedefs(std::ostream& os) const
{
bool first = true;
for (auto& symbol : frame) {
if (symbol.get_type().get_kind() == TYPEDEF) {
if (first) {
os << "// type definitions\n";
first = false;
}
symbol.get_type().print_declaration(os) << ";\n";
}
}
return os;
}
std::ostream& declarations_t::print_variables(std::ostream& os, bool global) const
{
if (!variables.empty()) {
os << "// variables\n";
for (const auto& var : variables)
if (var.uid.get_type().get_kind() != CONSTANT)
var.print(os) << ";\n";
}
return os;
}
std::ostream& declarations_t::print_functions(std::ostream& os) const
{
if (!functions.empty()) {
os << "// functions\n";
for (const auto& fun : functions)
fun.print(os) << "\n\n";
}
return os;
}
std::ostream& instance_t::print_mapping(std::ostream& os) const
{
for (const auto& [symbol, expr] : mapping)
os << symbol.get_name() << " = " << expr.str() << "\n";
return os;
}
std::ostream& instance_t::print_parameters(std::ostream& os) const
{
auto b = std::begin(parameters), e = std::end(parameters);
if (b != e) {
b->get_type().print_declaration(os) << " " << b->get_name();
while (++b != e)
b->get_type().print_declaration(os << ", ") << " " << b->get_name();
}
return os;
}
std::ostream& instance_t::print_arguments(std::ostream& os) const
{
auto b = std::begin(parameters), e = std::end(parameters);
if (b != e) {
auto itr = mapping.find(*b);
assert(itr != std::end(mapping));
itr->second.print(os);
while (++b != e) {
itr = mapping.find(*b);
assert(itr != std::end(mapping));
itr->second.print(os << ", ");
}
}
return os;
}
std::string instance_t::mapping_str() const
{
auto os = std::ostringstream{};
print_mapping(os);
return os.str();
}
std::string instance_t::parameters_str() const
{
auto os = std::ostringstream{};
print_parameters(os);
return os.str();
}
std::string instance_t::arguments_str() const
{
auto os = std::ostringstream{};
print_arguments(os);
return os.str();
}
location_t& template_t::add_location(const string& name, expression_t inv, expression_t er, position_t pos)
{
bool duplicate = frame.contains(name);
auto& loc = locations.emplace_back();
loc.uid = frame.add_symbol(name, type_t::create_primitive(LOCATION), pos, &loc);
loc.nr = locations.size() - 1;
loc.invariant = inv;
loc.exp_rate = er;
if (duplicate) {
throw DuplicateDefinitionError(name);
}
return loc;
}
// FIXME: like for unnamed locations, a name is autegenerated
// this name may conflict with user-defined names
branchpoint_t& template_t::add_branchpoint(const string& name, position_t pos)
{
bool duplicate = frame.contains(name);
auto& branchpoint = branchpoints.emplace_back();
branchpoint.uid = frame.add_symbol(name, type_t::create_primitive(BRANCHPOINT), pos, &branchpoint);
branchpoint.bpNr = branchpoints.size() - 1;
if (duplicate) {
throw DuplicateDefinitionError(name);
}
return branchpoint;
}
edge_t& template_t::add_edge(symbol_t src, symbol_t dst, bool control, string actname)
{
int32_t nr = edges.empty() ? 0 : edges.back().nr + 1;
edge_t& edge = edges.emplace_back();
if (src.get_type().is_location()) {
edge.src = static_cast<location_t*>(src.get_data());
edge.srcb = nullptr;
} else {
edge.src = nullptr;
edge.srcb = static_cast<branchpoint_t*>(src.get_data());
}
if (dst.get_type().is_location()) {
edge.dst = static_cast<location_t*>(dst.get_data());
edge.dstb = nullptr;
} else {
edge.dst = nullptr;
edge.dstb = static_cast<branchpoint_t*>(dst.get_data());
}
edge.control = control;
edge.color = 0x000000;
edge.actname = std::move(actname);
edge.nr = nr;
return edge;
}
// LSC
instance_line_t& template_t::add_instance_line()
{
// bool duplicate = frame.get_index_of(name) != -1;
instance_line_t& instance = instances.emplace_back();
// instance.uid = frame.add_symbol(name, type_t::create_primitive(INSTANCELINE), &instance);
instance.instance_nr = instances.size() - 1;
// if (duplicate)
// {
// throw TypeException(boost::format("$Duplicate_definition_of %1%") % name);
// }
return instance;
}
message_t& template_t::add_message(symbol_t src, symbol_t dst, int loc, bool pch)
{
int32_t nr = messages.empty() ? 0 : messages.back().nr + 1;
auto& message = messages.emplace_back(nr);
message.src = static_cast<instance_line_t*>(src.get_data());
message.dst = static_cast<instance_line_t*>(dst.get_data());
message.location = loc;
message.is_in_prechart = pch;
return message;
}
update_t& template_t::add_update(symbol_t anchor, int loc, bool pch)
{
int32_t nr = updates.empty() ? 0 : updates.back().nr + 1;
auto& update = updates.emplace_back(nr);
update.anchor = static_cast<instance_line_t*>(anchor.get_data());
update.location = loc;
update.is_in_prechart = pch;
return update;
}
condition_t& template_t::add_condition(vector<symbol_t> anchors, int loc, bool pch, bool isHot)
{
int32_t nr = conditions.empty() ? 0 : conditions.back().nr + 1;
auto& condition = conditions.emplace_back(nr);
for (auto& anchor : anchors) {
condition.anchors.push_back(static_cast<instance_line_t*>(anchor.get_data())); // TODO
}
condition.location = loc;
condition.is_in_prechart = pch;
condition.isHot = isHot;
return condition;
}
template <typename Fn, typename Element, typename Res = std::invoke_result_t<Fn, Element>>
deque<Res> collect(Fn&& fn, const deque<Element>& elements)
{
auto res = std::deque<Res>{};
std::transform(std::begin(elements), std::end(elements), std::back_inserter(res), std::forward<Fn>(fn));
return res;
}
/**
* returns the simregions of an LSC scenario.
* A simregion is a simultaneous region containing
* 1 or 0 message,
* 1 or 0 update and
* 1 or 0 condition,
* at the same location.
* a message, update or condition must be in only one simregion.
*/
const vector<simregion_t> template_t::get_simregions()
{
// cout <<"=======LSC: get_simregions=======\n";
// Copy the numbers of messages, conditions and updates from the scenario
deque<int> m_nr = collect(std::mem_fn(&message_t::get_nr), messages);
deque<int> c_nr = collect(std::mem_fn(&condition_t::get_nr), conditions);
deque<int> u_nr = collect(std::mem_fn(&update_t::get_nr), updates);
auto simregions = vector<simregion_t>{};
simregions.reserve(m_nr.size());
/**
* iterates over messages
*/
for (auto& message_nr : m_nr) {
simregion_t s = simregion_t();
s.set_message(messages, message_nr);
instance_line_t* source = s.message->src;
instance_line_t* target = s.message->dst;
int y = s.message->location;
/**
* we give priority to the condition on the target, if there is also one
* in the source, it must be part of another simregion
*/
if (get_condition(*target, y, s.condition)) {
for (auto c_itr = c_nr.begin(); c_itr != c_nr.end(); ++c_itr) {
if (*c_itr == s.condition->nr) {
c_nr.erase(c_itr);
break;
}
}
} else if (get_condition(*source, y, s.condition)) {
for (auto c_itr = c_nr.begin(); c_itr != c_nr.end(); ++c_itr) {
if (*c_itr == s.condition->nr) {
c_nr.erase(c_itr);
break;
}
}
}
if (get_update(*target, y, s.update)) {
for (auto u_itr = u_nr.begin(); u_itr != u_nr.end(); ++u_itr) {
if (*u_itr == s.update->nr) {
u_nr.erase(u_itr);
break;
}
}
} else if (get_update(*source, y, s.update)) {
for (auto u_itr = u_nr.begin(); u_itr != u_nr.end(); ++u_itr) {
if (*u_itr == s.update->nr) {
u_nr.erase(u_itr);
break;
}
}
}
s.nr = simregions.size();
simregions.push_back(std::move(s));
}
/**
* iterates over remaining conditions
*/
for (auto& c_itr : c_nr) {
auto s = simregion_t{};
s.set_condition(conditions, c_itr);
int y = s.condition->location;
if (get_update(s.condition->anchors, y, s.update)) {
for (auto u_itr = u_nr.begin(); u_itr != u_nr.end(); ++u_itr) {
if (*u_itr == s.update->nr) {
u_nr.erase(u_itr);
break;
}
}
}
s.nr = simregions.size();
simregions.push_back(s);
}
/**
* iterates over remaining updates
*/
for (auto& u_itr : u_nr) {
auto s = simregion_t();
s.set_update(updates, u_itr);
s.nr = simregions.size();
simregions.push_back(s);
}
/*// cout << "-----unordered simregions-----\n";
for (unsigned int i = 0; i < simregions.size(); ++i){
// cout << simregions[i].str() << " " << simregions[i].nr<< "\n";
} //test OK*/
return simregions;
}
/**
* gets the condition on the given instance, at y location,
* returns false if there isn't any
*/
bool template_t::get_condition(instance_line_t& instance, int y, condition_t*& simCondition)
{
for (auto& condition : conditions) {
if (condition.location == y) {
for (auto& anchor : condition.anchors) {
instance_line_t* instancej = anchor;
if (instancej->instance_nr == instance.instance_nr) {
simCondition = &condition;
return true;
}
}
}
}
return false;
}
/**
* gets the update on the given instance at y location,
* returns false if there isn't any
*/
bool template_t::get_update(instance_line_t& instance, int y, update_t*& simUpdate)
{
for (auto& update : updates) {
if (update.location == y) {
if (update.anchor->instance_nr == instance.instance_nr) {
simUpdate = &update;
return true;
}
}
}
return false;
}
/**
* gets the first update on one of the given instances, at y location
* (in simUpdate), returns false if there isn't any
*/
bool template_t::get_update(vector<instance_line_t*>& instances, int y, update_t*& simUpdate)
{
for (auto& instance : instances) {
if (get_update(*instance, y, simUpdate))
return true;
}
return false;
}
void instance_line_t::add_parameters(instance_t& inst, frame_t params, const vector<expression_t>& arguments1)
{
unbound = params.get_size();
parameters = params;
parameters.add(inst.parameters);
mapping = inst.mapping;
arguments = arguments1.size();
templ = inst.templ;
for (size_t i = 0; i < arguments1.size(); i++) {
mapping[inst.parameters[i]] = arguments1[i];
}
}
/**
* return the simregions anchored to this instance,
* ordered by location number
*/
vector<simregion_t> instance_line_t::getSimregions(const vector<simregion_t>& simregions)
{
vector<simregion_t> i_simregions;
// get the simregions anchored to this instance
for (const auto& reg : simregions) {
const message_t* m = reg.message;
if ((m->src->instance_nr == this->instance_nr || m->dst->instance_nr == this->instance_nr)) {
i_simregions.push_back(reg);
continue;
}
const update_t* u = reg.update;
if (u->anchor->instance_nr == this->instance_nr) {
i_simregions.push_back(reg);
continue;
}
const condition_t* c = reg.condition;
for (auto* instance : c->anchors) {
if (instance->instance_nr == this->instance_nr) {
i_simregions.push_back(reg);
break;
}
}
}
// ordering the simregions by location number
sort(i_simregions.begin(), i_simregions.end(), compare_simregion());
// std::cout << "--------instance--------\n";
// for (auto& reg : i_simregions) {
// std::cout << reg.str() << " " << reg.getLoc() << " " << reg.is_in_prechart()<<"\n";
// } //test OK
return i_simregions;
}
int simregion_t::get_loc() const
{
if (has_message())
return message->location;
if (has_condition())
return condition->location;
if (has_update())
return update->location;
assert(false);
return -1; // should not happen
}
bool simregion_t::is_in_prechart() const
{
if (has_message())
return message->is_in_prechart;
if (has_condition())
return condition->is_in_prechart;
if (has_update())
return update->is_in_prechart;
assert(false);
return false; // should not happen
}
void simregion_t::set_message(std::deque<message_t>& messages, uint32_t nr)
{
for (auto& message : messages) {
if (message.nr == nr) {
this->message = &message;
return;
}
}
}
void simregion_t::set_condition(std::deque<condition_t>& conditions, uint32_t nr)
{
for (auto& condition : conditions) {
if (condition.nr == nr) {
this->condition = &condition;
return;
}
}
}
void simregion_t::set_update(std::deque<update_t>& updates, uint32_t nr)
{
for (auto& update : updates) {
if (update.nr == nr) {
this->update = &update;
return;
}
}
}
std::ostream& simregion_t::print(std::ostream& os) const
{
os << "s(";
auto need_sep = false;
if (has_message()) {
message->label.print(os << "m:");
need_sep = true;
}
if (has_condition()) {
if (need_sep)
os << " ";
else
need_sep = true;
os << "c:";
if (condition->isHot)
os << " HOT ";
condition->label.print(os);
}
if (has_update()) {
if (need_sep)
os << " ";
update->label.print(os << "u:");
}
return os << ")";
}
inline auto find_simregion_by_nr(uint32_t nr)
{
return [nr](const simregion_t& reg) { return reg.nr == nr; };
}
void cut_t::erase(const simregion_t& s)
{
simregions.erase(std::remove_if(simregions.begin(), simregions.end(), find_simregion_by_nr(s.nr)),
simregions.end());
}
bool cut_t::contains(const simregion_t& s) const
{
return std::find_if(simregions.begin(), simregions.end(), find_simregion_by_nr(s.nr)) != simregions.end();
}
/**
* returns true if the cut is in the prechart,
* given one of the following simregions.
* if one of the following simregions is not in the prechart,
* then all following simregions aren't in the prechart (because of the
* construction of the partial order),
* and the cut is not in the prechart (but can contain only simregions
* that are in the prechart, if it is the limit between the prechart
* and the mainchart)
*/
bool cut_t::is_in_prechart(const simregion_t& fSimregion) const
{
if (!is_in_prechart())
return false;
if (!fSimregion.is_in_prechart())
return false;
return true;
}
bool cut_t::is_in_prechart() const
{
return std::all_of(simregions.begin(), simregions.end(), [](auto& sr) { return sr.is_in_prechart(); });
}
bool cut_t::equals(const cut_t& y) const
{
if (simregions.size() != y.simregions.size())
return false;
auto ycopy = y.simregions;
for (const auto& s : simregions)
ycopy.erase(std::remove_if(ycopy.begin(), ycopy.end(), find_simregion_by_nr(s.nr)), ycopy.end());
return ycopy.empty();
}
std::ostream& cut_t::print(std::ostream& os) const
{
os << "CUT(";
if (auto b = simregions.begin(), e = simregions.end(); b != e) {
b->print(os);
while (++b != e)
b->print(os << " ");
}
return os << ")";
}
/**
* return true if the LSC is of invariant mode
*/
bool template_t::is_invariant() const
{
if (is_TA)
return false;
return mode == "invariant";
}
std::ostream& chan_priority_t::print(std::ostream& os) const
{
os << "chan priority ";
auto head_s = head.str();
if (head_s.empty())
head_s = "default";
os << head_s;
for (const auto& [ch, expr] : tail) {
if (ch == '<')
os << ' ';
os << ch << ' ';
expr.print(os);
}
return os;
}
Document::Document()
{
global.frame = frame_t::create();
#ifdef ENABLE_CORA
addVariable(&global, type_t::create_primitive(COST), "cost", expression_t());
#endif
}
void Document::add(Library&& lib) { libraries.push_back(std::move(lib)); }
Library& Document::last_library()
{
if (libraries.empty())
throw std::runtime_error("$No_library_loaded");
return libraries.back();
}
/** Creates and returns a new template. The template is created with
* the given name and parameters and added to the global frame. The
* method does not check for duplicate declarations. An instance with
* the same name and parameters is added as well.
*/
template_t& Document::add_template(const string& name, frame_t params, position_t position, const bool is_TA,
const string& typeLSC, const string& mode)
{
type_t type = (is_TA) ? type_t::create_instance(params) : type_t::create_LSC_instance(params);
template_t& templ = templates.emplace_back();
templ.parameters = params;
templ.frame = frame_t::create(global.frame);
templ.frame.add(params);
templ.templ = &templ;
templ.uid = global.frame.add_symbol(name, type, position, (instance_t*)&templ);
templ.arguments = 0;
templ.unbound = params.get_size();
templ.is_TA = is_TA;
templ.dynamic = false;
// LSC
templ.type = typeLSC;
templ.mode = mode;
return templ;
}
template_t& Document::add_dynamic_template(const std::string& name, frame_t params, position_t pos)
{
type_t type = type_t::create_instance(params);
dyn_templates.emplace_back();
template_t& templ = dyn_templates.back();
templ.parameters = params;
templ.frame = frame_t::create(global.frame);
templ.frame.add(params);
templ.templ = &templ;
templ.uid = global.frame.add_symbol(name, type, pos, (instance_t*)&templ);
templ.arguments = 0;
templ.unbound = params.get_size();
templ.is_TA = true;
templ.dynamic = true;
templ.dyn_index = dyn_templates.size() - 1;
templ.is_defined = false;
return templ;
}
std::vector<template_t*>& Document::get_dynamic_templates()
{
if (dyn_templates_vec.size() != dyn_templates.size()) {
dyn_templates_vec.clear();
dyn_templates_vec.reserve(dyn_templates.size());
for (auto&& t : dyn_templates)
dyn_templates_vec.push_back(&t);
}
return dyn_templates_vec;
}
inline auto equal_name(const std::string& name)
{
return [&name](const auto& e) { return (e.uid.get_name() == name); };
}
const template_t* Document::find_template(const std::string& name) const
{
auto has_name = equal_name(name);
auto it = std::find_if(templates.begin(), templates.end(), has_name);
if (it == templates.end()) {
it = std::find_if(std::begin(dyn_templates), std::end(dyn_templates), has_name);
if (it == std::end(dyn_templates))
return nullptr;
}
return &(*it);
}
template_t* Document::find_dynamic_template(const std::string& name)
{
auto it = std::find_if(dyn_templates.begin(), dyn_templates.end(), equal_name(name));
if (it == std::end(dyn_templates))
return nullptr;
return &(*it);
}
instance_t& Document::add_instance(const string& name, instance_t& inst, frame_t params,
const vector<expression_t>& arguments, position_t pos)
{
type_t type = type_t::create_instance(params);
instance_t& instance = instances.emplace_back();
instance.uid = global.frame.add_symbol(name, type, pos, &instance);
instance.unbound = params.get_size();
instance.parameters = params;
instance.parameters.add(inst.parameters);
instance.mapping = inst.mapping;
instance.arguments = arguments.size();
instance.templ = inst.templ;
for (size_t i = 0; i < arguments.size(); ++i)
instance.mapping[inst.parameters[i]] = arguments[i];
return instance;
}
instance_t& Document::add_LSC_instance(const string& name, instance_t& inst, frame_t params,
const vector<expression_t>& arguments, position_t pos)
{
type_t type = type_t::create_LSC_instance(params);
instance_t& instance = lsc_instances.emplace_back();
instance.uid = global.frame.add_symbol(name, type, pos, &instance);
instance.unbound = params.get_size();
instance.parameters = params;
instance.parameters.add(inst.parameters);
instance.mapping = inst.mapping;
instance.arguments = arguments.size();
instance.templ = inst.templ;
for (size_t i = 0; i < arguments.size(); ++i)
instance.mapping[inst.parameters[i]] = arguments[i];
return instance;
}
void Document::remove_process(instance_t& instance)
{
get_globals().frame.remove(instance.uid);
for (auto itr = processes.cbegin(); itr != processes.cend(); ++itr) {
if (itr->uid == instance.uid) {
processes.erase(itr);
break;
}
}
}
void Document::add_process(instance_t& instance, position_t pos)
{
type_t type;
instance_t& process = processes.emplace_back(instance);
if (process.unbound == 0)
type = type_t::create_process(process.templ->frame);
else
type = type_t::create_process_set(instance.uid.get_type());
process.uid = global.frame.add_symbol(instance.uid.get_name(), type, pos, &process);
}
bool Document::queries_empty() const { return queries.empty(); }
void Document::add_gantt(declarations_t* context, gantt_t g) { context->ganttChart.push_back(std::move(g)); }
void Document::add_query(query_t query) { queries.push_back(std::move(query)); }
options_t& Document::get_options() { return model_options; }
void Document::set_options(const options_t& options) { model_options = options; }
// Add a regular variable
variable_t* Document::add_variable(declarations_t* context, type_t type, const string& name, expression_t initial,
position_t pos)
{
variable_t* var = add_variable(context->variables, context->frame, type, name, pos);
var->init = initial;
return var;
}
variable_t* Document::add_variable_to_function(function_t* function, frame_t frame, type_t type, const string& name,
expression_t initial, position_t pos)
{
variable_t* var = add_variable(function->variables, frame, type, name, pos);
var->init = initial;
return var;
}
// Add a regular variable
variable_t* Document::add_variable(list<variable_t>& variables, frame_t frame, type_t type, const string& name,
position_t pos)
{
bool duplicate = frame.contains(name);
// Add variable
variable_t& var = variables.emplace_back();
// Add symbol
var.uid = frame.add_symbol(name, type, pos, &var);
if (duplicate)
throw DuplicateDefinitionError(name);
return &var;
}
void Document::copy_variables_from_to(const template_t* from, template_t* to) const
{
for (auto&& var : from->variables) {
to->variables.push_back(var);
to->frame.add(var.uid);
}
}
void Document::copy_functions_from_to(const template_t* from, template_t* to) const
{
// TODO to be implemented and to be used in Translator::lscProcBegin (see Translator.cpp)
}
void Document::add_progress_measure(declarations_t* context, expression_t guard, expression_t measure)
{
context->progress.emplace_back(guard, measure);
}
static void visit(DocumentVisitor& visitor, frame_t frame)
{
for (size_t i = 0; i < frame.get_size(); ++i) {
type_t type = frame[i].get_type();
if (type.get_kind() == TYPEDEF) {
visitor.visitTypeDef(frame[i]);
continue;
}
void* data = frame[i].get_data();
type = type.strip_array();
if ((type.is(Constants::INT) || type.is(Constants::STRING) || type.is(Constants::DOUBLE) ||
type.is(Constants::BOOL) || type.is(CLOCK) || type.is(CHANNEL) || type.is(SCALAR) ||
type.get_kind() == RECORD) &&
data != nullptr) // <--- ignore parameters
{
visitor.visitVariable(*static_cast<variable_t*>(data));
} else if (type.is(LOCATION)) {
visitor.visitLocation(*static_cast<location_t*>(data));
} else if (type.is(LOCATION_EXPR)) {
visitor.visitLocation(*static_cast<location_t*>(data));
} else if (type.is(FUNCTION)) {
visitor.visitFunction(*static_cast<function_t*>(data));
} else if (type.is(FUNCTION_EXTERNAL)) {
// we cannot look inside a external function, skip.
} else if (type.is(INSTANCE_LINE)) {
visitor.visitInstanceLine(*static_cast<instance_line_t*>(data));