From 653344bbaefe87af3809cdaa9ddabbab1cb7ca00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Per=20=C3=96stlund?= Date: Thu, 28 Sep 2023 16:38:44 +0200 Subject: [PATCH] Use correct context in getModelInstance (#11279) Fixes #11278 --- OMCompiler/Compiler/Script/NFApi.mo | 6 ++ .../GetModelInstanceFunction1.mos | 58 +++++++++++++++++++ testsuite/openmodelica/instance-API/Makefile | 1 + 3 files changed, 65 insertions(+) create mode 100644 testsuite/openmodelica/instance-API/GetModelInstanceFunction1.mos diff --git a/OMCompiler/Compiler/Script/NFApi.mo b/OMCompiler/Compiler/Script/NFApi.mo index 755c9a18ea2..16c559c6e3e 100644 --- a/OMCompiler/Compiler/Script/NFApi.mo +++ b/OMCompiler/Compiler/Script/NFApi.mo @@ -867,6 +867,12 @@ algorithm (_, top) := mkTop(SymbolTable.getAbsyn(), AbsynUtil.pathString(classPath)); mod := parseModifier(modifier, top); cls_node := Inst.lookupRootClass(classPath, top, context); + + if SCodeUtil.isFunction(InstNode.definition(cls_node)) then + context := InstContext.unset(context, NFInstContext.CLASS); + context := InstContext.set(context, NFInstContext.FUNCTION); + end if; + cls_node := Inst.instantiateRootClass(cls_node, context, mod); execStat("Inst.instantiateRootClass"); inst_tree := buildInstanceTree(cls_node); diff --git a/testsuite/openmodelica/instance-API/GetModelInstanceFunction1.mos b/testsuite/openmodelica/instance-API/GetModelInstanceFunction1.mos new file mode 100644 index 00000000000..af782aa6d26 --- /dev/null +++ b/testsuite/openmodelica/instance-API/GetModelInstanceFunction1.mos @@ -0,0 +1,58 @@ +// name: GetModelInstanceFunction1 +// keywords: +// status: correct +// cflags: -d=newInst +// +// + +loadString(" + function f + input Integer N; + output Real y[N]; + algorithm + y := fill(1.0, N); + end f; +"); + +getModelInstance(f, prettyPrint = true); + +// Result: +// true +// "{ +// \"name\": \"f\", +// \"restriction\": \"function\", +// \"elements\": [ +// { +// \"$kind\": \"component\", +// \"name\": \"N\", +// \"type\": \"Integer\", +// \"prefixes\": { +// \"direction\": \"input\" +// } +// }, +// { +// \"$kind\": \"component\", +// \"name\": \"y\", +// \"type\": \"Real\", +// \"dims\": { +// \"absyn\": [ +// \"N\" +// ], +// \"typed\": [ +// \"N\" +// ] +// }, +// \"prefixes\": { +// \"direction\": \"output\" +// } +// } +// ], +// \"source\": { +// \"filename\": \"\", +// \"lineStart\": 2, +// \"columnStart\": 3, +// \"lineEnd\": 7, +// \"columnEnd\": 8 +// } +// }" +// endResult diff --git a/testsuite/openmodelica/instance-API/Makefile b/testsuite/openmodelica/instance-API/Makefile index a0bf50dd307..078b70a1f6a 100644 --- a/testsuite/openmodelica/instance-API/Makefile +++ b/testsuite/openmodelica/instance-API/Makefile @@ -43,6 +43,7 @@ GetModelInstanceExp2.mos \ GetModelInstanceExtends1.mos \ GetModelInstanceExtends2.mos \ GetModelInstanceExtends3.mos \ +GetModelInstanceFunction1.mos \ GetModelInstanceIcon1.mos \ GetModelInstanceIcon2.mos \ GetModelInstanceIcon3.mos \