From b8693af68bb70ad41da2cb2ba3042d7b3325f7d9 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Thu, 13 Oct 2022 02:20:47 -0400 Subject: [PATCH] Added some permission checks --- analyzer/analyzer.go | 13 ++++++++ analyzer/type-section.go | 30 +++++++++++++++++++ tests/analyzer/typeSection/main.arf | 8 +++-- tests/analyzer/typeSection/something/main.arf | 5 +++- 4 files changed, 53 insertions(+), 3 deletions(-) diff --git a/analyzer/analyzer.go b/analyzer/analyzer.go index 1f9ecd8..1718463 100644 --- a/analyzer/analyzer.go +++ b/analyzer/analyzer.go @@ -249,6 +249,19 @@ func (analyzer *analysisOperation) typeCheck ( return } +// inCurrentModule returns whether or not the specified section resides within +// the current module. +func (analyzer *analysisOperation) inCurrentModule ( + section Section, +) ( + inCurrent bool, +){ + inCurrent = + section.locator().modulePath == + analyzer.currentPosition.modulePath + return +} + // doIndent perfroms a fmt.Sprint operation on input, indenting the string. This // does not add a trailing newline. func doIndent (indent int, input ...any) (output string) { diff --git a/analyzer/type-section.go b/analyzer/type-section.go index 4825250..3a408cc 100644 --- a/analyzer/type-section.go +++ b/analyzer/type-section.go @@ -169,6 +169,8 @@ func (analyzer *analysisOperation) analyzeObjectMembers ( err error, ) { inheritedSection := into.what.actual + inheritsFromSameModule := analyzer.inCurrentModule(inheritedSection) + for index := 0; index < from.MembersLength(); index ++ { inputMember := from.Member(index) @@ -185,6 +187,20 @@ func (analyzer *analysisOperation) analyzeObjectMembers ( // modifying default value/permissions of an // inherited member + canAccessMember := + inheritsFromSameModule || + inheritedMember.permission != + types.PermissionPrivate + + if !canAccessMember { + err = inputMember.NewError ( + "inherited member is private (pv) in " + + "parent type, and cannot be modified " + + "here", + infoerr.ErrorKindError) + return + } + outputMember.what = inheritedMember.what if !inputMember.Type().Nil() { err = inputMember.NewError ( @@ -202,11 +218,25 @@ func (analyzer *analysisOperation) analyzeObjectMembers ( return } + canOverwriteMember := + inheritsFromSameModule || + inheritedMember.permission == + types.PermissionReadWrite + // apply default value if inputMember.Argument().Nil() { // if it is unspecified, inherit it outputMember.argument = inheritedMember.argument } else { + if !canOverwriteMember { + err = inputMember.Argument().NewError ( + "member is read-only (ro) in " + + "parent type, its default " + + "value cannot be overridden", + infoerr.ErrorKindError) + return + } + outputMember.argument, err = analyzer.analyzeArgument(inputMember.Argument()) if err != nil { return } diff --git a/tests/analyzer/typeSection/main.arf b/tests/analyzer/typeSection/main.arf index 7714a49..8255521 100644 --- a/tests/analyzer/typeSection/main.arf +++ b/tests/analyzer/typeSection/main.arf @@ -10,7 +10,11 @@ type ro cBasicObject:Obj ro that:UInt ro this:Int -type ro dInheritedFromOther:something.Thing +type ro dInheritFromOther:something.aBasic type ro eInheritObject:cBasicObject - rw that + ro that 5 + +type ro dInheritObjectFromOther:something.bBird + ro wing 2 + ro beak:Int 238 diff --git a/tests/analyzer/typeSection/something/main.arf b/tests/analyzer/typeSection/something/main.arf index 7440373..e9aed4c 100644 --- a/tests/analyzer/typeSection/something/main.arf +++ b/tests/analyzer/typeSection/something/main.arf @@ -1,3 +1,6 @@ :arf --- -type ro Thing:Int +type ro aBasic:Int + +type ro bBird:Obj + rw wing:Int 2