1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-10 12:32:20 +02:00

Allowed more flexible attribute type lookup, supporting inner types

This commit is contained in:
Brian Fiete 2020-05-18 13:12:18 -07:00
parent 3509d659ea
commit 86d8f78761
5 changed files with 49 additions and 66 deletions

View file

@ -4282,37 +4282,33 @@ void BfCompiler::GetSymbolReferences()
while (attrib != NULL)
{
if (attrib->mAttributeTypeRef != NULL)
{
String attrName = attrib->mAttributeTypeRef->ToString();
BfType* attrType = NULL;
BfAtomComposite nameComposite;
if (mSystem->ParseAtomComposite(attrName + "Attribute", nameComposite))
{
auto attrType = module->ResolveTypeRef(attrib->mAttributeTypeRef, BfPopulateType_Identity, BfResolveTypeRefFlag_Attribute);
BfTypeDef* attrTypeDef = NULL;
if ((attrType != NULL) && (attrType->IsTypeInstance()))
attrTypeDef = attrType->ToTypeInstance()->mTypeDef;
if (attrTypeDef != NULL)
{
BfTypeDef* attrTypeDef = module->FindTypeDefRaw(nameComposite, 0, replaceTypeInst, declaringType, NULL);
if (attrTypeDef != NULL)
{
mResolvePassData->HandleTypeReference(attrib->mAttributeTypeRef, attrTypeDef);
mResolvePassData->HandleTypeReference(attrib->mAttributeTypeRef, attrTypeDef);
attrTypeDef->PopulateMemberSets();
for (auto argExpr : attrib->mArguments)
attrTypeDef->PopulateMemberSets();
for (auto argExpr : attrib->mArguments)
{
if (auto assignExpr = BfNodeDynCast<BfAssignmentExpression>(argExpr))
{
if (auto assignExpr = BfNodeDynCast<BfAssignmentExpression>(argExpr))
auto propName = assignExpr->mLeft->ToString();
BfMemberSetEntry* propDefEntry;
if (attrTypeDef->mPropertySet.TryGetWith(propName, &propDefEntry))
{
auto propName = assignExpr->mLeft->ToString();
BfMemberSetEntry* propDefEntry;
if (attrTypeDef->mPropertySet.TryGetWith(propName, &propDefEntry))
{
mResolvePassData->HandlePropertyReference(assignExpr->mLeft, attrTypeDef, (BfPropertyDef*)propDefEntry->mMemberDef);
}
else if (attrTypeDef->mFieldSet.TryGetWith(propName, &propDefEntry))
{
mResolvePassData->HandleFieldReference(assignExpr->mLeft, attrTypeDef, (BfFieldDef*)propDefEntry->mMemberDef);
}
mResolvePassData->HandlePropertyReference(assignExpr->mLeft, attrTypeDef, (BfPropertyDef*)propDefEntry->mMemberDef);
}
else if (attrTypeDef->mFieldSet.TryGetWith(propName, &propDefEntry))
{
mResolvePassData->HandleFieldReference(assignExpr->mLeft, attrTypeDef, (BfFieldDef*)propDefEntry->mMemberDef);
}
}
}
}
}
}
attrib = attrib->mNextAttribute;

View file

@ -5062,8 +5062,10 @@ BfTypedValue BfExprEvaluator::CreateCall(BfAstNode* targetSrc, const BfTypedValu
}
// Temporarily disable so we don't capture calls in params
SetAndRestoreValue<BfFunctionBindResult*> prevBindResult(mFunctionBindResult, NULL);
SetAndRestoreValue<bool> prevAllowVariableDeclarations(mModule->mCurMethodState->mCurScope->mAllowVariableDeclarations, false); // Don't allow variable declarations in arguments
SetAndRestoreValue<BfFunctionBindResult*> prevBindResult(mFunctionBindResult, NULL);
SetAndRestoreValue<bool> prevAllowVariableDeclarations;
if (mModule->mCurMethodState != NULL)
prevAllowVariableDeclarations.Init(mModule->mCurMethodState->mCurScope->mAllowVariableDeclarations, false);
BfMethodInstance* methodInstance = moduleMethodInstance.mMethodInstance;

View file

@ -9791,17 +9791,11 @@ void BfModule::GetCustomAttributes(BfCustomAttributes* customAttributes, BfAttri
continue;
}
String attrName = attributesDirective->mAttributeTypeRef->ToString();
BfType* attrType = NULL;
BfAtomComposite nameComposite;
BfTypeDef* attrTypeDef = NULL;
if (mSystem->ParseAtomComposite(attrName + "Attribute", nameComposite))
{
BfTypeLookupError error;
error.mRefNode = attributesDirective->mAttributeTypeRef;
attrTypeDef = FindTypeDefRaw(nameComposite, 0, mCurTypeInstance, GetActiveTypeDef(NULL, true), &error);
}
BfType* attrType = ResolveTypeRef(attributesDirective->mAttributeTypeRef, BfPopulateType_Identity, BfResolveTypeRefFlag_Attribute);
BfTypeDef* attrTypeDef = NULL;
if ((attrType != NULL) && (attrType->IsTypeInstance()))
attrTypeDef = attrType->ToTypeInstance()->mTypeDef;
if ((mCompiler->mResolvePassData != NULL) && (mCompiler->mResolvePassData->mAutoComplete != NULL))
{
mCompiler->mResolvePassData->mAutoComplete->CheckAttributeTypeRef(attributesDirective->mAttributeTypeRef);
@ -9809,26 +9803,17 @@ void BfModule::GetCustomAttributes(BfCustomAttributes* customAttributes, BfAttri
mCompiler->mResolvePassData->HandleTypeReference(attributesDirective->mAttributeTypeRef, attrTypeDef);
}
if (attrTypeDef == NULL)
{
TypeRefNotFound(attributesDirective->mAttributeTypeRef, "Attribute");
continue;
}
bool isBypassedAttr = false;
if (attrTypeDef != NULL)
{
attrType = mContext->mUnreifiedModule->ResolveTypeDef(attrTypeDef, BfPopulateType_Identity);
{
// 'Object' has some dependencies on some attributes, but those attributes are classes so we have a circular dependency issue
// We solve it by having a 'bypass' for known attributes that Object depends on
if ((attributesDirective->mArguments.empty()) && (autoComplete == NULL) && (attrType != NULL) && (attrType->IsTypeInstance()))
{
if (attrTypeDef == mCompiler->mCReprAttributeTypeDef)
{
BfTypeInstance* attrTypeInst = attrType->ToTypeInstance();
//if (attrTypeDef == mCompiler->mCReprAttributeTypeDef)
if (attrType->IsInstanceOf(mCompiler->mCReprAttributeTypeDef))
{
for (auto methodDef : attrTypeDef->mMethods)
{
if ((methodDef->mMethodType == BfMethodType_Ctor) && (methodDef->mProtection == BfProtection_Public))
@ -9860,17 +9845,6 @@ void BfModule::GetCustomAttributes(BfCustomAttributes* customAttributes, BfAttri
}
}
// Don't allow this
/*else if (mContext->mCurTypeState != NULL)
{
SetAndRestoreValue<BfTypeReference*> prevTypeRef(mContext->mCurTypeState->mCurAttributeTypeRef, attributesDirective->mAttributeTypeRef);
attrType = mContext->mUnreifiedModule->ResolveTypeRef(attributesDirective->mAttributeTypeRef, BfPopulateType_DataAndMethods);
}
else
{
attrType = mContext->mUnreifiedModule->ResolveTypeRef(attributesDirective->mAttributeTypeRef);
}*/
BfTypeInstance* attrTypeInst = NULL;
if (attrType == NULL)
continue;

View file

@ -1276,6 +1276,7 @@ enum BfResolveTypeRefFlags
BfResolveTypeRefFlag_AllowGenericParamConstValue = 0x10 | 0x20,
BfResolveTypeRefFlag_AutoComplete = 0x40,
BfResolveTypeRefFlag_FromIndirectSource = 0x80, // Such as a type alias or a generic parameter
BfResolveTypeRefFlag_Attribute = 0x100
};
enum BfSrcPosFlags
@ -1689,7 +1690,7 @@ public:
BfTypeDef* FindTypeDefRaw(const BfAtomComposite& findName, int numGenericArgs, BfTypeInstance* typeInstance, BfTypeDef* useTypeDef, BfTypeLookupError* error);
BfTypeDef* FindTypeDef(const BfAtomComposite& findName, int numGenericArgs = 0, BfTypeInstance* typeInstanceOverride = NULL, BfTypeLookupError* error = NULL);
BfTypeDef* FindTypeDef(const StringImpl& typeName, int numGenericArgs = 0, BfTypeInstance* typeInstanceOverride = NULL, BfTypeLookupError* error = NULL);
BfTypeDef* FindTypeDef(BfTypeReference* typeRef, BfTypeInstance* typeInstanceOverride = NULL, BfTypeLookupError* error = NULL, int numGenericParams = 0);
BfTypeDef* FindTypeDef(BfTypeReference* typeRef, BfTypeInstance* typeInstanceOverride = NULL, BfTypeLookupError* error = NULL, int numGenericParams = 0, BfResolveTypeRefFlags resolveFlags = (BfResolveTypeRefFlags)0);
BfTypedValue TryLookupGenericConstVaue(BfIdentifierNode* identifierNode, BfType* expectingType);
void CheckTypeRefFixit(BfAstNode* typeRef, const char* appendName = NULL);
void CheckIdentifierFixit(BfAstNode* node);

View file

@ -6398,7 +6398,7 @@ BfTypeDef* BfModule::FindTypeDef(const StringImpl& typeName, int numGenericArgs,
return result;
}
BfTypeDef* BfModule::FindTypeDef(BfTypeReference* typeRef, BfTypeInstance* typeInstanceOverride, BfTypeLookupError* error, int numGenericParams)
BfTypeDef* BfModule::FindTypeDef(BfTypeReference* typeRef, BfTypeInstance* typeInstanceOverride, BfTypeLookupError* error, int numGenericParams, BfResolveTypeRefFlags resolveFlags)
{
BP_ZONE("BfModule::FindTypeDef_5");
@ -6442,10 +6442,20 @@ BfTypeDef* BfModule::FindTypeDef(BfTypeReference* typeRef, BfTypeInstance* typeI
}
BfSizedAtomComposite findName;
if (!mSystem->ParseAtomComposite(findNameStr, findName))
if ((resolveFlags & BfResolveTypeRefFlag_Attribute) != 0)
{
return NULL;
String attributeName;
attributeName += findNameStr;
attributeName += "Attribute";
if (!mSystem->ParseAtomComposite(attributeName, findName))
return NULL;
}
else
{
if (!mSystem->ParseAtomComposite(findNameStr, findName))
return NULL;
}
#ifdef BF_AST_HAS_PARENT_MEMBER
if (auto parentGenericTypeRef = BfNodeDynCast<BfGenericInstanceTypeRef>(typeRef->mParent))
@ -6972,7 +6982,7 @@ BfType* BfModule::ResolveTypeRef(BfTypeReference* typeRef, BfPopulateType popula
{
BfTypeLookupError error;
error.mRefNode = typeRef;
typeDef = FindTypeDef(typeRef, contextTypeInstance, &error);
typeDef = FindTypeDef(typeRef, contextTypeInstance, &error, 0, resolveFlags);
if (auto namedTypeRef = BfNodeDynCast<BfNamedTypeReference>(typeRef))
{