MicroModelicaCCompiler  4.5.3
helpers.hpp
Go to the documentation of this file.
1 /*****************************************************************************
2 
3  This file is part of QSS Solver.
4 
5  QSS Solver is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  QSS Solver is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with QSS Solver. If not, see <http://www.gnu.org/licenses/>.
17 
18  ******************************************************************************/
19 
20 #pragma once
21 
22 #include <string>
23 
24 #include <ir/annotation.hpp>
25 #include <ir/index.hpp>
26 #include <ir/equation.hpp>
27 #include <ir/expression.hpp>
28 #include <util/symbol_table.hpp>
29 #include <util/util.hpp>
30 
31 namespace MicroModelica {
32 namespace IR {
33 
38 class ExternalFunction {
39  public:
40  ExternalFunction(){};
41  ExternalFunction(std::string lvalue, std::string name, AST_ExpressionList args);
42  ~ExternalFunction() = default;
43  friend std::ostream& operator<<(std::ostream& out, const ExternalFunction& e);
44 
45  private:
46  std::string _lvalue;
47  std::string _name;
48  AST_ExpressionList _args;
49 };
50 
52 
53 class CompiledFunction {
54  public:
56  CompiledFunction(std::string name, std::string includeDir, std::string libraryDir, Util::SymbolTable& libraries,
57  std::string prefix = "__");
58  ~CompiledFunction() = default;
59  inline bool hasIncludeDirectory() const { return !_includeDirectory.empty(); };
60  inline bool hasLibraryDirectory() const { return !_libraryDirectory.empty(); };
61  inline bool hasLibraries() const { return _libraries.size() > 0; };
62  inline std::string includeDirectory() const { return _includeDirectory; };
63  inline std::string libraryDirectory() const { return _libraryDirectory; };
64  inline Util::SymbolTable libraries() { return _libraries; };
65  inline std::string name() const { return _name; };
66  inline std::string prototype() const { return _prototype; };
67  friend std::ostream& operator<<(std::ostream& out, const CompiledFunction& cf);
68  std::string print() const;
69  void setArguments(AST_ExpressionList arguments) { _arguments = arguments; };
70  void setOutputArguments(AST_ExpressionList output_arguments) { _output_arguments = output_arguments; };
71 
72  private:
73  std::string _name;
74  std::string _prefix;
75  std::string _prototype;
76  std::string _includeDirectory;
77  std::string _libraryDirectory;
79  AST_ExpressionList _arguments;
80  AST_ExpressionList _output_arguments;
81 };
82 
84 
86  public:
89  CompiledPackage(string name);
90  ~CompiledPackage(){};
91  string name();
92  string prefix();
99  inline void setDefinitions(CompiledFunctionTable& cft) { _cft = cft; };
100 
101  private:
102  std::string _name;
105 };
106 
108 
109 namespace FUNCTION_PRINTER {
111 }
112 
114  public:
115  ~FunctionPrinter() = default;
116  std::string loop(int end);
117  std::string endLoop();
118  std::string beginSwitch();
119  std::string endSwitch();
120  std::string beginExpression(std::string token, Option<Range> range) const;
121  std::string endExpression(Option<Range> range,
123  std::string jacobianTerms(list<Equation> eqs);
124  std::string getIndexes(string var, Option<Range> range, int offset, bool modelica_index) const;
125  std::map<std::string, std::string> parseIndexes(string var, Option<Range> range, int offset, bool modelica_index = true) const;
126  std::map<int, std::string> parseConstants(Expression ref) const;
127  std::string beginDimGuards(std::string token, string args, Option<Range> range, std::multimap<std::string, int> used_variables) const;
128  std::string endDimGuards(Option<Range> range) const;
129  std::string printAlgebraicGuards(Equation alg, Index usage);
131  std::string accessMacros(std::string token, int offset, Option<Range> range, bool modelica_index = true) const;
132  std::string equationVariableMacros(Option<Range> range, Expression lhs, std::string id) const;
133  std::string jacMacrosAccess(Equation eq, std::string index = "row", std::string tab = TAB) const;
134  std::string outputVariableName(Expression exp, Option<Range> range);
135  std::string algebraic(Equation alg, bool reduction);
136 
137  protected:
138  map<std::string, std::string> _alg_dict;
139 
140  private:
141  std::string mod(std::string var, int dim, Option<Range> range) const;
142  std::string div(std::string var, int dim, Option<Range> range) const;
143 };
144 
145 class Input {
146  public:
147  Input(){};
148  Input(Index idx, Option<Range> range, int id);
149  ~Input(){};
150  std::string print() const;
151  std::string macro() const;
152  friend std::ostream& operator<<(std::ostream& out, const Input& i);
153 
154  private:
155  std::string token() const;
156  Index _idx;
158  int _id;
159 };
160 
162 
163 } // namespace IR
164 } // namespace MicroModelica
MicroModelica::IR::ExternalFunction::~ExternalFunction
~ExternalFunction()=default
MicroModelica::IR::FunctionPrinter
Definition: helpers.hpp:147
MicroModelica::IR::CompiledFunction::name
std::string name() const
Definition: helpers.hpp:99
MicroModelica::IR::CompiledFunction::setOutputArguments
void setOutputArguments(AST_ExpressionList output_arguments)
Definition: helpers.hpp:104
ModelTable< int, ExternalFunction >
MicroModelica::IR::CompiledFunction::_name
std::string _name
Definition: helpers.hpp:104
MicroModelica::IR::Input::_range
Option< Range > _range
Definition: helpers.hpp:191
MicroModelica::IR::CompiledPackage::libraryDirectories
Util::SymbolTable libraryDirectories()
Definition: helpers.cpp:168
MicroModelica::IR::Input::macro
std::string macro() const
Definition: helpers.cpp:494
usage
void usage()
Definition: main.cpp:48
index.hpp
MicroModelica::IR::CompiledFunction::_arguments
AST_ExpressionList _arguments
Definition: helpers.hpp:113
MicroModelica::IR::InputTable
ModelTable< int, Input > InputTable
Definition: helpers.hpp:195
MicroModelica::IR::CompiledFunction::_includeDirectory
std::string _includeDirectory
Definition: helpers.hpp:110
MicroModelica::IR::CompiledPackageTable
ModelTable< std::string, CompiledPackage > CompiledPackageTable
Definition: helpers.hpp:141
MicroModelica::IR::FunctionPrinter::getIndexes
std::string getIndexes(string var, Option< Range > range, int offset, bool modelica_index) const
Definition: helpers.cpp:303
expression.hpp
MicroModelica::IR::CompiledPackage::setDefinitions
void setDefinitions(CompiledFunctionTable &cft)
Definition: helpers.hpp:133
MicroModelica::IR::FunctionPrinter::endExpression
std::string endExpression(Option< Range > range, FUNCTION_PRINTER::ReturnStatementType ret=FUNCTION_PRINTER::ReturnStatementType::Return) const
Definition: helpers.cpp:232
MicroModelica::IR::CompiledFunction::hasLibraryDirectory
bool hasLibraryDirectory() const
Definition: helpers.hpp:94
MicroModelica::IR::FunctionPrinter::endSwitch
std::string endSwitch()
Definition: helpers.cpp:217
MicroModelica::IR::CompiledFunction::libraries
Util::SymbolTable libraries()
Definition: helpers.hpp:98
MicroModelica::IR::CompiledFunction::prototype
std::string prototype() const
Definition: helpers.hpp:100
MicroModelica::IR::FunctionPrinter::algebraic
std::string algebraic(Equation alg, bool reduction)
Definition: helpers.cpp:288
MicroModelica::IR::CompiledFunction::~CompiledFunction
~CompiledFunction()=default
symbol_table.hpp
MicroModelica::IR::CompiledFunction::_libraries
Util::SymbolTable _libraries
Definition: helpers.hpp:112
MicroModelica::IR::CompiledPackage::prefix
string prefix()
Definition: helpers.cpp:150
Option
Definition: util_types.hpp:32
MicroModelica::IR::CompiledFunction::hasIncludeDirectory
bool hasIncludeDirectory() const
Definition: helpers.hpp:93
MicroModelica::IR::FunctionPrinter::jacMacrosAccess
std::string jacMacrosAccess(Equation eq, std::string index="row", std::string tab=TAB) const
Definition: helpers.cpp:455
MicroModelica::IR::ExternalFunction::_args
AST_ExpressionList _args
Definition: helpers.hpp:99
MicroModelica::IR::FunctionPrinter::loop
std::string loop(int end)
Definition: helpers.cpp:196
MicroModelica::IR::FunctionPrinter::endDimGuards
std::string endDimGuards(Option< Range > range) const
Definition: helpers.cpp:256
MicroModelica::IR::FunctionPrinter::~FunctionPrinter
~FunctionPrinter()=default
MicroModelica::IR::CompiledFunction::includeDirectory
std::string includeDirectory() const
Definition: helpers.hpp:96
MicroModelica::IR::CompiledPackage::setObjects
void setObjects(Util::ImportTable &objects)
Definition: helpers.hpp:132
MicroModelica::IR::Input::_id
int _id
Definition: helpers.hpp:192
MicroModelica::IR::FUNCTION_PRINTER::Return
@ Return
Definition: helpers.hpp:144
MicroModelica::IR::CompiledPackage::includeDirectories
Util::SymbolTable includeDirectories()
Definition: helpers.cpp:181
MicroModelica::Util::SymbolTable
ModelTable< std::string, std::string > SymbolTable
Definition: equation.hpp:62
MicroModelica::IR::Expression
Definition: expression.hpp:64
MicroModelica::IR::CompiledFunction::_output_arguments
AST_ExpressionList _output_arguments
Definition: helpers.hpp:114
MicroModelica::IR::ExternalFunction::ExternalFunction
ExternalFunction()
Definition: helpers.hpp:91
MicroModelica::IR::Input::operator<<
friend std::ostream & operator<<(std::ostream &out, const Input &i)
Definition: helpers.cpp:500
MicroModelica::IR::ExternalFunction::_name
std::string _name
Definition: helpers.hpp:98
MicroModelica::IR::CompiledFunction::_prototype
std::string _prototype
Definition: helpers.hpp:109
MicroModelica::IR::ExternalFunctionTable
ModelTable< int, ExternalFunction > ExternalFunctionTable
Definition: helpers.hpp:85
MicroModelica::IR::FunctionPrinter::printAlgebraicGuards
std::string printAlgebraicGuards(Equation alg, Index usage)
Definition: helpers.cpp:357
MicroModelica::IR::CompiledFunction::print
std::string print() const
Definition: helpers.cpp:100
MicroModelica::IR::Equation
Definition: equation.hpp:67
MicroModelica::IR::Input::Input
Input()
Definition: helpers.hpp:181
MicroModelica::IR::Input::~Input
~Input()
Definition: helpers.hpp:183
MicroModelica::IR::CompiledPackage::definitions
CompiledFunctionTable definitions()
Definition: helpers.cpp:152
equation.hpp
MicroModelica::IR::CompiledPackage::objects
Util::ImportTable objects()
Definition: helpers.cpp:154
MicroModelica::IR::FUNCTION_PRINTER::Break
@ Break
Definition: helpers.hpp:144
MicroModelica::IR::CompiledFunction::CompiledFunction
CompiledFunction()
Definition: helpers.cpp:87
MicroModelica::IR::FunctionPrinter::parseIndexes
std::map< std::string, std::string > parseIndexes(string var, Option< Range > range, int offset, bool modelica_index=true) const
Definition: helpers.cpp:330
MicroModelica::IR::CompiledFunctionTable
ModelTable< std::string, CompiledFunction > CompiledFunctionTable
Definition: helpers.hpp:117
TAB
#define TAB
Definition: util.hpp:79
MicroModelica::IR::FunctionPrinter::_alg_dict
map< std::string, std::string > _alg_dict
Definition: helpers.hpp:172
MicroModelica::IR::Input
Definition: helpers.hpp:179
MicroModelica::IR::FunctionPrinter::div
std::string div(std::string var, int dim, Option< Range > range) const
Definition: helpers.cpp:394
MicroModelica::IR::CompiledPackage::name
string name()
Definition: helpers.cpp:148
MicroModelica::IR::FunctionPrinter::beginSwitch
std::string beginSwitch()
Definition: helpers.cpp:210
MicroModelica::IR::ExternalFunction::operator<<
friend std::ostream & operator<<(std::ostream &out, const ExternalFunction &e)
Definition: helpers.cpp:61
MicroModelica::IR::CompiledFunction::hasLibraries
bool hasLibraries() const
Definition: helpers.hpp:95
MicroModelica::IR::Index
Definition: index.hpp:92
MicroModelica::IR::CompiledFunction::_libraryDirectory
std::string _libraryDirectory
Definition: helpers.hpp:111
MicroModelica::IR::FunctionPrinter::outputVariableName
std::string outputVariableName(Expression exp, Option< Range > range)
Definition: helpers.cpp:401
MicroModelica::IR::Input::print
std::string print() const
Definition: helpers.cpp:471
MicroModelica
Definition: files.cpp:45
MicroModelica::IR::Input::token
std::string token() const
Definition: helpers.cpp:487
MicroModelica::IR::FunctionPrinter::parseConstants
std::map< int, std::string > parseConstants(Expression ref) const
Definition: helpers.cpp:315
MicroModelica::IR::CompiledFunction::operator<<
friend std::ostream & operator<<(std::ostream &out, const CompiledFunction &cf)
Definition: helpers.cpp:94
MicroModelica::IR::CompiledFunction::libraryDirectory
std::string libraryDirectory() const
Definition: helpers.hpp:97
MicroModelica::IR::Input::_idx
Index _idx
Definition: helpers.hpp:190
MicroModelica::IR::FunctionPrinter::beginDimGuards
std::string beginDimGuards(std::string token, string args, Option< Range > range, std::multimap< std::string, int > used_variables) const
Definition: helpers.cpp:266
MicroModelica::IR::FunctionPrinter::beginExpression
std::string beginExpression(std::string token, Option< Range > range) const
Definition: helpers.cpp:219
MicroModelica::IR::CompiledPackage::CompiledPackage
CompiledPackage()
Definition: helpers.cpp:142
MicroModelica::IR::CompiledPackage::_name
std::string _name
Definition: helpers.hpp:133
ModelTable::size
const int size() const
Definition: table.hpp:109
MicroModelica::IR::CompiledPackage::_objects
Util::ImportTable _objects
Definition: helpers.hpp:138
MicroModelica::IR::FunctionPrinter::equationVariableMacros
std::string equationVariableMacros(Option< Range > range, Expression lhs, std::string id) const
Definition: helpers.cpp:421
MicroModelica::IR::CompiledFunction::_prefix
std::string _prefix
Definition: helpers.hpp:108
annotation.hpp
MicroModelica::IR::FUNCTION_PRINTER::Continue
@ Continue
Definition: helpers.hpp:144
MicroModelica::IR::FunctionPrinter::mod
std::string mod(std::string var, int dim, Option< Range > range) const
Definition: helpers.cpp:384
MicroModelica::IR::CompiledPackage::~CompiledPackage
~CompiledPackage()
Definition: helpers.hpp:124
MicroModelica::IR::ExternalFunction::_lvalue
std::string _lvalue
Definition: helpers.hpp:97
MicroModelica::IR::FunctionPrinter::endLoop
std::string endLoop()
Definition: helpers.cpp:203
MicroModelica::IR::CompiledFunction::setArguments
void setArguments(AST_ExpressionList arguments)
Definition: helpers.hpp:103
MicroModelica::IR::CompiledPackage::_cft
CompiledFunctionTable _cft
Definition: helpers.hpp:137
util.hpp
MicroModelica::IR::CompiledPackage::linkLibraries
Util::SymbolTable linkLibraries()
Definition: helpers.cpp:156
MicroModelica::IR::FunctionPrinter::accessMacros
std::string accessMacros(std::string token, int offset, Option< Range > range, bool modelica_index=true) const
TODO: Review modelica_index parameter usage.
Definition: helpers.cpp:371
MicroModelica::IR::FunctionPrinter::jacobianTerms
std::string jacobianTerms(list< Equation > eqs)
MicroModelica::IR::FUNCTION_PRINTER::ReturnStatementType
ReturnStatementType
Definition: helpers.hpp:144
MicroModelica::IR::CompiledPackage
Definition: helpers.hpp:119