OMCompiler/Compiler/Template/Unparsing.tpl
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file is part of OpenModelica. | ||
| 3 | * | ||
| 4 | * Copyright (c) 1998-2026, Open Source Modelica Consortium (OSMC), | ||
| 5 | * c/o Linköpings universitet, Department of Computer and Information Science, | ||
| 6 | * SE-58183 Linköping, Sweden. | ||
| 7 | * | ||
| 8 | * All rights reserved. | ||
| 9 | * | ||
| 10 | * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF AGPL VERSION 3 LICENSE OR | ||
| 11 | * THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8. | ||
| 12 | * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES | ||
| 13 | * RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GNU AGPL | ||
| 14 | * VERSION 3, ACCORDING TO RECIPIENTS CHOICE. | ||
| 15 | * | ||
| 16 | * The OpenModelica software and the OSMC (Open Source Modelica Consortium) | ||
| 17 | * Public License (OSMC-PL) are obtained from OSMC, either from the above | ||
| 18 | * address, from the URLs: | ||
| 19 | * http://www.openmodelica.org or | ||
| 20 | * https://github.com/OpenModelica/ or | ||
| 21 | * http://www.ida.liu.se/projects/OpenModelica, | ||
| 22 | * and in the OpenModelica distribution. | ||
| 23 | * | ||
| 24 | * GNU AGPL version 3 is obtained from: | ||
| 25 | * https://www.gnu.org/licenses/licenses.html#GPL | ||
| 26 | * | ||
| 27 | * This program is distributed WITHOUT ANY WARRANTY; without | ||
| 28 | * even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 29 | * FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH | ||
| 30 | * IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL. | ||
| 31 | * | ||
| 32 | * See the full OSMC Public License conditions for more details. | ||
| 33 | * | ||
| 34 | */ | ||
| 35 | |||
| 36 | package Unparsing | ||
| 37 | |||
| 38 | import interface SimCodeTV; | ||
| 39 | |||
| 40 | ✗ | template programExternalHeader(SCode.Program program) | |
| 41 | ::= | ||
| 42 | << | ||
| 43 | /* Automatically generated header for external MetaModelica functions */ | ||
| 44 | #ifdef __cplusplus | ||
| 45 | extern "C" { | ||
| 46 | #endif | ||
| 47 | <%program |> cl => classExternalHeader(cl,"")%> | ||
| 48 | #ifdef __cplusplus | ||
| 49 | } | ||
| 50 | #endif | ||
| 51 | |||
| 52 | >> | ||
| 53 | /* adrpo: leave a newline at the end of file to get rid of the C warnings */ | ||
| 54 | end programExternalHeader; | ||
| 55 | |||
| 56 | ✗ | template programExternalHeaderFromTypes(list<DAE.Type> tys) | |
| 57 | ::= | ||
| 58 | << | ||
| 59 | /* Automatically generated header for bootstrapping MetaModelica */ | ||
| 60 | #ifdef __cplusplus | ||
| 61 | extern "C" { | ||
| 62 | #endif | ||
| 63 | <%tys |> ty as T_METARECORD(__) => | ||
| 64 | let fieldsStr=(ty.fields |> var as TYPES_VAR(__) => '"<%var.name%>"'; separator=",") | ||
| 65 | let omcname='<%stringReplace(stringReplace(AbsynUtil.pathString(path, "$", false, false),"_","__"), "$", "_")%>' | ||
| 66 | let nElts = listLength(ty.fields) | ||
| 67 | /* adrpo 2011-03-14 make MSVC happy, no arrays of 0 size! */ | ||
| 68 | let fieldsDescription = | ||
| 69 | match nElts | ||
| 70 | case "0" then | ||
| 71 | 'ADD_METARECORD_DEFINITIONS const char* <%omcname%>__desc__fields[1] = {"no fields"};' | ||
| 72 | case _ then | ||
| 73 | 'ADD_METARECORD_DEFINITIONS const char* <%omcname%>__desc__fields[<%nElts%>] = {<%fieldsStr%>};' | ||
| 74 | << | ||
| 75 | #ifdef ADD_METARECORD_DEFINITIONS | ||
| 76 | #ifndef <%omcname%>__desc_added | ||
| 77 | #define <%omcname%>__desc_added | ||
| 78 | <%fieldsDescription%> | ||
| 79 | ADD_METARECORD_DEFINITIONS struct record_description <%omcname%>__desc = { | ||
| 80 | "<%omcname%>", | ||
| 81 | "<%AbsynUtil.pathString(path, ".", false, false)%>", | ||
| 82 | <%omcname%>__desc__fields | ||
| 83 | }; | ||
| 84 | #endif | ||
| 85 | #else /* Only use the file as a header */ | ||
| 86 | extern struct record_description <%omcname%>__desc; | ||
| 87 | #endif | ||
| 88 | >> | ||
| 89 | ; separator = "\n" | ||
| 90 | %> | ||
| 91 | #ifdef __cplusplus | ||
| 92 | } | ||
| 93 | #endif | ||
| 94 | |||
| 95 | >> | ||
| 96 | /* adrpo: leave a newline at the end of file to get rid of the C warnings */ | ||
| 97 | end programExternalHeaderFromTypes; | ||
| 98 | |||
| 99 | ✗ | template classExternalHeader(SCode.Element cl, String pack) | |
| 100 | ::= | ||
| 101 | match cl | ||
| 102 | case c as SCode.CLASS(restriction = SCode.R_METARECORD(moved = true, name = Absyn.IDENT(name = name))) then elementExternalHeader(cl, name) | ||
| 103 | case c as SCode.CLASS(classDef=p as SCode.PARTS(__)) then (p.elementLst |> elt => elementExternalHeader(elt,c.name)) | ||
| 104 | end classExternalHeader; | ||
| 105 | |||
| 106 | ✗ | template pathString(Absyn.Path path) | |
| 107 | ::= | ||
| 108 | match path | ||
| 109 | case IDENT(__) then name | ||
| 110 | case QUALIFIED(__) then '<%name%>.<%pathString(path)%>' | ||
| 111 | case FULLYQUALIFIED(__) then pathString(path) | ||
| 112 | end pathString; | ||
| 113 | |||
| 114 | ✗ | template metaHelperBoxStart(Integer numVariables) | |
| 115 | "Helper to determine how mmc_mk_box should be called." | ||
| 116 | ::= | ||
| 117 | match numVariables | ||
| 118 | case 0 | ||
| 119 | case 1 | ||
| 120 | case 2 | ||
| 121 | case 3 | ||
| 122 | case 4 | ||
| 123 | case 5 | ||
| 124 | case 6 | ||
| 125 | case 7 | ||
| 126 | case 8 | ||
| 127 | case 9 then '<%numVariables%>(' | ||
| 128 | else '(<%numVariables%>, ' | ||
| 129 | end metaHelperBoxStart; | ||
| 130 | |||
| 131 | ✗ | template elementExternalHeader(SCode.Element elt, String pack) | |
| 132 | ::= | ||
| 133 | match elt | ||
| 134 | case c as SCode.CLASS(restriction=r as SCode.R_METARECORD(moved = true),classDef=p as SCode.PARTS(__)) | ||
| 135 | then | ||
| 136 | let fields=(p.elementLst |> SCode.COMPONENT(__) => name; separator=",") | ||
| 137 | let fieldsStr=(p.elementLst |> SCode.COMPONENT(__) => '"<%name%>"'; separator=",") | ||
| 138 | let omcname='<%pack%>_<%pathString(r.name)%>_<%stringReplace(c.name,"_","__")%>' | ||
| 139 | let nElts=listLength(p.elementLst) | ||
| 140 | let fullname='<%pack%>__<%stringReplace(c.name,"_","_5f")%>' | ||
| 141 | let ctor=intAdd(3,r.index) | ||
| 142 | /* adrpo 2011-03-14 make MSVC happy, no arrays of 0 size! */ | ||
| 143 | let fieldsDescription = | ||
| 144 | match nElts | ||
| 145 | case "0" then | ||
| 146 | 'ADD_METARECORD_DEFINITIONS const char* <%omcname%>__desc__fields[1] = {"no fields"};' | ||
| 147 | case _ then | ||
| 148 | 'ADD_METARECORD_DEFINITIONS const char* <%omcname%>__desc__fields[<%nElts%>] = {<%fieldsStr%>};' | ||
| 149 | << | ||
| 150 | #ifdef ADD_METARECORD_DEFINITIONS | ||
| 151 | #ifndef <%omcname%>__desc_added | ||
| 152 | #define <%omcname%>__desc_added | ||
| 153 | <%fieldsDescription%> | ||
| 154 | ADD_METARECORD_DEFINITIONS struct record_description <%omcname%>__desc = { | ||
| 155 | "<%omcname%>", | ||
| 156 | "<%pack%>.<%pathString(r.name)%>.<%c.name%>", | ||
| 157 | <%omcname%>__desc__fields | ||
| 158 | }; | ||
| 159 | #endif | ||
| 160 | #else /* Only use the file as a header */ | ||
| 161 | extern struct record_description <%omcname%>__desc; | ||
| 162 | #endif | ||
| 163 | #define <%fullname%>_3dBOX<%nElts%> <%ctor%> | ||
| 164 | <% if p.elementLst then | ||
| 165 | << | ||
| 166 | #define <%fullname%>(<%fields%>) (mmc_mk_box<%metaHelperBoxStart(intAdd(1,listLength(p.elementLst)))%><%ctor%>,&<%omcname%>__desc,<%fields%>))<%\n%> | ||
| 167 | >> | ||
| 168 | else | ||
| 169 | << | ||
| 170 | static const MMC_DEFSTRUCTLIT(<%fullname%>__struct,1,<%ctor%>) {&<%omcname%>__desc}}; | ||
| 171 | static void *<%fullname%> = MMC_REFSTRUCTLIT(<%fullname%>__struct);<%\n%> | ||
| 172 | >> | ||
| 173 | %> | ||
| 174 | >> | ||
| 175 | case SCode.CLASS(__) then classExternalHeader(elt,pack) | ||
| 176 | end elementExternalHeader; | ||
| 177 | |||
| 178 | |||
| 179 | ✗ | template programExternalHeaderJulia(SCode.Program program) | |
| 180 | ::= | ||
| 181 | let &buf1 = buffer "" | ||
| 182 | let &buf2 = buffer "" | ||
| 183 | let res = program |> cl => classExternalHeaderJulia(&buf1,&buf2,cl,"") | ||
| 184 | << | ||
| 185 | /* Automatically generated header for external MetaModelica functions */ | ||
| 186 | #include <julia.h> | ||
| 187 | #include <assert.h> | ||
| 188 | #ifdef __cplusplus | ||
| 189 | extern "C" { | ||
| 190 | #endif | ||
| 191 | #ifdef ADD_METARECORD_DEFINITIONS | ||
| 192 | <%buf1%> | ||
| 193 | void OpenModelica_initAbsynReferences() | ||
| 194 | { | ||
| 195 | /* Note: These values may be garbage collected away? Call this before each file is parsed? */ | ||
| 196 | <%buf2%> | ||
| 197 | } | ||
| 198 | #else | ||
| 199 | void OpenModelica_initAbsynReferences(); | ||
| 200 | #endif | ||
| 201 | <%res%> | ||
| 202 | #ifdef __cplusplus | ||
| 203 | } | ||
| 204 | #endif | ||
| 205 | |||
| 206 | >> | ||
| 207 | /* adrpo: leave a newline at the end of file to get rid of the C warnings */ | ||
| 208 | end programExternalHeaderJulia; | ||
| 209 | |||
| 210 | ✗ | template classExternalHeaderJulia(Text &buf1, Text &buf2, SCode.Element cl, String pack) | |
| 211 | ::= | ||
| 212 | match cl case c as SCode.CLASS(__) then | ||
| 213 | let &buf2 += | ||
| 214 | << | ||
| 215 | jl_eval_string("using <%c.name%>"); | ||
| 216 | jl_module_t* <%c.name%> = (jl_module_t *) jl_eval_string("<%c.name%>"); | ||
| 217 | if (!<%c.name%>) | ||
| 218 | { | ||
| 219 | fprintf(stderr, "module <%c.name%> not loaded, load it via using."); | ||
| 220 | fflush(NULL); | ||
| 221 | } | ||
| 222 | assert(jl_is_module(<%c.name%>));<%\n%> | ||
| 223 | >> | ||
| 224 | classExternalHeaderJuliaWork(&buf1,&buf2,cl,pack) | ||
| 225 | end classExternalHeaderJulia; | ||
| 226 | |||
| 227 | ✗ | template classExternalHeaderJuliaWork(Text &buf1, Text &buf2, SCode.Element cl, String pack) | |
| 228 | ::= | ||
| 229 | match cl case c as SCode.CLASS(classDef=p as SCode.PARTS(__)) then | ||
| 230 | (p.elementLst |> elt => elementExternalHeaderJulia(&buf1,&buf2,elt,c.name)) | ||
| 231 | end classExternalHeaderJuliaWork; | ||
| 232 | |||
| 233 | ✗ | template elementExternalHeaderJulia(Text &buf1, Text &buf2, SCode.Element elt, String pack) | |
| 234 | ::= | ||
| 235 | match elt | ||
| 236 | case c as SCode.CLASS(restriction=r as SCode.R_UNIONTYPE(__),classDef=p as SCode.PARTS(__)) | ||
| 237 | then | ||
| 238 | let omcname='<%pack%>_<%c.name%>' | ||
| 239 | let &buf1 += | ||
| 240 | << | ||
| 241 | jl_value_t *<%omcname%> = NULL; | ||
| 242 | <%\n%> | ||
| 243 | >> | ||
| 244 | let &buf2 += | ||
| 245 | << | ||
| 246 | assert((<%omcname%> = jl_get_global(<%pack%>, jl_symbol("<%c.name%>"))));<%\n%> | ||
| 247 | >> | ||
| 248 | 'extern jl_value_t *<%omcname%>;<%\n%>' | ||
| 249 | case c as SCode.CLASS(restriction=r as SCode.R_METARECORD(moved = true),classDef=p as SCode.PARTS(__)) | ||
| 250 | then | ||
| 251 | let fields1=(p.elementLst |> SCode.COMPONENT(__) => name; separator=",") | ||
| 252 | let fields2=(p.elementLst |> SCode.COMPONENT(__) => ', <%name%>') | ||
| 253 | let fieldsWithType=(p.elementLst |> SCode.COMPONENT(__) => 'jl_value_t *<%name%>'; separator=",") | ||
| 254 | let funcName = '<%pack%>.<%pathString(r.name)%>' | ||
| 255 | let omcname='<%pack%>_<%pathString(r.name)%>_<%stringReplace(c.name,"_","__")%>' | ||
| 256 | let fullname='<%pack%>__<%stringReplace(c.name,"_","_5f")%>' | ||
| 257 | let &buf1 += | ||
| 258 | << | ||
| 259 | jl_function_t *<%omcname%> = NULL; | ||
| 260 | jl_value_t *<%omcname%>_type = NULL; | ||
| 261 | <%\n%> | ||
| 262 | >> | ||
| 263 | let &buf2 += | ||
| 264 | << | ||
| 265 | assert((<%omcname%> = jl_get_function(<%pack%>, "<%c.name%>"))); | ||
| 266 | assert((<%omcname%>_type = jl_get_global(<%pack%>, jl_symbol("<%c.name%>"))));<%\n%> | ||
| 267 | >> | ||
| 268 | << | ||
| 269 | extern jl_function_t *<%omcname%>; | ||
| 270 | extern jl_function_t *<%omcname%>_type; | ||
| 271 | <% match listLength(p.elementLst) | ||
| 272 | case 0 then '#define <%fullname%> jl_call0(<%omcname%>)' | ||
| 273 | case 1 | ||
| 274 | case 2 | ||
| 275 | case 3 then | ||
| 276 | << | ||
| 277 | static inline jl_value_t* <%fullname%>(<%fieldsWithType%>) { | ||
| 278 | return jl_call<%listLength(p.elementLst)%>(<%omcname%><%fields2%>); | ||
| 279 | } | ||
| 280 | >> | ||
| 281 | else | ||
| 282 | << | ||
| 283 | static inline jl_value_t* <%fullname%>(<%fieldsWithType%>) { | ||
| 284 | jl_value_t *values[<%listLength(p.elementLst)%>] = {<%fields1%>}; | ||
| 285 | return jl_call(<%omcname%>, values, <%listLength(p.elementLst)%>); | ||
| 286 | } | ||
| 287 | >> | ||
| 288 | %> | ||
| 289 | <%\n%> | ||
| 290 | >> | ||
| 291 | case SCode.CLASS(__) then classExternalHeaderJuliaWork(&buf1,&buf2,elt,pack) | ||
| 292 | end elementExternalHeaderJulia; | ||
| 293 | |||
| 294 | annotation(__OpenModelica_Interface="backend_tools"); | ||
| 295 | end Unparsing; | ||
| 296 | |||
| 297 | // vim: filetype=susan sw=2 sts=2 | ||
| 298 |