Browse Source

Protected protection improvements

Brian Fiete 4 years ago
parent
commit
8852e7e194

+ 1 - 1
BeefLibs/corlib/src/Threading/Tasks/Future.bf

@@ -33,7 +33,7 @@ namespace System.Threading.Tasks
 		}
 
 		public this(delegate TResult(Object) func, Object state, CancellationToken cancellationToken, TaskCreationOptions creationOptions)
-		    : this(func, state, Task.[Friend]InternalCurrentIfAttached(creationOptions), cancellationToken,
+		    : this(func, state, Task.InternalCurrentIfAttached(creationOptions), cancellationToken,
 		            creationOptions, InternalTaskOptions.None, null)
 		{
 		    //StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;

+ 14 - 14
BeefLibs/corlib/src/Type.bf

@@ -752,7 +752,7 @@ namespace System.Reflection
         {
             get
             {
-                return (TypeInstance)Type.[Friend]GetType(mBaseType);
+                return (TypeInstance)Type.GetType(mBaseType);
             }
         }
 
@@ -768,7 +768,7 @@ namespace System.Reflection
 		{
 		    get
 		    {
-		        return (TypeInstance)Type.[Friend]GetType(mOuterType);
+		        return (TypeInstance)Type.GetType(mOuterType);
 		    }
 		}
 
@@ -776,7 +776,7 @@ namespace System.Reflection
 		{
 		    get
 		    {
-		        return Type.[Friend]GetType(mUnderlyingType);
+		        return Type.GetType(mUnderlyingType);
 		    }
 		}
 
@@ -803,7 +803,7 @@ namespace System.Reflection
 		            return true;
 		        if (curType.mBaseType == 0)
 		            return false;
-		        curType = (TypeInstance)Type.[Friend]GetType(curType.mBaseType);
+		        curType = (TypeInstance)Type.GetType(curType.mBaseType);
 		    }
 		}
 
@@ -921,7 +921,7 @@ namespace System.Reflection
 		{
 			get
 			{
-				return Type.[Friend]GetType(mElementType);
+				return Type.GetType(mElementType);
 			}
 		}
 
@@ -951,7 +951,7 @@ namespace System.Reflection
 		{
 			get
 			{
-				return Type.[Friend]GetType(mElementType);
+				return Type.GetType(mElementType);
 			}
 		}
 
@@ -978,7 +978,7 @@ namespace System.Reflection
 		{
 			get
 			{
-				return Type.[Friend]GetType(mElementType);
+				return Type.GetType(mElementType);
 			}
 		}
 
@@ -1015,14 +1015,14 @@ namespace System.Reflection
     [Ordered, AlwaysInclude(AssumeInstantiated=true)]
     class SpecializedGenericType : TypeInstance
     {
-        TypeId mUnspecializedType;
-        TypeId* mResolvedTypeRefs;
+        protected TypeId mUnspecializedType;
+        protected TypeId* mResolvedTypeRefs;
 
 		public Type UnspecializedType
 		{
 			get
 			{
-				return Type.[Friend]GetType(mUnspecializedType);
+				return Type.GetType(mUnspecializedType);
 			}
 		}
 
@@ -1030,7 +1030,7 @@ namespace System.Reflection
 		{
 			get
 			{
-				var unspecializedTypeG = Type.[Friend]GetType(mUnspecializedType);
+				var unspecializedTypeG = Type.GetType(mUnspecializedType);
 				var unspecializedType = (UnspecializedGenericType)unspecializedTypeG;
 				return unspecializedType.[Friend]mGenericParamCount;
 			}
@@ -1043,7 +1043,7 @@ namespace System.Reflection
 
 		public override void GetFullName(String strBuffer)
 		{
-			var unspecializedTypeG = Type.[Friend]GetType(mUnspecializedType);
+			var unspecializedTypeG = Type.GetType(mUnspecializedType);
 			var unspecializedType = (UnspecializedGenericType)unspecializedTypeG;
 			base.GetFullName(strBuffer);
 
@@ -1059,7 +1059,7 @@ namespace System.Reflection
 				{
 					if (i > 0)
 						strBuffer.Append(", ");
-					Type.[Friend]GetType(mResolvedTypeRefs[i]).GetFullName(strBuffer);
+					Type.GetType(mResolvedTypeRefs[i]).GetFullName(strBuffer);
 				}
 				strBuffer.Append('>');
 			}
@@ -1075,7 +1075,7 @@ namespace System.Reflection
 
 		public override void GetFullName(String strBuffer)
 		{
-			Type.[Friend]GetType(mResolvedTypeRefs[0]).GetFullName(strBuffer);
+			Type.GetType(mResolvedTypeRefs[0]).GetFullName(strBuffer);
 			strBuffer.Append('[');
 			for (int commaNum < mRank - 1)
 				strBuffer.Append(',');

+ 16 - 0
IDE/Tests/CompileFail001/src/Protection.bf

@@ -49,6 +49,16 @@ namespace Tests
 			{
 				mAPriv
 			}
+
+			private static void PrivA2()
+			{
+
+			}
+
+			protected static void ProtA2()
+			{
+
+			}
 		}
 
 		class ClassB : ClassA
@@ -70,7 +80,11 @@ namespace Tests
 			{
 			    var ca = new ClassA(); //FAIL
 			    base.PrivA(); //FAIL
+				base.ProtA();
 				ca.GetPriv!();
+
+				ClassA.PrivA2(); //FAIL
+				ClassA.ProtA2();
 			}
 
 			protected void ProtB()
@@ -103,6 +117,8 @@ namespace Tests
 				ca.mAProt = 1; //FAIL
                 mAPriv = 1; //FAIL
 				mAProt = 1;
+				base.mAPriv = 1; //FAIL
+				base.mAProt = 1;
 			}
 		}
 

+ 30 - 8
IDEHelper/Compiler/BfExprEvaluator.cpp

@@ -2129,7 +2129,20 @@ bool BfMethodMatcher::CheckType(BfTypeInstance* typeInstance, BfTypedValue targe
 			if (curTypeDef->mMethodSet.TryGetWith(mMethodName, &entry))
 				nextMethodDef = (BfMethodDef*)entry->mMemberDef;
 		}
+
 		BfProtectionCheckFlags protectionCheckFlags = BfProtectionCheckFlag_None;
+		if (target)
+		{
+			if (mBypassVirtual)
+			{
+				// Not an "instance lookup"
+			}
+			else
+			{
+				protectionCheckFlags = (BfProtectionCheckFlags)(protectionCheckFlags | BfProtectionCheckFlag_InstanceLookup);
+			}
+		}
+
 		while (nextMethodDef != NULL)
 		{
 			bool allowExplicitInterface = curTypeInst->IsInterface() && mBypassVirtual;
@@ -2199,9 +2212,9 @@ bool BfMethodMatcher::CheckType(BfTypeInstance* typeInstance, BfTypedValue targe
 				if ((!curTypeInst->IsTypeMemberIncluded(checkMethod->mDeclaringType, activeTypeDef, mModule)) ||
 					(!curTypeInst->IsTypeMemberAccessible(checkMethod->mDeclaringType, visibleProjectSet)))
 					continue;
-			}
+			}			
 
-			MatchFailKind matchFailKind = MatchFailKind_None;			
+			MatchFailKind matchFailKind = MatchFailKind_None;
 			if (!mModule->CheckProtection(protectionCheckFlags, curTypeInst, checkMethod->mDeclaringType->mProject, checkMethod->mProtection, typeInstance))
 			{
 				if ((mBypassVirtual) && (checkMethod->mProtection == BfProtection_Protected) && (mModule->TypeIsSubTypeOf(mModule->mCurTypeInstance, typeInstance)))
@@ -3933,6 +3946,18 @@ BfTypedValue BfExprEvaluator::LookupField(BfAstNode* targetSrc, BfTypedValue tar
 			}
 
 			BfProtectionCheckFlags protectionCheckFlags = BfProtectionCheckFlag_None;
+			if (target)
+			{
+				if ((flags & (BfLookupFieldFlag_IsImplicitThis | BfLookupFieldFlag_BaseLookup)) != 0)
+				{
+					// Not an "instance lookup"
+				}
+				else
+				{
+					protectionCheckFlags = (BfProtectionCheckFlags)(protectionCheckFlags | BfProtectionCheckFlag_InstanceLookup);
+				}
+			}
+
 			BfFieldDef* matchedField = NULL;
 			while (nextField != NULL)
 			{
@@ -8607,7 +8632,7 @@ void BfExprEvaluator::LookupQualifiedName(BfAstNode* nameNode, BfIdentifierNode*
 	}
 
 	if (mPropDef == NULL)
-		mResult = LookupField(nameRight, lookupVal, fieldName);
+		mResult = LookupField(nameRight, lookupVal, fieldName, CheckIsBase(nameLeft) ? BfLookupFieldFlag_BaseLookup : BfLookupFieldFlag_None);
 
 	if ((!mResult) && (mPropDef == NULL) && (lookupType->IsGenericParam()))
 	{
@@ -17888,12 +17913,9 @@ void BfExprEvaluator::Visit(BfIndexerExpression* indexerExpr)
 	// Try first as a non-static indexer, then as a static indexer
 	for (int pass = 0; pass < 2; pass++)
 	{
-// 		SetAndRestoreValue<bool> prevIgnoreErrors(mModule->mIgnoreErrors, (mModule->mIgnoreErrors) || (pass == 0));
-// 		SetAndRestoreValue<bool> prevHadIgnoredError(mModule->mHadIgnoredError, false);
-
-
+		///
 		{
-			SetAndRestoreValue<BfEvalExprFlags> prevFlags(mBfEvalExprFlags, (BfEvalExprFlags)(mBfEvalExprFlags | BfEvalExprFlags_NoLookupError));
+			SetAndRestoreValue<BfEvalExprFlags> prevFlags(mBfEvalExprFlags, (BfEvalExprFlags)(mBfEvalExprFlags | BfEvalExprFlags_NoLookupError), pass == 0);
 			VisitChild(indexerExpr->mTarget);
 		}
 		ResolveGenericType();

+ 4 - 3
IDEHelper/Compiler/BfExprEvaluator.h

@@ -319,9 +319,10 @@ enum BfLookupFieldFlags
 {
 	BfLookupFieldFlag_None = 0,
 	BfLookupFieldFlag_IsImplicitThis = 1,
-	BfLookupFieldFlag_CheckingOuter = 2,
-	BfLookupFieldFlag_IgnoreProtection = 4,
-	BfLookupFieldFlag_BindOnly = 8
+	BfLookupFieldFlag_BaseLookup = 2,
+	BfLookupFieldFlag_CheckingOuter = 4,
+	BfLookupFieldFlag_IgnoreProtection = 8,
+	BfLookupFieldFlag_BindOnly = 0x10
 };
 
 enum BfUnaryOpFlags

+ 28 - 10
IDEHelper/Compiler/BfModule.cpp

@@ -2584,19 +2584,37 @@ bool BfModule::CheckProtection(BfProtectionCheckFlags& flags, BfTypeInstance* me
 				curCheckType = mixinOwner;
 			}
 
-			auto lookupCheckType = lookupStartType;
-			while (lookupCheckType->mInheritDepth >= curCheckType->mInheritDepth)
+			if ((flags & BfProtectionCheckFlag_InstanceLookup) != 0)
 			{
-				if (lookupCheckType == curCheckType)
+				auto lookupCheckType = lookupStartType;
+				while (lookupCheckType->mInheritDepth >= curCheckType->mInheritDepth)
 				{
-					allowProtected = true;
-					break;
+					if (lookupCheckType == curCheckType)
+					{
+						allowProtected = true;
+						break;
+					}
+					if (lookupCheckType == memberOwner)
+						break;
+					lookupCheckType = lookupCheckType->mBaseType;
+					if (lookupCheckType == NULL)
+						break;
+				}
+			}
+			else
+			{
+				auto lookupCheckType = curCheckType;
+				while (lookupCheckType->mInheritDepth >= memberOwner->mInheritDepth)
+				{
+					if (lookupCheckType == memberOwner)
+					{
+						allowProtected = true;
+						break;
+					}
+					lookupCheckType = lookupCheckType->mBaseType;
+					if (lookupCheckType == NULL)
+						break;
 				}
-				if (lookupCheckType == memberOwner)
-					break;
-				lookupCheckType = lookupCheckType->mBaseType;
-				if (lookupCheckType == NULL)
-					break;
 			}
 
 			if (!allowProtected)

+ 1 - 0
IDEHelper/Compiler/BfModule.h

@@ -112,6 +112,7 @@ enum BfProtectionCheckFlags
 	BfProtectionCheckFlag_CheckedPrivate = 2,
 	BfProtectionCheckFlag_AllowProtected = 4,
 	BfProtectionCheckFlag_AllowPrivate = 8,
+	BfProtectionCheckFlag_InstanceLookup = 0x10
 };
 
 enum BfEmbeddedStatementFlags