From a195f47893454ede90a9312b45b78ee414a7cbc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Per=20=C3=96stlund?= Date: Tue, 18 Jun 2024 16:32:42 +0200 Subject: [PATCH] Ignore protected access in instance API (#12608) --- OMCompiler/Compiler/NFFrontEnd/NFLookup.mo | 10 +- .../Compiler/NFFrontEnd/NFLookupState.mo | 3 +- .../GetModelInstanceProtected1.mos | 154 ++++++++++++++++++ testsuite/openmodelica/instance-API/Makefile | 1 + 4 files changed, 162 insertions(+), 6 deletions(-) create mode 100644 testsuite/openmodelica/instance-API/GetModelInstanceProtected1.mos diff --git a/OMCompiler/Compiler/NFFrontEnd/NFLookup.mo b/OMCompiler/Compiler/NFFrontEnd/NFLookup.mo index f1c1cde647f..6a08f78a8c9 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFLookup.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFLookup.mo @@ -713,7 +713,7 @@ algorithm if is_import then state := LookupState.ERROR(LookupState.IMPORT()); else - state := LookupState.next(node, state, checkAccessViolations); + state := LookupState.next(node, state, context, checkAccessViolations); end if; then (); @@ -725,7 +725,7 @@ algorithm if is_import then state := LookupState.ERROR(LookupState.IMPORT()); else - state := LookupState.next(node, state, checkAccessViolations); + state := LookupState.next(node, state, context, checkAccessViolations); (node, state) := lookupLocalName(name.path, node, state, context, checkAccessViolations); end if; then @@ -779,14 +779,14 @@ algorithm case Absyn.Path.IDENT() algorithm node := lookupLocalSimpleName(name.name, node); - state := LookupState.next(node, state); + state := LookupState.next(node, state, context); then (node :: nodes, state); case Absyn.Path.QUALIFIED() algorithm node := lookupLocalSimpleName(name.name, node); - state := LookupState.next(node, state); + state := LookupState.next(node, state, context); then lookupLocalNames(name.path, node, node :: nodes, state, context); @@ -1020,7 +1020,7 @@ algorithm state := LookupState.ERROR(LookupState.NON_ENCAPSULATED()); return; else - state := LookupState.next(n, state); + state := LookupState.next(n, state, context); end if; (foundCref, foundScope, state) := match cref diff --git a/OMCompiler/Compiler/NFFrontEnd/NFLookupState.mo b/OMCompiler/Compiler/NFFrontEnd/NFLookupState.mo index 6cb4afa15d1..5c15ad26a9c 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFLookupState.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFLookupState.mo @@ -419,13 +419,14 @@ uniontype LookupState print a (hopefully relevant) error message and fail." input InstNode node; input LookupState currentState; + input InstContext.Type context; input Boolean checkAccessViolations = true; output LookupState nextState; protected LookupState entry_ty; SCode.Element el; algorithm - if checkAccessViolations then + if checkAccessViolations and not InstContext.inInstanceAPI(context) then // Check that the element is allowed to be accessed given its visibility. checkProtection(node, currentState); // Check that we're allowed to look in the current scope. diff --git a/testsuite/openmodelica/instance-API/GetModelInstanceProtected1.mos b/testsuite/openmodelica/instance-API/GetModelInstanceProtected1.mos new file mode 100644 index 00000000000..86d2dea1e1f --- /dev/null +++ b/testsuite/openmodelica/instance-API/GetModelInstanceProtected1.mos @@ -0,0 +1,154 @@ +// name: GetModelInstanceProtected1 +// keywords: +// status: correct +// cflags: -d=newInst +// +// + +loadString(" + connector C + Real e; + flow Real f; + end C; + + model A + protected + C c1, c2; + end A; + + model M + A a; + equation + connect(a.c1, a.c2); + end M; +"); + +getModelInstance(M, prettyPrint=true); +getErrorString(); + +// Result: +// true +// "{ +// \"name\": \"M\", +// \"restriction\": \"model\", +// \"elements\": [ +// { +// \"$kind\": \"component\", +// \"name\": \"a\", +// \"type\": { +// \"name\": \"A\", +// \"restriction\": \"model\", +// \"elements\": [ +// { +// \"$kind\": \"component\", +// \"name\": \"c1\", +// \"type\": { +// \"name\": \"C\", +// \"restriction\": \"connector\", +// \"elements\": [ +// { +// \"$kind\": \"component\", +// \"name\": \"e\", +// \"type\": \"Real\" +// }, +// { +// \"$kind\": \"component\", +// \"name\": \"f\", +// \"type\": \"Real\", +// \"prefixes\": { +// \"connector\": \"flow\" +// } +// } +// ], +// \"source\": { +// \"filename\": \"\", +// \"lineStart\": 2, +// \"columnStart\": 3, +// \"lineEnd\": 5, +// \"columnEnd\": 8 +// } +// }, +// \"prefixes\": { +// \"public\": false +// } +// }, +// { +// \"$kind\": \"component\", +// \"name\": \"c2\", +// \"type\": { +// \"name\": \"C\", +// \"restriction\": \"connector\", +// \"elements\": [ +// { +// \"$kind\": \"component\", +// \"name\": \"e\", +// \"type\": \"Real\" +// }, +// { +// \"$kind\": \"component\", +// \"name\": \"f\", +// \"type\": \"Real\", +// \"prefixes\": { +// \"connector\": \"flow\" +// } +// } +// ], +// \"source\": { +// \"filename\": \"\", +// \"lineStart\": 2, +// \"columnStart\": 3, +// \"lineEnd\": 5, +// \"columnEnd\": 8 +// } +// }, +// \"prefixes\": { +// \"public\": false +// } +// } +// ], +// \"source\": { +// \"filename\": \"\", +// \"lineStart\": 7, +// \"columnStart\": 3, +// \"lineEnd\": 10, +// \"columnEnd\": 8 +// } +// } +// } +// ], +// \"connections\": [ +// { +// \"lhs\": { +// \"$kind\": \"cref\", +// \"parts\": [ +// { +// \"name\": \"a\" +// }, +// { +// \"name\": \"c1\" +// } +// ] +// }, +// \"rhs\": { +// \"$kind\": \"cref\", +// \"parts\": [ +// { +// \"name\": \"a\" +// }, +// { +// \"name\": \"c2\" +// } +// ] +// } +// } +// ], +// \"source\": { +// \"filename\": \"\", +// \"lineStart\": 12, +// \"columnStart\": 3, +// \"lineEnd\": 16, +// \"columnEnd\": 8 +// } +// }" +// "" +// endResult diff --git a/testsuite/openmodelica/instance-API/Makefile b/testsuite/openmodelica/instance-API/Makefile index 71707de05d5..59d69c94dd4 100644 --- a/testsuite/openmodelica/instance-API/Makefile +++ b/testsuite/openmodelica/instance-API/Makefile @@ -69,6 +69,7 @@ GetModelInstanceMod2.mos \ GetModelInstanceMod3.mos \ GetModelInstanceMod4.mos \ GetModelInstanceMod5.mos \ +GetModelInstanceProtected1.mos \ GetModelInstanceReplaceable1.mos \ GetModelInstanceReplaceable2.mos \ GetModelInstanceReplaceable3.mos \