Skip to content

Commit

Permalink
Ignore protected access in instance API (OpenModelica#12608)
Browse files Browse the repository at this point in the history
  • Loading branch information
perost committed Jun 18, 2024
1 parent 6c0894d commit a195f47
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 6 deletions.
10 changes: 5 additions & 5 deletions OMCompiler/Compiler/NFFrontEnd/NFLookup.mo
Original file line number Diff line number Diff line change
Expand Up @@ -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
();
Expand All @@ -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
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion OMCompiler/Compiler/NFFrontEnd/NFLookupState.mo
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
154 changes: 154 additions & 0 deletions testsuite/openmodelica/instance-API/GetModelInstanceProtected1.mos
Original file line number Diff line number Diff line change
@@ -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\": \"<interactive>\",
// \"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\": \"<interactive>\",
// \"lineStart\": 2,
// \"columnStart\": 3,
// \"lineEnd\": 5,
// \"columnEnd\": 8
// }
// },
// \"prefixes\": {
// \"public\": false
// }
// }
// ],
// \"source\": {
// \"filename\": \"<interactive>\",
// \"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\": \"<interactive>\",
// \"lineStart\": 12,
// \"columnStart\": 3,
// \"lineEnd\": 16,
// \"columnEnd\": 8
// }
// }"
// ""
// endResult
1 change: 1 addition & 0 deletions testsuite/openmodelica/instance-API/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ GetModelInstanceMod2.mos \
GetModelInstanceMod3.mos \
GetModelInstanceMod4.mos \
GetModelInstanceMod5.mos \
GetModelInstanceProtected1.mos \
GetModelInstanceReplaceable1.mos \
GetModelInstanceReplaceable2.mos \
GetModelInstanceReplaceable3.mos \
Expand Down

0 comments on commit a195f47

Please sign in to comment.