Skip to content

Commit

Permalink
Address some comment feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
kaizhangNV committed Oct 6, 2024
1 parent 73a61d1 commit 0e8aa84
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 49 deletions.
32 changes: 16 additions & 16 deletions source/slang/slang-check-conversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ namespace Slang
return true;
}

Expr* SemanticsVisitor::_prepareCtorInvokeExpr(Type* toType, const SourceLoc& loc, const List<Expr*>& coercedArgs)
Expr* SemanticsVisitor::_createCtorInvokeExpr(Type* toType, const SourceLoc& loc, const List<Expr*>& coercedArgs)
{
auto* varExpr = getASTBuilder()->create<VarExpr>();
varExpr->type = (QualType)getASTBuilder()->getTypeType(toType);
Expand All @@ -339,10 +339,10 @@ namespace Slang
// translation from initializer list to constructor invocation if the struct has constructor.
bool SemanticsVisitor::_invokeExprForExplicitCtor(Type* toType, InitializerListExpr* fromInitializerListExpr, Expr** outExpr)
{
if(auto toDeclRefType = as<DeclRefType>(toType))
if (auto toDeclRefType = as<DeclRefType>(toType))
{
auto toTypeDeclRef = toDeclRefType->getDeclRef();
if(auto toStructDeclRef = toTypeDeclRef.as<StructDecl>())
if (auto toStructDeclRef = toTypeDeclRef.as<StructDecl>())
{
if (isFromStdLib(toStructDeclRef.getDecl()))
{
Expand All @@ -352,7 +352,7 @@ namespace Slang

if (_hasExplicitConstructor(toStructDeclRef.getDecl()))
{
auto ctorInvokeExpr = _prepareCtorInvokeExpr(toType, fromInitializerListExpr->loc, fromInitializerListExpr->args);
auto ctorInvokeExpr = _createCtorInvokeExpr(toType, fromInitializerListExpr->loc, fromInitializerListExpr->args);
ctorInvokeExpr = CheckTerm(ctorInvokeExpr);
if (outExpr && ctorInvokeExpr)
{
Expand All @@ -371,10 +371,10 @@ namespace Slang
Expr** outExpr)
{
StructDecl* structDecl = nullptr;
if(auto toDeclRefType = as<DeclRefType>(toType))
if (auto toDeclRefType = as<DeclRefType>(toType))
{
auto toTypeDeclRef = toDeclRefType->getDeclRef();
if(auto toStructDeclRef = toTypeDeclRef.as<StructDecl>())
if (auto toStructDeclRef = toTypeDeclRef.as<StructDecl>())
{
structDecl = toStructDeclRef.getDecl();
}
Expand All @@ -388,7 +388,7 @@ namespace Slang
SLANG_ASSERT(synthesizedConstructor);

List<Expr*> coercedArgs;
auto ctorInvokeExpr = _prepareCtorInvokeExpr(toType, fromInitializerListExpr->loc, fromInitializerListExpr->args);
auto ctorInvokeExpr = _createCtorInvokeExpr(toType, fromInitializerListExpr->loc, fromInitializerListExpr->args);

DiagnosticSink tempSink(getSourceManager(), nullptr);
SemanticsVisitor subVisitor(withSink(&tempSink));
Expand Down Expand Up @@ -452,12 +452,12 @@ namespace Slang
// we will collect the new arguments here
List<Expr*> coercedArgs;

if(isEffectivelyScalarForInitializerLists(toType))
if (isEffectivelyScalarForInitializerLists(toType))
{
// For any type that is effectively a non-aggregate,
// we expect to read a single value from the initializer list
//
if(ioArgIndex < argCount)
if (ioArgIndex < argCount)
{
auto arg = fromInitializerListExpr->args[ioArgIndex++];
return _coerce(
Expand Down Expand Up @@ -502,7 +502,7 @@ namespace Slang
return false;
}

for(UInt ee = 0; ee < elementCount; ++ee)
for (UInt ee = 0; ee < elementCount; ++ee)
{
Expr* coercedArg = nullptr;
bool argResult = _readValueFromInitializerList(
Expand All @@ -521,7 +521,7 @@ namespace Slang
}
}
}
else if(auto toArrayType = as<ArrayExpressionType>(toType))
else if (auto toArrayType = as<ArrayExpressionType>(toType))
{
// TODO(tfoley): If we can compute the size of the array statically,
// then we want to check that there aren't too many initializers present
Expand Down Expand Up @@ -781,12 +781,12 @@ namespace Slang
// If this isn't prohibited, then we can proceed to try and coerce from
// the initializer list itself; assuming that coercion is closed under
// composition this shouldn't fail.
if(!as<InitializerListType>(fromInitializerListExpr->type) &&
if (!as<InitializerListType>(fromInitializerListExpr->type) &&
!canCoerce(toType, fromInitializerListExpr->type, nullptr))
return _failedCoercion(toType, outToExpr, fromInitializerListExpr);

// try to invoke the user-defined constructor if it exists
if(_invokeExprForExplicitCtor(toType, fromInitializerListExpr, outToExpr))
if (_invokeExprForExplicitCtor(toType, fromInitializerListExpr, outToExpr))
{
return true;
}
Expand All @@ -798,14 +798,14 @@ namespace Slang
}

// We will fall back to the legacy logic of initialize list.
if(!_readAggregateValueFromInitializerList(toType, outToExpr, fromInitializerListExpr, argIndex))
if (!_readAggregateValueFromInitializerList(toType, outToExpr, fromInitializerListExpr, argIndex))
{
return false;
}

if(argIndex != argCount)
if (argIndex != argCount)
{
if( outToExpr )
if ( outToExpr )
{
getSink()->diagnose(fromInitializerListExpr, Diagnostics::tooManyInitializers, argIndex, argCount);
}
Expand Down
49 changes: 17 additions & 32 deletions source/slang/slang-check-decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace Slang
{
static ConstructorDecl* _getDefaultCtor(StructDecl* structDecl);
static List<ConstructorDecl*> _getCtorList(ASTBuilder* m_astBuilder, SemanticsVisitor* visitor, StructDecl* structDecl, ConstructorDecl** defaultCtorOut);
static Expr* constructDefaultInitExprForVar(SemanticsVisitor* visitor, VarDeclBase* varDecl);
static Expr* constructDefaultInitExprForType(SemanticsVisitor* visitor, VarDeclBase* varDecl);

/// Visitor to transition declarations to `DeclCheckState::CheckedModifiers`
struct SemanticsDeclModifiersVisitor
Expand Down Expand Up @@ -229,8 +229,8 @@ namespace Slang
//
// We will defer the actual implementation of the constructor to the body visit, because
// we will have full information about each field in the struct during that stage.
void _synthesizeEmptyConstructor(StructDecl* structDecl);
void _searchMembersWithHigherVisibility(StructDecl* structDecl, const DeclVisibility& ctorVisibility, List<VarDeclBase*>& resultMembers);
void _synthesizeCtorSignature(StructDecl* structDecl);
void _searchMembersWithHigherVisibility(StructDecl* structDecl, const DeclVisibility ctorVisibility, List<VarDeclBase*>& resultMembers);
};

struct SemanticsDeclTypeResolutionVisitor
Expand Down Expand Up @@ -2158,7 +2158,7 @@ namespace Slang
return true;
}

static Expr* constructDefaultInitExprForVar(SemanticsVisitor* visitor, VarDeclBase* varDecl)
static Expr* constructDefaultInitExprForType(SemanticsVisitor* visitor, VarDeclBase* varDecl)
{
if (!varDecl->type || !varDecl->type.type)
return nullptr;
Expand Down Expand Up @@ -2201,7 +2201,7 @@ namespace Slang
&& as<VarDecl>(varDecl)
)
{
varDecl->initExpr = constructDefaultInitExprForVar(this, varDecl);
varDecl->initExpr = constructDefaultInitExprForType(this, varDecl);
}

if (auto initExpr = varDecl->initExpr)
Expand Down Expand Up @@ -6967,7 +6967,7 @@ namespace Slang
}
}

void SemanticsDeclBasesVisitor::_searchMembersWithHigherVisibility(StructDecl* structDecl, const DeclVisibility& ctorVisibility, List<VarDeclBase*>& resultMembers)
void SemanticsDeclBasesVisitor::_searchMembersWithHigherVisibility(StructDecl* structDecl, const DeclVisibility ctorVisibility, List<VarDeclBase*>& resultMembers)
{
auto findMembers = [&](StructDecl* structDecl)
{
Expand All @@ -6983,15 +6983,10 @@ namespace Slang

for (auto inheritanceMember : structDecl->getMembersOfType<InheritanceDecl>())
{
auto declRefType = as<DeclRefType>(inheritanceMember->base.type);
if (!declRefType)
continue;

auto structOfInheritance = as<StructDecl>(declRefType->getDeclRef().getDecl());
if (!structOfInheritance)
continue;

findMembers(structOfInheritance);
// TODO: We might need to use the parameters of the base type's constructor instead of the members.
// But the problem is base type could have more than one constructor, which one should we use?
if (auto baseTypeDecl = isDeclRefTypeOf<StructDecl>(inheritanceMember->base.type))
findMembers(baseTypeDecl.getDecl());
}

findMembers(structDecl);
Expand All @@ -7016,10 +7011,10 @@ namespace Slang

// For the 2nd condition, we need to check if the type is default initializable, if so,
// we can use the default constructor.
return constructDefaultInitExprForVar(visitor, varDecl);
return constructDefaultInitExprForType(visitor, varDecl);
}

void SemanticsDeclBasesVisitor::_synthesizeEmptyConstructor(StructDecl* structDecl)
void SemanticsDeclBasesVisitor::_synthesizeCtorSignature(StructDecl* structDecl)
{
// If a type already defines any explicit constructors, do not synthesize any constructors.
if (_hasExplicitConstructor(structDecl))
Expand All @@ -7041,7 +7036,6 @@ namespace Slang
ctor->members.reserve(resultMembers.getCount());

// 2. Add the parameter list
uint32_t paramIndex = 0;
bool stopProcessingDefaultValues = false;
for (SlangInt i = resultMembers.getCount() - 1; i >= 0; i--)
{
Expand All @@ -7056,14 +7050,7 @@ namespace Slang
stopProcessingDefaultValues = true;

ctorParam->parentDecl = ctor;
StringBuilder prefix;
prefix << "param_" << paramIndex++ << "_";

Name* paramName = member->getName() ?
getName(prefix + member->getName()->text) :
getName(prefix);

ctorParam->nameAndLoc = NameLoc(paramName, ctor->loc);
ctorParam->nameAndLoc = NameLoc(member->getName(), ctor->loc);
ctor->members.add(ctorParam);
}
ctor->members.reverse();
Expand Down Expand Up @@ -7159,7 +7146,7 @@ namespace Slang
}

if (!isFromStdLib(decl))
_synthesizeEmptyConstructor(decl);
_synthesizeCtorSignature(decl);
}

void SemanticsDeclBasesVisitor::visitClassDecl(ClassDecl* decl)
Expand Down Expand Up @@ -7308,17 +7295,15 @@ namespace Slang

bool SemanticsVisitor::_hasExplicitConstructor(StructDecl* structDecl)
{
bool result = false;
for (auto ctor : getMembersOfType<ConstructorDecl>(getASTBuilder(), structDecl, MemberFilterStyle::All))
{
// constructor that is not synthesized must be user defined.
if (ctor.getDecl()->findModifier<SynthesizedModifier>() == nullptr)
{
result = true;
break;
return true;
}
}
return result;
return false;
}

void SemanticsDeclBasesVisitor::visitEnumDecl(EnumDecl* decl)
Expand Down Expand Up @@ -8782,7 +8767,7 @@ namespace Slang
if (!isDefaultInitializableType
|| varDeclBase->initExpr)
continue;
varDeclBase->initExpr = constructDefaultInitExprForVar(this, varDeclBase);
varDeclBase->initExpr = constructDefaultInitExprForType(this, varDeclBase);
}

synthesizeCtorBody(structDeclInfo, inheritanceDefaultCtorList, structDecl);
Expand Down
2 changes: 1 addition & 1 deletion source/slang/slang-check-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2760,7 +2760,7 @@ namespace Slang

bool _invokeExprForExplicitCtor(Type* toType, InitializerListExpr* fromInitializerListExpr, Expr** outExpr);
bool _invokeExprForSynthesizedCtor(Type* toType, InitializerListExpr* fromInitializerListExpr, Expr** outExpr);
Expr* _prepareCtorInvokeExpr(Type* toType, const SourceLoc& loc, const List<Expr*>& coercedArgs);
Expr* _createCtorInvokeExpr(Type* toType, const SourceLoc& loc, const List<Expr*>& coercedArgs);
bool _hasExplicitConstructor(StructDecl* structDecl);
ConstructorDecl* _getSynthesizedConstructor(StructDecl* structDecl);
bool isCStyleStruct(StructDecl* structDecl);
Expand Down

0 comments on commit 0e8aa84

Please sign in to comment.