diff --git a/src/engine/.vscode/launch.json b/src/engine/.vscode/launch.json index 17c762f7..68010dee 100644 --- a/src/engine/.vscode/launch.json +++ b/src/engine/.vscode/launch.json @@ -23,6 +23,25 @@ "ignoreFailures": true } ] + }, + + // Advection model + { + "name": "Advection.", + "type": "cppdbg", + "request": "launch", + "program": "/home/joaquin/work/qss-solver/build/advection/advection", + "cwd": "/home/joaquin/work/qss-solver/build/advection/", + "environment": [], + "MIMode": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] } + ] } \ No newline at end of file diff --git a/src/engine/.vscode/settings.json b/src/engine/.vscode/settings.json index 4a1504b0..a5623fce 100644 --- a/src/engine/.vscode/settings.json +++ b/src/engine/.vscode/settings.json @@ -1,7 +1,20 @@ { "files.associations": { "random": "cpp", - "qss_bdf.h": "c" + "qss_bdf.h": "c", + "array": "c", + "compare": "c", + "functional": "c", + "istream": "c", + "ostream": "c", + "ranges": "c", + "tuple": "c", + "type_traits": "c", + "utility": "c", + "typeindex": "c", + "typeinfo": "c", + "qss_data.h": "c" }, - "C_Cpp.dimInactiveRegions": true + "C_Cpp.dimInactiveRegions": true, + "sonarlint.pathToCompileCommands": "${workspaceFolder}/compile_commands.json" } \ No newline at end of file diff --git a/src/mmoc/ir/mmo_ir.cpp b/src/mmoc/ir/mmo_ir.cpp index 0afdd582..32564b2b 100644 --- a/src/mmoc/ir/mmo_ir.cpp +++ b/src/mmoc/ir/mmo_ir.cpp @@ -125,16 +125,17 @@ void MicroModelicaIR::visit(AST_Element x) Variable vi(newType_Integer(), tp, current_element(it)->modification(), nullptr, size, array); _class->insert(current_element(it)->name(), vi, t); } else { + Variable new_var; if ((tp & TP_PARAMETER) && c->isInteger()) { - Variable vi(newType_Integer(), tp, current_element(it)->modification(), nullptr, size, array); - _class->insert(current_element(it)->name(), vi, t); + new_var = Variable(newType_Integer(), tp, current_element(it)->modification(), nullptr, size, array); } else if (c->isString()) { - Variable vi(newType_String(), tp, current_element(it)->modification(), nullptr, size, array); - _class->insert(current_element(it)->name(), vi, t); + new_var = Variable(newType_String(), tp, current_element(it)->modification(), nullptr, size, array); + } else if ((tp & TP_DISCRETE) && c->isInteger()) { + new_var = Variable(newType_Integer(), tp, current_element(it)->modification(), nullptr, size, array); } else { - Variable vi(newType_Real(), tp, current_element(it)->modification(), nullptr, size, array); - _class->insert(current_element(it)->name(), vi, t); + new_var = Variable(newType_Real(), tp, current_element(it)->modification(), nullptr, size, array); } + _class->insert(current_element(it)->name(), new_var, t); } } } else if (e == ELCLASS) { diff --git a/src/mmoc/tests/system/gt_data/allow_discrete_integer/allow_discrete_integer.c b/src/mmoc/tests/system/gt_data/allow_discrete_integer/allow_discrete_integer.c new file mode 100644 index 00000000..9bd85a4e --- /dev/null +++ b/src/mmoc/tests/system/gt_data/allow_discrete_integer/allow_discrete_integer.c @@ -0,0 +1,162 @@ +#include +#include +#include +#include + +#include "allow_discrete_integer.h" +#include +#include +#include +#include +#include + +void MOD_settings(SD_simulationSettings settings) +{ + settings->debug = 0; + settings->parallel = FALSE; + settings->hybrid = TRUE; + settings->method = 0; +} + +void MOD_definition(int idx, double *x, double *d, double *a, double t, double *dx) +{ + int _d1; + int i; + if (_is_var_u(idx)) { + _get_u_idxs(idx); + _apply_usage_eq_1(_d1); + if ((i >= 1 && i <= 1000)) { + _der_u(i,0) = _time; + + + } + return; + } +} + +void MOD_zeroCrossing(int idx, double *x, double *d, double *a, double t, double *zc) +{ + switch(idx) { + case _eval_event_1: { + _zc(0) = _time-(0); + + + return; + } + } +} + +void MOD_handlerPos(int idx, double *x, double* q, double *d, double *a, double t) +{ + switch(idx) { + case _eval_event_1: { + _init_u((int)_d,0) = 0; + return; + } + } +} + +void MOD_handlerNeg(int idx, double *x, double* q, double *d, double *a, double t) +{ +} + +void MOD_output(int idx, double *x, double *d, double *a, double t, double *out) +{ +} + +void MOD_jacobian(double *x, double *d, double *a, double t, SD_jacMatrices dvdx, double *jac) +{ + int row, row_t, eq_var, c_row, c_row_g; + int col, col_g, col_t; + int x_ind; + double aux; + int _d1; + int _rg_d1; + int i; + SD_cleanJacMatrices(dvdx); + for(row = 1; row <= 1000; row++) { + c_row = _c_index(row); + _get_eq_1_var_idxs(row, eq_var); + _get_u_idxs(eq_var); + } + // Assign Jacobian Matrix values for equation: 0 + for (row = 0; row < 1000; row++) { + for (col = 0; col < dvdx->df_dx[0]->size[row]; col++) { + row_t = dvdx->df_dx[0]->index[row][col]; + _assign_jac(row_t, dvdx->df_dx[0]->value[row][col]); + } + } +} + +void MOD_dependencies(int idx, double *x, double *d, double *a, double t, double *dx, int *map) +{ +} + +void MOD_BDF_definition(double *x, double *d, double *a, double t, double *dx, int *BDFMap, int nBDF) +{ + int idx; + int __bdf_it; + for(__bdf_it = 0; __bdf_it < nBDF; __bdf_it++) { + idx = BDFMap[__bdf_it]; + int _d1; + int i; + if (_is_var_u(idx)) { + _get_u_idxs(idx); + _apply_usage_eq_1(_d1); + if ((i >= 1 && i <= 1000)) { + _eval_dep_u(i,1) = _time; + + + } + continue; + } + } +} + +void QSS_initializeDataStructs(QSS_simulator simulator) +{ + simulator->data = QSS_Data(1000,1,1,1000,0,1,0,"allow_discrete_integer"); + QSS_data modelData = simulator->data; + MODEL_DATA_ACCESS(modelData) + int* states = (int*) malloc(1000*sizeof(int)); + int* discretes = (int*) malloc(1*sizeof(int)); + int* events = (int*) malloc(1*sizeof(int)); + int row, eq_var, c_row; + int x_ind; + int _d1; + int _rg_d1; + int i; + for(row = 1; row <= 1000; row++) { + c_row = _c_index(row); + _get_eq_1_var_idxs(row, eq_var); + _get_u_idxs(eq_var); + } + modelData->event[_idx_event_1].nLHSSt++; + QSS_allocDataMatrix(modelData); + cleanVector(states, 0, 1000); + for(row = 1; row <= 1000; row++) { + c_row = _c_index(row); + _get_eq_1_var_idxs(row, eq_var); + _get_u_idxs(eq_var); + } + cleanVector(events, 0, 1); + modelData->event[_idx_event_1].LHSSt[events[_idx_event_1]++] = _idx_u(0,0); + modelData->event[_idx_event_1].direction = 1; + modelData->event[_idx_event_1].relation = 2; + SD_setupJacMatrices(modelData->jac_matrices); + simulator->time = QSS_Time(1000,1,1000,0,ST_Binary, NULL); + for(i = 1; i<=1000; i+=1) { + modelData->IT[_input_1(i)] = _idx_u(i,0); + } + simulator->output = SD_Output("allow_discrete_integer",0,1,1000,NULL,0,0,CI_Step,SD_Memory,NULL); + SD_output modelOutput = simulator->output; + simulator->model = QSS_Model(MOD_definition, MOD_dependencies, MOD_zeroCrossing, MOD_handlerPos, MOD_handlerNeg, MOD_jacobian, MOD_BDF_definition); + free(states); + free(discretes); + free(events); +} + +void CLC_initializeDataStructs(CLC_simulator simulator) +{ +} + diff --git a/src/mmoc/tests/system/gt_data/allow_discrete_integer/allow_discrete_integer.h b/src/mmoc/tests/system/gt_data/allow_discrete_integer/allow_discrete_integer.h new file mode 100644 index 00000000..7dbedb9b --- /dev/null +++ b/src/mmoc/tests/system/gt_data/allow_discrete_integer/allow_discrete_integer.h @@ -0,0 +1,66 @@ +// Model data access macro. + +#define MODEL_DATA_ACCESS(m) \ + double* x = m->x; \ + double* d = m->d; + +// Coeff multipliers definition. + +#define COEFF_MULTIPLIER(c) COEFF_MULTIPLIER_##c +#define COEFF_MULTIPLIER_0 1 +#define COEFF_MULTIPLIER_1 1 + +// Model Variables Macros + +// Macros definition for variable: _event_1 +#define _idx_event_1 0 +#define _eval_event_1 0 + +// Macros definition for variable: d +#define _idx_d 0 +#define _d d[_idx_d] + +// Macros definition for variable: u +#define _idx_u(d1,coeff) ((d1-1)) +#define _state_idx_u(d1,coeff) ((d1-1))*2 + coeff +#define _u(d1,coeff) x[_state_idx_u(d1,coeff)] * COEFF_MULTIPLIER(coeff) +#define _init_u(d1,coeff) x[_state_idx_u(d1,coeff)] +#define _q_u(d1,coeff) q[_state_idx_u(d1,coeff)] * COEFF_MULTIPLIER(coeff) +#define _eval_u(d1,coeff) ((d1-1)) +#define _is_var_u(idx) idx >= 0 && idx < 1000 +#define _get_u_idxs(idx)\ + _d1 = (idx)+ 1; +#define _eval_dep_u(d1,coeff) dx[_state_idx_u(d1,coeff)] + + +// Derivative Equations Macros + +// Macros for equation: 1 +#define _apply_usage_eq_1(_d1) \ + i = _d1; +#define _get_eq_1_var_idxs(row, var)\ + _rg_d1 = 0 + (row-1)+ 1;\ + var = _idx_u(_rg_d1,0); + +// Event Macros + +// Macros for event: 1 + +#define _zc(coeff) zc[coeff] + +// Input Matrix Macros + +#define _input_1(i) ((i-1)) + +// Jacobian Macros definition. +#define _assign_jac(r, val) \ + col_t = dvdx->df_dx_t->size[r] + dvdx->df_dx_t->index[r][0]; \ + dvdx->df_dx_t->index[r][0]++; \ + jac[col_t] = val; +#define _c_index(i) (i-1) + +#define _time t + +// Derivative Macros definition. +// Derivative definition for variable: u +#define _der_u(d1,coeff) dx[coeff+1] diff --git a/src/mmoc/tests/system/gt_data/allow_discrete_integer/allow_discrete_integer.ini b/src/mmoc/tests/system/gt_data/allow_discrete_integer/allow_discrete_integer.ini new file mode 100644 index 00000000..191278e7 --- /dev/null +++ b/src/mmoc/tests/system/gt_data/allow_discrete_integer/allow_discrete_integer.ini @@ -0,0 +1,15 @@ +minstep=1.00000e-14; +zchyst=1.00000e-12; +derdelta=1.00000e-08; +symdiff=1; +lps=0; +nodesize=10000; +jacobian=1; +it=0.00000e+00; +ft=1.00000e+01; +sol="QSS"; +dqmin=(1.00000e-03); +dqrel=(1.00000e-03); +bdf=0; +BDFPartitionDepth=1; +BDFMaxStep=0.00000e+00; diff --git a/src/mmoc/tests/system/gt_data/allow_discrete_integer/allow_discrete_integer.mo b/src/mmoc/tests/system/gt_data/allow_discrete_integer/allow_discrete_integer.mo new file mode 100644 index 00000000..62609124 --- /dev/null +++ b/src/mmoc/tests/system/gt_data/allow_discrete_integer/allow_discrete_integer.mo @@ -0,0 +1,30 @@ +model allow_discrete_integer + constant Integer N=1000; + Real u[N]; + discrete Integer d; + +equation + for i in 1:N loop + der(u[i])=time; + end for; + +algorithm + when time > 0 then + reinit(u[d],0); + end when; + annotation( + experiment( + MMO_Description="Use time variable in initialization code.", + + MMO_Solver=QSS, + MMO_PartitionMethod=Metis, + Jacobian=Dense, + MMO_BDF_PDepth=1, + MMO_BDF_Max_Step=0, + MMO_RandomSeed=0, + StartTime=0, + StopTime=10, + Tolerance={1e-3}, + AbsTolerance={1e-3} + )); +end allow_discrete_integer; diff --git a/src/mmoc/tests/system/models_test.cpp b/src/mmoc/tests/system/models_test.cpp index 84b2f572..d358ba8e 100644 --- a/src/mmoc/tests/system/models_test.cpp +++ b/src/mmoc/tests/system/models_test.cpp @@ -57,13 +57,12 @@ TEST_P(IModelTests, GenerateCode) } const char* models[] = {"adr", "adr2D", "advection", "advection2D", "advection2D_LI", "advection_quoted", - "advectionFlux", "airconds", "aircont", "bball_downstairs", "boost", "BouncingBall", + "advectionFlux", "airconds", "aircont", "allow_discrete_integer", "bball_downstairs", "boost", "BouncingBall", "buck", "buckboost", "buck_circuit", "buck_term", "burgers", "cuk", "cuk2", "interleaved", "inverters", "lc_line", "lotka_volterra", "mliqss_adr", "mliqss_buck", "mliqss_test", "mliqss_TYSON", "NeuralNetwork1", "par_airconds", "par_airconds_cont", "rectifier", "rltest", "rltest_LI", "spikings", "testFor", "test_input", "TYSON", "VIRplanoS", "virus_replication"}; - INSTANTIATE_TEST_SUITE_P(Models, IModelTests, testing::ValuesIn(models)); /// @} diff --git a/src/mmoc/util/ast_util.cpp b/src/mmoc/util/ast_util.cpp index edafd44a..ca1b4c32 100644 --- a/src/mmoc/util/ast_util.cpp +++ b/src/mmoc/util/ast_util.cpp @@ -130,7 +130,7 @@ bool EqualExp::equalTraverseElement(AST_Expression a, AST_Expression b) case EXPCOMPREF: { AST_Expression_ComponentReference compRefA = a->getAsComponentReference(); Option varInfoA = ModelConfig::instance().lookup(CREF_NAME(compRefA)); - if (varInfoA && varInfoA->type()->getType() == TYARRAY) { + if (varInfoA && varInfoA->type()->getType() == SymbolType::TYARRAY) { return compareArrays(compRefA, b->getAsComponentReference()); } else { return CREF_NAME(a).compare(CREF_NAME(b)) == 0; diff --git a/src/mmoc/util/model_config.h b/src/mmoc/util/model_config.h index e9ceb94e..c1054ea6 100644 --- a/src/mmoc/util/model_config.h +++ b/src/mmoc/util/model_config.h @@ -85,6 +85,7 @@ class ModelConfig { inline void clearLocalSymbols() { _local_symbols.clear(); }; inline void setLocalInitSymbols() { _init_symbols = true; }; inline void unsetLocalInitSymbols() { _init_symbols = false; }; + inline void setEvents(IR::EventTable events) { _events = events; } inline IR::EventTable events() const { return _events; } inline bool functionOutputs() const { return _function_outputs; } diff --git a/src/mmoc/util/symbol_table.cpp b/src/mmoc/util/symbol_table.cpp index 775e727d..6ee60f23 100644 --- a/src/mmoc/util/symbol_table.cpp +++ b/src/mmoc/util/symbol_table.cpp @@ -37,7 +37,6 @@ namespace Util { Variable::Variable() : _unknown(false), - _discrete(false), _t(nullptr), _tp(TP_CONSTANT), _m(nullptr), @@ -59,7 +58,6 @@ Variable::Variable() Variable::Variable(Type t, AST_TypePrefix tp, AST_Modification m, AST_Comment c) : _unknown(false), - _discrete(false), _t(t), _tp(tp), _m(m), @@ -80,9 +78,8 @@ Variable::Variable(Type t, AST_TypePrefix tp, AST_Modification m, AST_Comment c) processModification(); } -Variable::Variable(Type t, AST_TypePrefix tp, AST_Modification m, AST_Comment c, vector s, bool array) +Variable::Variable(Type t, AST_TypePrefix tp, AST_Modification m, AST_Comment c, const vector &s, bool array) : _unknown(false), - _discrete(false), _t(t), _tp(tp), _m(m), @@ -105,7 +102,6 @@ Variable::Variable(Type t, AST_TypePrefix tp, AST_Modification m, AST_Comment c, Variable &Variable::operator=(const Variable &other) { - _discrete = other._discrete; _t = other._t; _tp = other._tp; _m = other._m; @@ -178,9 +174,8 @@ void Variable::setName(string name) { _name = name; } unsigned int Variable::size() { - vector::const_iterator it; unsigned int total = 1; - for (it = _size.begin(); it != _size.end(); it++) { + for (vector::const_iterator it = _size.begin(); it != _size.end(); it++) { total *= *it; } return total; @@ -208,6 +203,17 @@ string Variable::print() const return buffer.str(); } +bool Variable::isDiscreteInteger() const { return (_t->getType() == SymbolType::TYINTEGER) && isDiscrete(); } + +std::string Variable::castOperator() const +{ + string cast_operator; + if (isDiscreteInteger()) { + cast_operator = "(int)"; + } + return cast_operator; +} + ostream &operator<<(ostream &out, const Variable &v) { out << v.print(); @@ -234,7 +240,7 @@ string Variable::initialization() { stringstream buffer; if (hasAssignment() || hasStartModifier() || hasEachModifier()) { - Range range = Range(*this); + auto range = Range(*this); Expression ex(exp()); Expression var = Utils::instance().variableExpression(name(), range); if (hasEachModifier()) { @@ -299,7 +305,7 @@ void VarSymbolTable::insert(VarName name, Variable variable) } } -Option VarSymbolTable::lookup(string name) +Option VarSymbolTable::lookup(const string &name) const { std::map table = map(); Option var = table[name]; @@ -309,7 +315,7 @@ Option VarSymbolTable::lookup(string name) return Option(); } -unsigned int VarSymbolTable::maxDim() const { return _max_dims; } +unsigned long VarSymbolTable::maxDim() const { return _max_dims; } } // namespace Util } // namespace MicroModelica diff --git a/src/mmoc/util/symbol_table.h b/src/mmoc/util/symbol_table.h index 2181d945..b6feb5c0 100644 --- a/src/mmoc/util/symbol_table.h +++ b/src/mmoc/util/symbol_table.h @@ -42,7 +42,7 @@ class Variable { public: Variable(); Variable(Type t, AST_TypePrefix tp, AST_Modification m, AST_Comment c); - Variable(Type t, AST_TypePrefix tp, AST_Modification m, AST_Comment c, vector s, bool array); + Variable(Type t, AST_TypePrefix tp, AST_Modification m, AST_Comment c, const vector& s, bool array); Variable& operator=(const Variable& other); bool operator==(const Variable& other); bool operator!=(const Variable& other); @@ -50,7 +50,7 @@ class Variable { typedef enum { State, Algebraic, NotAssigned } RealType; inline void setRealType(RealType type) { _realType = type; }; - inline AST_TypePrefix typePrefix() { return _tp; }; + inline AST_TypePrefix typePrefix() const { return _tp; }; inline AST_Comment comment() { return _comm; }; inline void setComment(AST_Comment c) { _comm = c; }; inline AST_Modification modification() { return _m; }; @@ -67,13 +67,8 @@ class Variable { unsetStartEach(); }; inline bool isParameter() const { return _tp & TP_PARAMETER; }; - inline bool isDiscrete() const { return (_tp & TP_DISCRETE) || _discrete; }; + inline bool isDiscrete() const { return (_tp & TP_DISCRETE); }; inline bool builtIn() const { return _builtin; }; - inline void setDiscrete() - { - _discrete = true; - unsetAssignment(); - }; inline void setBuiltIn() { _builtin = true; }; inline bool isConstant() const { return _tp & TP_CONSTANT; }; inline bool isInput() const { return _tp & TP_INPUT; }; @@ -82,31 +77,31 @@ class Variable { inline bool isEqType() const { return _tp & TP_EQ; }; inline bool isLocal() const { return _tp & TP_LOCAL; }; inline bool isState() const { return _realType == State; }; - inline bool isString() const { return _t->getType() == TYSTRING; }; + inline bool isString() const { return _t->getType() == SymbolType::TYSTRING; }; inline void setState() { unsetAssignment(); }; - inline bool isUnknown() { return _unknown; }; + inline bool isUnknown() const { return _unknown; }; inline void setUnknown() { _unknown = true; }; - inline bool isTime() { return _name.compare("time") == 0; }; + inline bool isTime() const { return _name.compare("time") == 0; }; inline bool isAlgebraic() const { return _realType == Algebraic; }; inline void setValue(int val) { _value = val; }; - inline int value() { return _value; }; + inline int value() const { return _value; }; unsigned int size(); - inline bool hasAssignment() { return _hasAssigment; }; - inline bool hasStartModifier() { return _hasStart; }; - inline bool hasEachModifier() { return _hasEach; }; + inline bool hasAssignment() const { return _hasAssigment; }; + inline bool hasStartModifier() const { return _hasStart; }; + inline bool hasEachModifier() const { return _hasEach; }; inline void setEachModifier(bool each) { _hasEach = each; }; inline string name() const { return _name; }; void setName(string name); inline AST_Expression exp() { return _exp; }; inline bool isArray() const { return _isArray; }; - inline bool isScalar() { return !isArray(); }; + inline bool isScalar() const { return !isArray(); }; friend ostream& operator<<(ostream& os, const Variable& e); inline unsigned int size(int dim) const { return _size[dim]; }; unsigned int rowSize(unsigned int dim) const; - inline unsigned int dimensions() const { return _size.size(); }; + inline unsigned long dimensions() const { return _size.size(); }; std::string declaration(std::string prefix = ""); std::string initialization(); - inline bool hasOffset() { return _hasOffset; }; + inline bool hasOffset() const { return _hasOffset; }; inline void setOffset(int offset) { _offset = offset; @@ -115,9 +110,10 @@ class Variable { inline int offset() const { return _offset; }; inline bool isModelVar() const { return isState() || isDiscrete() || isAlgebraic() || isParameter() || isEqType() || isOutput(); }; std::string print() const; + std::string castOperator() const; friend std::ostream& operator<<(std::ostream& out, const Variable& v); - private: + protected: void processModification(); void unsetAssignment() { _hasAssigment = false; }; inline void unsetStartEach() @@ -125,9 +121,9 @@ class Variable { _hasEach = false; _hasStart = false; }; + bool isDiscreteInteger() const; bool _unknown; - bool _discrete; Type _t; AST_TypePrefix _tp; AST_Modification _m; @@ -157,16 +153,16 @@ class VarSymbolTable : public ModelTable { ~VarSymbolTable() = default; void initialize(TypeSymbolTable tst); void insert(VarName name, Variable variable); - inline bool parameters() { return _parameters; }; - Option lookup(std::string name); - unsigned int maxDim() const; + inline bool parameters() const { return _parameters; }; + Option lookup(const std::string& name) const; + unsigned long maxDim() const; private: bool _parameters; - unsigned int _max_dims; + unsigned long _max_dims; }; -typedef std::list VariableList; +using VariableList = std::list; } // namespace Util } // namespace MicroModelica diff --git a/src/mmoc/util/type.cpp b/src/mmoc/util/type.cpp index 065af537..377215fa 100644 --- a/src/mmoc/util/type.cpp +++ b/src/mmoc/util/type.cpp @@ -21,7 +21,7 @@ #include "type.h" -#include "../ast/ast_builder.h" +#include ostream &operator<<(ostream &os, const Type_ &e) { @@ -35,10 +35,6 @@ ostream &operator<<(ostream &os, const Type &e) return os; } -Type_Real_::Type_Real_() {} - -Type_Real_::~Type_Real_() {} - string Type_Real_::print() const { stringstream ret(stringstream::out); @@ -50,10 +46,6 @@ Type_Real newType_Real() { return new Type_Real_(); } void deleteType_Real(Type_Real m) { delete m; } -Type_Integer_::Type_Integer_() {} - -Type_Integer_::~Type_Integer_() {} - string Type_Integer_::print() const { stringstream ret(stringstream::out); @@ -72,8 +64,6 @@ string Type_Boolean_::print() const return ret.str(); } -Type_String_::~Type_String_() {} - string Type_String_::print() const { stringstream ret(stringstream::out); @@ -91,7 +81,7 @@ string Type_Array_::print() const AST_ExpressionList exls = newAST_ExpressionList(); Type tt = _t; AST_ListPrepend(exls, _dim); - while (tt->getType() == TYARRAY) { + while (tt->getType() == SymbolType::TYARRAY) { AST_ListPrepend(exls, tt->getAsArray()->dimension()); tt = tt->getAsArray()->arrayOf(); } @@ -107,8 +97,6 @@ string Type_Array_::print() const Type_Array_::Type_Array_(Type t, AST_Expression dim) : _t(t), _dim(dim){}; -Type_Array_::~Type_Array_() {} - Type Type_Array_::arrayOf() { return _t; } Type_Array Type_::getAsArray() { return dynamic_cast(this); } @@ -121,9 +109,9 @@ int operator==(Type_ &e1, Type_ &e2) { if (e1.getType() == e2.getType()) { switch (e1.getType()) { - case TYARRAY: + case SymbolType::TYARRAY: return *(e1.getAsArray()->arrayOf()) == e2.getAsArray()->arrayOf(); - case TYTUPLA: { + case SymbolType::TYTUPLA: { Type_Tupla t1 = e1.getAsTupla(), t2 = e2.getAsTupla(); if (t2->tupla()->size() != t1->tupla()->size()) return 0; TypeListIterator it1 = t1->tupla()->begin(), it2 = t1->tupla()->begin(); @@ -133,8 +121,7 @@ int operator==(Type_ &e1, Type_ &e2) } return 1; } - case TYFUNCTION: // No es necesario!! - { + case SymbolType::TYFUNCTION: { Type_Function f1 = e1.getAsFunction(), f2 = e2.getAsFunction(); return *(f1->output()) == f2->output(); } @@ -153,13 +140,12 @@ int operator!=(Type_ &e1, Type e2) { return !(e1 == *e2); } Type_Tupla_::Type_Tupla_(TypeList tyl) : _tyl(tyl){}; -Type_Tupla_::~Type_Tupla_() {} - string Type_Tupla_::print() const { stringstream ret(stringstream::out); TypeListIterator tyit; - int i = 0, s = _tyl->size(); + unsigned long i = 0; + unsigned long s = _tyl->size(); ret << "< "; foreach (tyit, _tyl) { i++; @@ -172,13 +158,12 @@ string Type_Tupla_::print() const Type_Function_::Type_Function_(Type o, TypeList i) : _input(i), _output(o){}; -Type_Function_::~Type_Function_() {} - string Type_Function_::print() const { stringstream ret(stringstream::out); TypeListIterator tyit; - int i = 0, s = _input->size(); + unsigned long i = 0; + unsigned long s = _input->size(); ret << _output << " function "; diff --git a/src/mmoc/util/type.h b/src/mmoc/util/type.h index 00d8df53..3a0a7dff 100644 --- a/src/mmoc/util/type.h +++ b/src/mmoc/util/type.h @@ -17,303 +17,91 @@ ******************************************************************************/ -#ifndef TYPE_H_ -#define TYPE_H_ +#pragma once #include #include #include -#include "../ast/ast_types.h" -#include "../ast/expression.h" -#include "macros.h" +#include +#include +#include -using namespace std; +enum class SymbolType { TYREAL, TYINTEGER, TYBOOLEAN, TYSTRING, TYARRAY, TYTUPLA, TYFUNCTION }; -/** - * - */ -enum TypesType { - TYREAL, //!< TYREAL - TYINTEGER, //!< TYINTEGER - TYBOOLEAN, //!< TYBOOLEAN - TYSTRING, //!< TYSTRING - TYARRAY, //!< TYARRAY - TYTUPLA, //!< TYTUPLA - TYFUNCTION //!< TYFUNCTION -}; - -/** - * - */ DEFINE_TYPE(Type); -/** - * - */ DEFINE_TYPE(Type_Real); -/** - * - */ DEFINE_TYPE(Type_Integer); -/** - * - */ DEFINE_TYPE(Type_Boolean); -/** - * - */ DEFINE_TYPE(Type_String); -/** - * - */ DEFINE_TYPE(Type_Array); -/** - * - */ DEFINE_TYPE(Type_Tupla); -/** - * - */ DEFINE_TYPE(Type_Function); -/** - * - */ DEFINE_LIST(Type); -/** - * - */ class Type_ { public: - /** - * - */ - virtual ~Type_(){}; - /** - * - * @return - */ - virtual TypesType getType() = 0; - /** - * - * @return - */ + virtual ~Type_() = default; + virtual SymbolType getType() = 0; virtual string print() const = 0; - /** - * - * @param os - * @param e - * @return - */ friend ostream &operator<<(ostream &os, const Type_ &e); - /** - * - * @param os - * @param e - * @return - */ friend ostream &operator<<(ostream &os, const Type &e); - /** - * - * @param e1 - * @param e2 - * @return - */ friend int operator==(Type_ &e1, Type_ &e2); - /** - * - * @param e1 - * @param e2 - * @return - */ friend int operator==(Type_ &e1, Type e2); - /** - * - * @param e1 - * @param e2 - * @return - */ friend int operator!=(Type_ &e1, Type_ &e2); - /** - * - * @param e1 - * @param e2 - * @return - */ friend int operator!=(Type_ &e1, Type e2); - /** - * - * @return - */ Type_Array getAsArray(); - /** - * - * @return - */ Type_Tupla getAsTupla(); - /** - * - * @return - */ Type_Function getAsFunction(); }; -/** - * - */ class Type_Real_ : public Type_ { public: - /** - * - */ - Type_Real_(); - /** - * - */ - virtual ~Type_Real_(); - /** - * - * @return - */ - TypesType getType() { return TYREAL; }; - /** - * - * @return - */ - string print() const; + Type_Real_() = default; + ~Type_Real_() override = default; + SymbolType getType() override { return SymbolType::TYREAL; }; + string print() const override; }; -/** - * - * @return - */ + Type_Real newType_Real(); -/** - * - * @param m - */ void deleteType_Real(Type_Real m); -/** - * - */ class Type_Integer_ : public Type_ { public: - /** - * - */ - Type_Integer_(); - /** - * - */ - virtual ~Type_Integer_(); - /** - * - * @return - */ - TypesType getType() { return TYINTEGER; }; - /** - * - * @return - */ - string print() const; + Type_Integer_() = default; + ~Type_Integer_() override = default; + SymbolType getType() override { return SymbolType::TYINTEGER; }; + string print() const override; }; -/** - * - * @return - */ + Type_Integer newType_Integer(); -/** - * - * @param m - */ void deleteType_Integer(Type_Integer m); -/** - * - */ class Type_Boolean_ : public Type_ { public: - /** - * - */ - virtual ~Type_Boolean_(){}; - /** - * - * @return - */ - TypesType getType() { return TYBOOLEAN; }; - /** - * - * @return - */ - string print() const; + ~Type_Boolean_() override = default; + ; + SymbolType getType() override { return SymbolType::TYBOOLEAN; }; + string print() const override; }; -/** - * - */ class Type_String_ : public Type_ { public: - /** - * - */ - virtual ~Type_String_(); - /** - * - * @return - */ - TypesType getType() { return TYSTRING; }; - /** - * - * @return - */ - string print() const; + ~Type_String_() override = default; + SymbolType getType() override { return SymbolType::TYSTRING; }; + string print() const override; }; -/** - * - * @return - */ + Type_String newType_String(); -/** - * - * @param m - */ void deleteType_String(Type_String m); -/** - * - */ class Type_Array_ : public Type_ { public: - /** - * - * @param t - * @param dim - */ Type_Array_(Type t, AST_Expression dim); - /** - * - */ - virtual ~Type_Array_(); - /** - * - * @return - */ - TypesType getType() { return TYARRAY; } - /** - * - * @return - */ - string print() const; - /** - * - * @return - */ + ~Type_Array_() override = default; + SymbolType getType() override { return SymbolType::TYARRAY; } + string print() const override; Type arrayOf(); - /** - * - * @return - */ AST_Expression dimension() { return _dim; }; private: @@ -321,79 +109,28 @@ class Type_Array_ : public Type_ { AST_Expression _dim; }; -/** - * - */ class Type_Tupla_ : public Type_ { public: - /** - * - * @param tyl - */ - Type_Tupla_(TypeList tyl); - /** - * - */ - virtual ~Type_Tupla_(); - /** - * - * @return - */ - string print() const; - /** - * - * @return - */ + explicit Type_Tupla_(TypeList tyl); + ~Type_Tupla_() override = default; + string print() const override; TypeList tupla() { return _tyl; }; - /** - * - * @return - */ - TypesType getType() { return TYTUPLA; } + SymbolType getType() override { return SymbolType::TYTUPLA; } private: TypeList _tyl; }; -/** - * - */ class Type_Function_ : public Type_ { public: - /** - * - * @param output - * @param input - */ Type_Function_(Type output, TypeList input); - /** - * - */ - virtual ~Type_Function_(); - /** - * - * @return - */ - string print() const; - /** - * - * @return - */ + ~Type_Function_() override = default; + string print() const override; TypeList input() { return _input; }; - /** - * - * @return - */ Type output() { return _output; }; - /** - * - * @return - */ - TypesType getType() { return TYFUNCTION; }; + SymbolType getType() override { return SymbolType::TYFUNCTION; }; private: TypeList _input; Type _output; }; - -#endif /* TYPE_H_ */ diff --git a/src/mmoc/util/visitors/expression_printer.cpp b/src/mmoc/util/visitors/expression_printer.cpp index 2e83a19e..7a2f2726 100644 --- a/src/mmoc/util/visitors/expression_printer.cpp +++ b/src/mmoc/util/visitors/expression_printer.cpp @@ -34,7 +34,7 @@ namespace MicroModelica { using namespace IR; namespace Util { -ExpressionPrinter::ExpressionPrinter(int order) : _order(order) {} +ExpressionPrinter::ExpressionPrinter(int order, bool array_index) : _order(order), _array_index(array_index) {} string ExpressionPrinter::foldTraverseElement(AST_Expression exp) { @@ -50,7 +50,7 @@ string ExpressionPrinter::foldTraverseElement(AST_Expression exp) case EXPBRACE: break; case EXPCALL: { - AST_Expression_Call call = exp->getAsCall(); + const AST_Expression_Call call = exp->getAsCall(); CompiledFunctionTable fs = Utils::instance().compiledFunctions(); Option f = fs[*call->name()]; if (!f) { @@ -87,7 +87,7 @@ string ExpressionPrinter::foldTraverseElement(AST_Expression exp) Error::instance().add(exp->lineNum(), EM_IR | EM_VARIABLE_NOT_FOUND, ER_Error, "expression_printer.cpp:80 %s", ref->name().c_str()); break; } - VariablePrinter var_printer(var.get(), ref, _order); + VariablePrinter var_printer(var.get(), ref, _order, _array_index); buffer << var_printer; break; } @@ -110,7 +110,8 @@ string ExpressionPrinter::foldTraverseElement(AST_Expression exp) case EXPOUTPUT: { AST_Expression_Output out = exp->getAsOutput(); AST_ExpressionListIterator it; - int size = out->expressionList()->size(), i = 0; + unsigned long size = out->expressionList()->size(); + unsigned long i = 0; buffer << "("; foreach (it, out->expressionList()) { buffer << apply(current_element(it)); @@ -188,14 +189,22 @@ string ExpressionPrinter::foldTraverseElementUMinus(AST_Expression exp) return buffer.str(); } -VariablePrinter::VariablePrinter(Variable var, AST_Expression_ComponentReference ref, int order) - : _var(var), _ref(ref), _order(order), _exp(), _begin_delimiter("("), _end_delimiter(")"), _begin_index_access(), _end_index_access() +VariablePrinter::VariablePrinter(const Variable& var, AST_Expression_ComponentReference ref, int order, bool array_index) + : _var(var), + _ref(ref), + _order(order), + _exp(), + _begin_delimiter("("), + _end_delimiter(")"), + _begin_index_access(), + _end_index_access(), + _cast() { - config(); + config(array_index); generate(); } -void VariablePrinter::config() +void VariablePrinter::config(bool array_index) { if (ModelConfig::instance().functionCode()) { _begin_delimiter = "["; @@ -203,6 +212,9 @@ void VariablePrinter::config() _begin_index_access = "("; _end_index_access = "-1)"; } + if (array_index) { + _cast = _var.castOperator(); + } } string VariablePrinter::access(bool array_access) const @@ -238,12 +250,14 @@ void VariablePrinter::generate() } else if (config.isQss() && config.algorithm() && !config.reinit() && _var.isState()) { buffer << "_q"; } - buffer << _var; + buffer << _cast << _var; if (HAS_INDEXES) { - ExpressionPrinter printer(_order); + const bool ARRAY_INDEX = true; + ExpressionPrinter printer(_order, ARRAY_INDEX); AST_ExpressionList indexes = _ref->firstIndex(); AST_ExpressionListIterator it; - int size = indexes->size(), i = 0; + unsigned long size = indexes->size(); + unsigned long i = 0; buffer << _begin_delimiter; foreach (it, indexes) { buffer << _begin_index_access << printer.apply(current_element(it)) << _end_index_access << (++i < size ? "," : ""); diff --git a/src/mmoc/util/visitors/expression_printer.h b/src/mmoc/util/visitors/expression_printer.h index 9ad82835..63bb8e07 100644 --- a/src/mmoc/util/visitors/expression_printer.h +++ b/src/mmoc/util/visitors/expression_printer.h @@ -27,27 +27,28 @@ namespace MicroModelica { namespace Util { class ExpressionPrinter : public AST_Expression_Visitor { public: - ExpressionPrinter(int order); - ~ExpressionPrinter() = default; + explicit ExpressionPrinter(int order, bool array_index = false); + ~ExpressionPrinter() override = default; private: - std::string foldTraverseElement(AST_Expression exp); - std::string foldTraverseElement(std::string l, std::string r, BinOpType bot); - std::string foldTraverseElementUMinus(AST_Expression exp); + std::string foldTraverseElement(AST_Expression exp) override; + std::string foldTraverseElement(std::string l, std::string r, BinOpType bot) override; + std::string foldTraverseElementUMinus(AST_Expression exp) override; IR::Expression _exp; int _order; + bool _array_index; }; class VariablePrinter { public: - VariablePrinter(Variable var, AST_Expression_ComponentReference ref, int order); + VariablePrinter(const Variable &var, AST_Expression_ComponentReference ref, int order, bool array_index = false); ~VariablePrinter() = default; friend std::ostream &operator<<(std::ostream &out, const VariablePrinter &var); protected: void generate(); - void config(); + void config(bool array_index); std::string access(bool arrray_access) const; private: @@ -59,6 +60,7 @@ class VariablePrinter { std::string _end_delimiter; std::string _begin_index_access; std::string _end_index_access; + std::string _cast; }; } // namespace Util