1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-08 19:48:20 +02:00

Better enum autocomplete hygiene

This commit is contained in:
Brian Fiete 2022-06-24 06:45:35 -07:00
parent fa1271b662
commit 20c88dfeb0
9 changed files with 109 additions and 37 deletions

View file

@ -425,12 +425,20 @@ namespace System
}
}
[AttributeUsage(.Field | .StaticField | .Method | .Property /*2*/)]
[AttributeUsage(.Field | .StaticField | .Method | .Property | .Types)]
public struct NoShowAttribute : Attribute
{
public this()
{
}
public this(bool allowDirect)
{
}
}
[AttributeUsage(.Parameter)]
public struct HideNameAttribute : Attribute
{

View file

@ -5,11 +5,12 @@ namespace System
{
struct Enum
{
[NoShow(true)]
[Comptime(ConstEval=true)]
public static int GetCount()
public static int GetCount<T>() where T : Enum
{
int count = 0;
for (var field in Compiler.OrigCalleeType.GetFields())
for (var field in typeof(T).GetFields())
{
if (field.IsEnumCase)
count++;
@ -17,13 +18,14 @@ namespace System
return count;
}
[NoShow(true)]
[Comptime(ConstEval=true)]
public static var GetMinValue()
public static var GetMinValue<T>() where T : Enum
{
Compiler.SetReturnType(Compiler.OrigCalleeType);
Compiler.SetReturnType(typeof(T));
int? minValue = null;
for (var field in Compiler.OrigCalleeType.GetFields())
for (var field in typeof(T).GetFields())
{
if (field.IsEnumCase)
{
@ -36,13 +38,14 @@ namespace System
return minValue.ValueOrDefault;
}
[NoShow(true)]
[Comptime(ConstEval=true)]
public static var GetMaxValue()
public static var GetMaxValue<T>() where T : Enum
{
Compiler.SetReturnType(Compiler.OrigCalleeType);
Compiler.SetReturnType(typeof(T));
int? maxValue = null;
for (var field in Compiler.OrigCalleeType.GetFields())
for (var field in typeof(T).GetFields())
{
if (field.IsEnumCase)
{
@ -57,6 +60,7 @@ namespace System
return maxValue.ValueOrDefault;
}
[NoShow(true)]
public static void EnumToString(Type type, String strBuffer, int64 iVal)
{
for (var field in type.GetFields())
@ -71,6 +75,7 @@ namespace System
iVal.ToString(strBuffer);
}
[NoShow(true)]
public static Result<T> Parse<T>(StringView str, bool ignoreCase = false) where T : enum
{
for (var (name, data) in GetEnumerator<T>())
@ -84,7 +89,8 @@ namespace System
return .Err;
}
public static bool IsDefined<T>(T value)
[NoShow(true)]
public static bool IsDefined<T>(T value) where T : Enum
where T : enum
{
for (var data in GetValues<T>())
@ -96,24 +102,28 @@ namespace System
return false;
}
[NoShow(true)]
public static EnumEnumerator<TEnum> GetEnumerator<TEnum>()
where TEnum : enum
{
return .();
}
[NoShow(true)]
public static EnumValuesEnumerator<TEnum> GetValues<TEnum>()
where TEnum : enum
{
return .();
}
[NoShow(true)]
public static EnumNamesEnumerator<TEnum> GetNames<TEnum>()
where TEnum : enum
{
return .();
}
[NoShow(true)]
private struct EnumFieldsEnumerator<TEnum>
where TEnum : enum
{
@ -179,6 +189,7 @@ namespace System
}
}
[NoShow(true)]
public struct EnumEnumerator<TEnum> : EnumFieldsEnumerator<TEnum>, IEnumerator<(StringView name, TEnum value)>
where TEnum : enum
{
@ -198,6 +209,7 @@ namespace System
}
}
[NoShow(true)]
public struct EnumValuesEnumerator<TEnum> : EnumFieldsEnumerator<TEnum>, IEnumerator<TEnum>
where TEnum : enum
{
@ -217,6 +229,7 @@ namespace System
}
}
[NoShow(true)]
public struct EnumNamesEnumerator<TEnum> : EnumFieldsEnumerator<TEnum>, IEnumerator<StringView>
where TEnum : enum
{

View file

@ -2,6 +2,7 @@ namespace System
{
struct ValueType
{
[NoShow(true)]
public static extern bool Equals<T>(T val1, T val2);
}
}

View file

@ -712,13 +712,18 @@ const char* BfAutoComplete::GetTypeName(BfType* type)
return "value";
}
void BfAutoComplete::AddInnerTypes(BfTypeInstance* typeInst, const StringImpl& filter, bool allowProtected, bool allowPrivate)
void BfAutoComplete::AddInnerTypes(BfTypeInstance* typeInst, const StringImpl& filter, BfTypeInstance* startType, bool allowProtected, bool allowPrivate)
{
if (typeInst->IsEnum())
AddEntry(AutoCompleteEntry("valuetype", "UnderlyingType"), filter);
BfShow checkShow = (typeInst == startType) ? BfShow_Hide : BfShow_HideIndirect;
for (auto innerType : typeInst->mTypeDef->mNestedTypes)
{
if (innerType->mShow >= checkShow)
continue;
if (innerType->mOuterType->mTypeCode == BfTypeCode_Extension)
{
if (typeInst->mDefineState < BfTypeDefineState_Defined)
@ -737,7 +742,7 @@ void BfAutoComplete::AddInnerTypes(BfTypeInstance* typeInst, const StringImpl& f
allowPrivate = false;
if (typeInst->mBaseType != NULL)
AddInnerTypes(typeInst->mBaseType, filter, allowProtected, allowPrivate);
AddInnerTypes(typeInst->mBaseType, filter, startType, allowProtected, allowPrivate);
}
void BfAutoComplete::AddCurrentTypes(BfTypeInstance* typeInst, const StringImpl& filter, bool allowProtected, bool allowPrivate, bool onlyAttribute)
@ -900,7 +905,7 @@ void BfAutoComplete::AddTypeMembers(BfTypeInstance* typeInst, bool addStatic, bo
auto activeTypeDef = mModule->GetActiveTypeDef();
if ((addStatic) && (mModule->mCurMethodInstance == NULL) && (typeInst->IsEnum()))
if ((addStatic) && (mModule->mCurMethodInstance == NULL) && (typeInst->IsEnum()) && (allowImplicitThis))
{
AddEntry(AutoCompleteEntry("value", "_"), filter);
}
@ -909,6 +914,8 @@ void BfAutoComplete::AddTypeMembers(BfTypeInstance* typeInst, bool addStatic, bo
mModule->PopulateType(typeInst, BfPopulateType_Data);
BfShow checkShow = (startType == typeInst) ? BfShow_Hide : BfShow_HideIndirect;
BfProtectionCheckFlags protectionCheckFlags = BfProtectionCheckFlag_None;
for (auto& fieldInst : typeInst->mFieldInstances)
{
@ -916,7 +923,7 @@ void BfAutoComplete::AddTypeMembers(BfTypeInstance* typeInst, bool addStatic, bo
if (fieldDef == NULL)
continue;
if (fieldDef->mIsNoShow)
if (fieldDef->mShow >= checkShow)
continue;
if ((CHECK_STATIC(fieldDef->mIsStatic)) &&
@ -934,7 +941,7 @@ void BfAutoComplete::AddTypeMembers(BfTypeInstance* typeInst, bool addStatic, bo
{
if (methodDef->mIsOverride)
continue;
if (methodDef->mIsNoShow)
if (methodDef->mShow >= checkShow)
continue;
if (methodDef->mName.IsEmpty())
continue;
@ -965,7 +972,7 @@ void BfAutoComplete::AddTypeMembers(BfTypeInstance* typeInst, bool addStatic, bo
for (auto propDef : typeInst->mTypeDef->mProperties)
{
if (propDef->mIsNoShow)
if (propDef->mShow >= checkShow)
continue;
if ((!typeInst->IsTypeMemberIncluded(propDef->mDeclaringType, activeTypeDef, mModule)) ||
@ -1016,13 +1023,15 @@ void BfAutoComplete::AddSelfResultTypeMembers(BfTypeInstance* typeInst, BfTypeIn
mModule->PopulateType(typeInst, BfPopulateType_Data);
BfShow checkShow = allowPrivate ? BfShow_Hide : BfShow_HideIndirect;
for (auto& fieldInst : typeInst->mFieldInstances)
{
auto fieldDef = fieldInst.GetFieldDef();
if (fieldDef == NULL)
continue;
if (fieldDef->mIsNoShow)
if (fieldDef->mShow > checkShow)
continue;
if ((fieldDef->mIsStatic) && (CheckProtection(fieldDef->mProtection, fieldDef->mDeclaringType, allowProtected, allowPrivate)))
@ -1042,7 +1051,7 @@ void BfAutoComplete::AddSelfResultTypeMembers(BfTypeInstance* typeInst, BfTypeIn
{
if (methodDef->mIsOverride)
continue;
if (methodDef->mIsNoShow)
if (methodDef->mShow > checkShow)
continue;
if (methodDef->mName.IsEmpty())
continue;
@ -1082,7 +1091,7 @@ void BfAutoComplete::AddSelfResultTypeMembers(BfTypeInstance* typeInst, BfTypeIn
for (auto propDef : typeInst->mTypeDef->mProperties)
{
if (propDef->mIsNoShow)
if (propDef->mShow > checkShow)
continue;
if ((!typeInst->IsTypeMemberIncluded(propDef->mDeclaringType, activeTypeDef, mModule)) ||
@ -1216,11 +1225,13 @@ void BfAutoComplete::AddExtensionMethods(BfTypeInstance* targetType, BfTypeInsta
mModule->PopulateType(extensionContainer, BfPopulateType_Data);
BfShow checkShow = allowPrivate ? BfShow_Hide : BfShow_HideIndirect;
for (auto methodDef : extensionContainer->mTypeDef->mMethods)
{
if (methodDef->mMethodType != BfMethodType_Extension)
continue;
if (methodDef->mIsNoShow)
if (methodDef->mShow >= checkShow)
continue;
if (methodDef->mName.IsEmpty())
continue;
@ -1596,7 +1607,7 @@ void BfAutoComplete::CheckIdentifier(BfAstNode* identifierNode, bool isInExpress
for (auto typeInst : staticSearch->mStaticTypes)
{
AddTypeMembers(typeInst, true, false, filter, typeInst, true, true, false);
AddInnerTypes(typeInst, filter, false, false);
AddInnerTypes(typeInst, filter, typeInst, false, false);
}
}
@ -1977,7 +1988,7 @@ bool BfAutoComplete::CheckMemberReference(BfAstNode* target, BfAstNode* dotToken
bool allowProtected = allowPrivate;
if (isStatic)
AddInnerTypes(typeInst, filter, allowProtected, allowPrivate);
AddInnerTypes(typeInst, filter, typeInst, allowProtected, allowPrivate);
if (!onlyShowTypes)
{
@ -2116,11 +2127,13 @@ bool BfAutoComplete::CheckExplicitInterface(BfTypeInstance* interfaceType, BfAst
auto activeTypeDef = mModule->GetActiveTypeDef();
BfShow checkShow = BfShow_Hide;
for (auto methodDef : interfaceType->mTypeDef->mMethods)
{
if (methodDef->mIsOverride)
continue;
if (methodDef->mIsNoShow)
if (methodDef->mShow >= checkShow)
continue;
if (methodDef->mName.IsEmpty())
continue;
@ -2617,12 +2630,14 @@ void BfAutoComplete::AddOverrides(const StringImpl& filter)
auto activeTypeDef = mModule->GetActiveTypeDef();
BfShow checkShow = BfShow_Hide;
BfTypeInstance* curType = mModule->mCurTypeInstance;
while (curType != NULL)
{
for (auto methodDef : curType->mTypeDef->mMethods)
{
if (methodDef->mIsNoShow)
if (methodDef->mShow >= checkShow)
continue;
bool allowInternalOverride = false;

View file

@ -223,7 +223,7 @@ public:
void AddField(BfTypeInstance* typeInst, BfFieldDef* fieldDef, BfFieldInstance* fieldInstance, const StringImpl& filter);
void AddProp(BfTypeInstance* typeInst, BfPropertyDef* propDef, const StringImpl& filter);
void AddTypeDef(BfTypeDef* typeDef, const StringImpl& filter, bool onlyAttribute = false);
void AddInnerTypes(BfTypeInstance* typeInst, const StringImpl& filter, bool allowProtected, bool allowPrivate);
void AddInnerTypes(BfTypeInstance* typeInst, const StringImpl& filter, BfTypeInstance* startType, bool allowProtected, bool allowPrivate);
void AddCurrentTypes(BfTypeInstance* typeInst, const StringImpl& filter, bool allowProtected, bool allowPrivate, bool onlyAttribute);
void AddTypeMembers(BfTypeInstance* typeInst, bool addStatic, bool addNonStatic, const StringImpl& filter, BfTypeInstance* startType, bool allowInterfaces, bool allowImplicitThis, bool checkOuterType);
void AddSelfResultTypeMembers(BfTypeInstance* typeInst, BfTypeInstance* selfType, const StringImpl& filter, bool allowPrivate);

View file

@ -851,7 +851,18 @@ void BfDefBuilder::ParseAttributes(BfAttributeDirective* attributes, BfMethodDef
methodDef->mHasComptime = true;
}
else if (typeRefName == "NoShow")
methodDef->mIsNoShow = true;
{
methodDef->mShow = BfShow_Hide;
if (!attributes->mArguments.IsEmpty())
{
if (auto literalExpr = BfNodeDynCast<BfLiteralExpression>(attributes->mArguments[0]))
{
if (literalExpr->mValue.mBool)
methodDef->mShow = BfShow_HideIndirect;
}
}
}
else if (typeRefName == "NoDiscard")
methodDef->mIsNoDiscard = true;
else if (typeRefName == "NoSplat")
@ -892,6 +903,19 @@ void BfDefBuilder::ParseAttributes(BfAttributeDirective* attributes, BfTypeDef*
typeDef->mIsAlwaysInclude = true;
else if (typeRefName == "NoDiscard")
typeDef->mIsNoDiscard = true;
else if (typeRefName == "NoShow")
{
typeDef->mShow = BfShow_Hide;
if (!attributes->mArguments.IsEmpty())
{
if (auto literalExpr = BfNodeDynCast<BfLiteralExpression>(attributes->mArguments[0]))
{
if (literalExpr->mValue.mBool)
typeDef->mShow = BfShow_HideIndirect;
}
}
}
}
attributes = attributes->mNextAttribute;

View file

@ -22532,6 +22532,9 @@ void BfModule::GetMethodCustomAttributes(BfMethodInstance* methodInstance)
auto propertyMethodDeclaration = methodDef->GetPropertyMethodDeclaration();
auto typeInstance = methodInstance->GetOwner();
if (typeInstance->IsInstanceOf(mCompiler->mValueTypeTypeDef))
return;
BfTypeState typeState(typeInstance);
SetAndRestoreValue<BfTypeState*> prevTypeState(mContext->mCurTypeState, &typeState);
SetAndRestoreValue<BfTypeInstance*> prevTypeInstance(mCurTypeInstance, typeInstance);

View file

@ -2975,7 +2975,7 @@ void BfSystem::InjectNewRevision(BfTypeDef* typeDef)
BF_ASSERT(typeDef->mTypeCode == nextTypeDef->mTypeCode);
typeDef->mTypeCode = nextTypeDef->mTypeCode;
typeDef->mShow = nextTypeDef->mShow;
typeDef->mIsAlwaysInclude = nextTypeDef->mIsAlwaysInclude;
typeDef->mIsNoDiscard = nextTypeDef->mIsNoDiscard;
typeDef->mIsPartial = nextTypeDef->mIsPartial;
@ -3080,6 +3080,7 @@ void BfSystem::AddToCompositePartial(BfPassInstance* passInstance, BfTypeDef* co
typeDef->mSystem = partialTypeDef->mSystem;
typeDef->mTypeCode = partialTypeDef->mTypeCode;
typeDef->mShow = partialTypeDef->mShow;
typeDef->mIsFunction = partialTypeDef->mIsFunction;
typeDef->mIsDelegate = partialTypeDef->mIsDelegate;
typeDef->mNestDepth = partialTypeDef->mNestDepth;
@ -3500,9 +3501,7 @@ void BfSystem::CopyTypeDef(BfTypeDef* typeDef, BfTypeDef* fromTypeDef)
typeDef->mProtection = fromTypeDef->mProtection;
typeDef->mTypeCode = fromTypeDef->mTypeCode;
typeDef->mTypeCode = fromTypeDef->mTypeCode;
typeDef->mShow = fromTypeDef->mShow;
typeDef->mIsAlwaysInclude = fromTypeDef->mIsAlwaysInclude;
typeDef->mIsNoDiscard = fromTypeDef->mIsNoDiscard;
typeDef->mIsPartial = fromTypeDef->mIsPartial;

View file

@ -536,6 +536,13 @@ enum BfParamKind : uint8
BfParamKind_VarArgs
};
enum BfShow : uint8
{
BfShow_Show,
BfShow_HideIndirect,
BfShow_Hide
};
class BfParameterDef
{
public:
@ -570,7 +577,7 @@ public:
BfProtection mProtection;
uint8 mNamePrefixCount; // Number of @'s
bool mIsStatic;
bool mIsNoShow;
BfShow mShow;
bool mIsReadOnly;
bool mHasMultiDefs;
@ -581,7 +588,7 @@ public:
mProtection = BfProtection_Public;
mNamePrefixCount = 0;
mIsStatic = false;
mIsNoShow = false;
mShow = BfShow_Show;
mIsReadOnly = false;
mHasMultiDefs = false;
}
@ -1134,6 +1141,7 @@ public:
int mNestDepth;
int mDupDetectedRevision; // Error state
BfTypeCode mTypeCode;
BfShow mShow;
bool mIsAlwaysInclude;
bool mIsNoDiscard;
bool mIsPartial;
@ -1171,6 +1179,7 @@ public:
mSystem = NULL;
mProject = NULL;
mTypeCode = BfTypeCode_None;
mShow = BfShow_Show;
mIsAlwaysInclude = false;
mIsNoDiscard = false;
mIsExplicitPartial = false;