﻿id	summary	reporter	owner	description	type	status	priority	milestone	component	version	resolution	keywords	cc
5994	Invalid code generation when accessing array index with size(array,1)	a.d.e.suisse@…	Mahder Alemseged Gebremedhin	"Hello everybody

When accessing an array index inside a function by using the size() function, invalid C-code is generated. this leads to a compile error.

Function code:
{{{
within TestLib;

function modifyArray
    input Real values[:];
    output Real result[size(values, 1)];
  algorithm
    result := values;
    result[size(values, 1)] := 42.0;
end modifyArray;
}}}
Model code:
{{{
model Test
parameter
  Real sample[:] = {1.0, 2.0, 3.0};
  Real modified[:] = TestLib.modifyArray(values = sample);
algorithm
equation

end Test;
}}}
Generated C-code (Test_functions.c):
{{{
...
real_array omc_TestLib_modifyArray(threadData_t *threadData, real_array _values)
{
  real_array _result;
  modelica_integer tmp1;
  modelica_integer tmp2;
  _tailrecursive: OMC_LABEL_UNUSED
  tmp1 = size_of_dimension_base_array(_values, ((modelica_integer) 1));
  alloc_real_array(&(_result), 1, tmp1); // _result has no default value.
  copy_real_array_data(_values, &_result);

  tmp2 = size_of_dimension_base_array(_values, ((modelica_integer) 1));
  real_array_get(_result, 1, tmp3) = 42.0;
  _return: OMC_LABEL_UNUSED
  return _result;
}
...
}}}
As seen in the C-code, the variable ""tmp2"" is used to store the size of the array, but for accessing the array an undeclared variable ""tmp3"" is used.
This leads to following compiler output:
{{{
C:/Program Files/OpenModelica1.14.1-64bit//share/omc/scripts/Compile.bat Test gcc mingw64 parallel 8 0
PATH = ""C:\PROGRA~1\OPENMO~1.1-6\tools\msys\mingw64\bin;C:\PROGRA~1\OPENMO~1.1-6\tools\msys\mingw64\bin\..\..\usr\bin;""
mingw32-make: Entering directory 'C:/Users/patri/AppData/Local/Temp/OPENMO~1/OMEdit/Test'
gcc  -Os -falign-functions -fno-ipa-pure-const -mstackrealign -msse2 -mfpmath=sse     -I""C:/Program Files/OpenModelica1.14.1-64bit//include/omc/c"" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=Test -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=0 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0  -c -o Test.o Test.c
gcc  -Os -falign-functions -fno-ipa-pure-const -mstackrealign -msse2 -mfpmath=sse     -I""C:/Program Files/OpenModelica1.14.1-64bit//include/omc/c"" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=Test -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=0 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0  -c -o Test_functions.o Test_functions.c
gcc  -Os -falign-functions -fno-ipa-pure-const -mstackrealign -msse2 -mfpmath=sse     -I""C:/Program Files/OpenModelica1.14.1-64bit//include/omc/c"" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=Test -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=0 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0  -c -o Test_records.o Test_records.c
gcc  -Os -falign-functions -fno-ipa-pure-const -mstackrealign -msse2 -mfpmath=sse     -I""C:/Program Files/OpenModelica1.14.1-64bit//include/omc/c"" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=Test -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=0 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0  -c -o Test_01exo.o Test_01exo.c
gcc  -Os -falign-functions -fno-ipa-pure-const -mstackrealign -msse2 -mfpmath=sse     -I""C:/Program Files/OpenModelica1.14.1-64bit//include/omc/c"" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=Test -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=0 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0  -c -o Test_02nls.o Test_02nls.c
gcc  -Os -falign-functions -fno-ipa-pure-const -mstackrealign -msse2 -mfpmath=sse     -I""C:/Program Files/OpenModelica1.14.1-64bit//include/omc/c"" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=Test -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=0 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0  -c -o Test_03lsy.o Test_03lsy.c
gcc  -Os -falign-functions -fno-ipa-pure-const -mstackrealign -msse2 -mfpmath=sse     -I""C:/Program Files/OpenModelica1.14.1-64bit//include/omc/c"" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=Test -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=0 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0  -c -o Test_04set.o Test_04set.c
gcc  -Os -falign-functions -fno-ipa-pure-const -mstackrealign -msse2 -mfpmath=sse     -I""C:/Program Files/OpenModelica1.14.1-64bit//include/omc/c"" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -DOMC_MODEL_PREFIX=Test -DOMC_NUM_MIXED_SYSTEMS=0 -DOMC_NUM_LINEAR_SYSTEMS=0 -DOMC_NUM_NONLINEAR_SYSTEMS=0 -DOMC_NDELAY_EXPRESSIONS=0 -DOMC_NVAR_STRING=0  -c -o Test_05evt.o Test_05evt.c
In file included from C:/Program Files/OpenModelica1.14.1-64bit/include/omc/c/util/modelica.h:56:0,
                 from Test_functions.h:4,
                 from Test_functions.c:2:
Test_functions.c: In function 'omc_TestLib_modifyArray':
Test_functions.c:22:30: error: 'tmp3' undeclared (first use in this function)
   real_array_get(_result, 1, tmp3) = 42.0;
                              ^
C:/Program Files/OpenModelica1.14.1-64bit/include/omc/c/util/generic_array.h:64:109: note: in definition of macro 'real_array_get'
 #define real_array_get(src,ndims,...)               (*(modelica_real*)(real_array_element_addr(&src, ndims, __VA_ARGS__)))
                                                                                                             ^
Test_functions.c:22:30: note: each undeclared identifier is reported only once for each function it appears in
   real_array_get(_result, 1, tmp3) = 42.0;
                              ^
C:/Program Files/OpenModelica1.14.1-64bit/include/omc/c/util/generic_array.h:64:109: note: in definition of macro 'real_array_get'
 #define real_array_get(src,ndims,...)               (*(modelica_real*)(real_array_element_addr(&src, ndims, __VA_ARGS__)))
                                                                                                             ^
<builtin>: recipe for target 'Test_functions.o' failed
mingw32-make: *** [Test_functions.o] Error 1
mingw32-make: *** Waiting for unfinished jobs....
mingw32-make: Leaving directory 'C:/Users/patri/AppData/Local/Temp/OPENMO~1/OMEdit/Test'
Compilation process failed. Exited with code 2.
}}}

This behavior was observed in three environments:
* Two different Windows 10 PCs
  * OpenModelica 1.14.1 64 bit
  * One installed by Installer from OpenModelica,org
  * One installed using chocolatey
* Ubuntu 18.04 LTS inside Hyper-V
  * OpenModelica 1.15.0-dev
  * Installed by apt from OpenModelica.org"	defect	closed	high	1.16.0	New Instantiation	v.1.15.0-dev	fixed		
