summaryrefslogtreecommitdiffstats
path: root/src/uscxml/transform/promela/PromelaCodeAnalyzer.h
blob: 7f69ac97082f9efb2e02c6bf5ab2213375a8bbd0 (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
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
/**
 *  @file
 *  @author     2016 Stefan Radomski (stefan.radomski@cs.tu-darmstadt.de)
 *  @copyright  Simplified BSD
 *
 *  @cond
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the FreeBSD license as published by the FreeBSD
 *  project.
 *
 *  This program 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.
 *
 *  You should have received a copy of the FreeBSD license along with this
 *  program. If not, see <http://www.opensource.org/licenses/bsd-license>.
 *  @endcond
 */

#ifndef PROMELACODEANALYZER_H_E89FF519
#define PROMELACODEANALYZER_H_E89FF519

#include "uscxml/transform/Trie.h"
#include "uscxml/plugins/datamodel/promela/PromelaParser.h"
#include "uscxml/plugins/datamodel/promela/parser/promela.tab.hpp"

#include <set>

namespace uscxml {

class ChartToPromela;

class USCXML_API PromelaCodeAnalyzer {
public:
	class PromelaTypedef {
	public:
		PromelaTypedef() {}
		std::string name;
		std::string type;
		size_t arraySize = 0;
		size_t minValue = 0;
		size_t maxValue = 0;
		std::map<std::string, PromelaTypedef> types;
		std::set<ChartToPromela*> occurrences;

		bool operator==(const PromelaTypedef& other) const {
			return name == other.name;
		}

	};

	PromelaCodeAnalyzer() : _eventTrie(".") {}

	void analyze(ChartToPromela* interpreter);

	void addCode(const std::string& code, ChartToPromela* interpreter);
	void addLiteral(const std::string& stateName, int forceIndex = -1);

	bool usesComplexEventStruct() {
		return _typeDefs.types.find("_event") != _typeDefs.types.end() && _typeDefs.types["_event"].types.size() > 0;
	}
	bool usesEventField(const std::string& fieldName) {
		if (usesComplexEventStruct() && _typeDefs.types["_event"].types.find(fieldName) != _typeDefs.types["_event"].types.end())
			return true;
		return false;
	}
	bool usesCancel(const std::string& elementName) {
		return _usesCancel;
	}

	bool usesEventDataField(const std::string& fieldName) {
		if (usesComplexEventStruct() &&
		        _typeDefs.types["_event"].types.find("data") != _typeDefs.types["_event"].types.end() &&
		        _typeDefs.types["_event"].types["data"].types.find(fieldName) != _typeDefs.types["_event"].types["data"].types.end())
			return true;
		return false;
	}

	size_t largestDelay = 0;

	std::string getTypeAssignment(const std::string& varTo, const std::string& varFrom, const PromelaTypedef& type, size_t indent = 0);
	std::string getTypeReset(const std::string& var, const PromelaTypedef& type, size_t indent = 0);

	bool usesInPredicate() {
		return _usesInPredicate;
	}
	void usesInPredicate(bool value) {
		_usesInPredicate = value;
	}
	bool usesPlatformVars() {
		return _usesPlatformVars;
	}

	bool hasIndexLessLoops() {
		return _hasIndexLessLoops;
	}

	std::string macroForLiteral(const std::string& literal);
	int indexForLiteral(const std::string& literal);

	std::set<std::string> getLiterals() {
		return _literals;
	}
	std::set<std::string> getEventsWithPrefix(const std::string& prefix);

	Trie& getTrie() {
		return _eventTrie;
	}

	std::string adaptCode(const std::string& code, const std::string& prefix);

	static std::string prefixIdentifiers(const std::string& expr, const std::string& prefix);
	static std::list<std::pair<size_t, size_t> > getTokenPositions(const std::string& expr, int type, PromelaParserNode* ast);

	PromelaTypedef& getTypes() {
		return _typeDefs;
	}

	PromelaTypedef& getType(const std::string& typeName) {
		return _typeDefs.types.at(typeName);
	}

	std::string sanitizeCode(const std::string& code);
	void addEvent(const std::string& eventName);
	std::string createMacroName(const std::string& literal);

protected:
	void addState(const std::string& stateName, size_t index);

	int enumerateLiteral(const std::string& literal, int forceIndex = -1);

	std::map<std::string, std::string> _strMacros;  // macronames for string literals
	std::map<std::string, int> _strIndex;               // integer enumeration for string
	std::set<std::string> _literals;

	PromelaTypedef _typeDefs;
	Trie _eventTrie;

private:
	std::set<std::string> _macroNameSet; // helper set for uniqueness of macros
	int _lastStrIndex = 1;
	bool _usesCancel = false;
	bool _usesInPredicate = false;
	bool _usesPlatformVars = false;
	bool _hasIndexLessLoops = false;
};



}

#endif /* end of include guard: PROMELACODEANALYZER_H_E89FF519 */