Skip to content

Commit

Permalink
fix easy comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kaizhangNV committed Sep 19, 2024
1 parent bb22fa2 commit 5401494
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 23 deletions.
10 changes: 10 additions & 0 deletions source/slang/slang-ast-builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ class SharedASTBuilder : public RefObject
}
return m_thisTypeName;
}

Name* getCtorName()
{
if (!m_ctorName)
{
m_ctorName = getNamePool()->getName("$init");
}
return m_ctorName;
}
protected:
// State shared between ASTBuilders

Expand Down Expand Up @@ -114,6 +123,7 @@ class SharedASTBuilder : public RefObject
NamePool* m_namePool = nullptr;

Name* m_thisTypeName = nullptr;
Name* m_ctorName = nullptr;

// This is a private builder used for these shared types
ASTBuilder* m_astBuilder = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion source/slang/slang-check-conversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ namespace Slang
// b. Collect and coerce init-list arguments into constructor parameters
// c. Create InvokeExpr for our valid ConstructorDecl
List<ConstructorDecl*> ctorList = _getCtorList(this->getASTBuilder(), this, toStructDecl, nullptr);
bool allowCStyleInitList = checkIfCStyleStruct(this, toStructDecl);
bool allowCStyleInitList = isCStyleStructDecl(this, toStructDecl, ctorList);

List<Expr*> maybeArgList;
maybeArgList.reserve(argCount);
Expand Down
5 changes: 3 additions & 2 deletions source/slang/slang-check-decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2798,7 +2798,7 @@ namespace Slang

// `checkIfCStyleStruct` must check after we add all possible Ctors.
// If we are a CStyleStruct add DefaultConstructExpr to all params (excluding arg 0)
bool isCStyleStruct = checkIfCStyleStruct(this, structDecl);
bool isCStyleStruct = isCStyleStructDecl(this, structDecl, ctorList);
if (isCStyleStruct && generatedMemberwiseCtors.getCount() > 0)
{
// We know the user provided 0 non-default ctor's, we only had a chance to generate non default Ctors above in this AST pass.
Expand Down Expand Up @@ -8299,6 +8299,7 @@ namespace Slang
return ctor->containsOption(ConstructorTags::Synthesized)
&& ctor->getParameters().getCount() != 0
&& !allParamHaveInitExpr(ctor);
// return ctor->findModifier<SynthesizedModifier>();
}

template<typename T>
Expand Down Expand Up @@ -8766,7 +8767,7 @@ namespace Slang
}

// Compiler generated ctor may be destroyed
bool destroyedDefaultCtor = false;
bool destroyedDefaultCtor = false;
if(structDeclInfo.m_defaultCtor
&& structDeclInfo.m_defaultCtor->containsOption(ConstructorTags::Synthesized))
{
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 @@ -3037,7 +3037,7 @@ namespace Slang
Expr* constructZeroInitListFunc(SemanticsVisitor* visitor, StructDecl* structDecl, Type* structDeclType, ConstructZeroInitListOptions options);
FuncDecl* findZeroInitListFunc(StructDecl* structDecl);

bool checkIfCStyleStruct(SemanticsVisitor* visitor, StructDecl* decl);
bool isCStyleStructDecl(SemanticsVisitor* visitor, StructDecl* decl, List<ConstructorDecl*> const& ctorList);

DefaultConstructExpr* createDefaultConstructExprForType(ASTBuilder* m_astBuilder, QualType type, SourceLoc loc);

Expand Down
41 changes: 22 additions & 19 deletions source/slang/slang-constructor-utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,35 @@ namespace Slang
return defaultConstructExpr;
}

bool allParamHaveInitExpr(ConstructorDecl* ctor)
{
for (auto i : ctor->getParameters())
if (!i->initExpr)
return false;
return true;
}

ConstructorDecl* _getDefaultCtor(StructDecl* structDecl)
{
for (auto ctor : structDecl->getMembersOfType<ConstructorDecl>())
{
if (!ctor->body || ctor->members.getCount() != 0)
// 1. default ctor must have definition
// 2. default ctor must have no parameters or all parameters have init expr
if (!ctor->body || (ctor->members.getCount() != 0 && !allParamHaveInitExpr(ctor)))
continue;
return ctor;
}
return nullptr;
}
bool allParamHaveInitExpr(ConstructorDecl* ctor)
{
for (auto i : ctor->getParameters())
if (!i->initExpr)
return false;
return true;
}

List<ConstructorDecl*> _getCtorList(ASTBuilder* m_astBuilder, SemanticsVisitor* visitor, StructDecl* structDecl, ConstructorDecl** defaultCtorOut)
{
List<ConstructorDecl*> ctorList;

auto ctorLookupResult = lookUpMember(
m_astBuilder,
visitor,
visitor->getName("$init"),
m_astBuilder->getSharedASTBuilder()->getCtorName(),
DeclRefType::create(m_astBuilder, structDecl),
structDecl->ownedScope,
LookupMask::Function,
Expand All @@ -63,7 +67,7 @@ namespace Slang
return ctorList;
}

for (auto m : ctorLookupResult.items)
for (auto m : ctorLookupResult)
{
lookupResultHandle(m);
}
Expand Down Expand Up @@ -151,7 +155,7 @@ namespace Slang
return nullptr;
}

Expr* _constructZeroInitListFuncMakeDefaultCtor(SemanticsVisitor* visitor, StructDecl* structDecl, Type* structDeclType, ConstructorDecl* defaultCtor)
Expr* constructDefaultCtorInvocationExpr(SemanticsVisitor* visitor, StructDecl* structDecl, Type* structDeclType, ConstructorDecl* defaultCtor)
{
auto* invoke = visitor->getASTBuilder()->create<InvokeExpr>();
auto member = visitor->getASTBuilder()->getMemberDeclRef(structDecl->getDefaultDeclRef(), defaultCtor);
Expand Down Expand Up @@ -192,34 +196,34 @@ namespace Slang
}
}
if(canCreateCtor)
return _constructZeroInitListFuncMakeDefaultCtor(visitor, structDecl, structDeclType, defaultCtor);
return constructDefaultCtorInvocationExpr(visitor, structDecl, structDeclType, defaultCtor);
}

// 2.
if (auto zeroInitListFunc = findZeroInitListFunc(structDecl))
if (auto defaultInitFunc = findZeroInitListFunc(structDecl))
{
auto* invoke = visitor->getASTBuilder()->create<InvokeExpr>();
DeclRef<Decl> member;
auto declRefType = as<DeclRefType>(structDeclType);
if(declRefType && as<GenericAppDeclRef>(declRefType->getDeclRefBase()))
member = visitor->getASTBuilder()->getMemberDeclRef(as<GenericAppDeclRef>(declRefType->getDeclRefBase()), zeroInitListFunc);
member = visitor->getASTBuilder()->getMemberDeclRef(as<GenericAppDeclRef>(declRefType->getDeclRefBase()), defaultInitFunc);
else
member = visitor->getASTBuilder()->getMemberDeclRef(structDecl, zeroInitListFunc);
member = visitor->getASTBuilder()->getMemberDeclRef(structDecl, defaultInitFunc);

invoke->functionExpr = visitor->ConstructDeclRefExpr(member, nullptr, zeroInitListFunc->getName(), zeroInitListFunc->loc, nullptr);
invoke->functionExpr = visitor->ConstructDeclRefExpr(member, nullptr, defaultInitFunc->getName(), defaultInitFunc->loc, nullptr);
invoke->type = structDeclType;
return invoke;
}

// 3.
if (defaultCtor)
return _constructZeroInitListFuncMakeDefaultCtor(visitor, structDecl, structDeclType, defaultCtor);
return constructDefaultCtorInvocationExpr(visitor, structDecl, structDeclType, defaultCtor);

// 4.
return createDefaultConstructExprForType(visitor->getASTBuilder(), QualType(structDeclType), {});
}

bool checkIfCStyleStruct(SemanticsVisitor* visitor, StructDecl* structDecl)
bool isCStyleStructDecl(SemanticsVisitor* visitor, StructDecl* structDecl, List<ConstructorDecl*> const& ctorList)
{
// CStyleStruct follows the following rules:
// 1. Does not contain a non 'Synthesized' Ctor (excluding 'DefaultCtor')
Expand All @@ -236,7 +240,6 @@ namespace Slang
return *isCStyleStruct;

// Add to IsCStyleStruct cache
auto ctorList = _getCtorList(visitor->getASTBuilder(), visitor, structDecl, nullptr);
int nonDefaultInitCount = 0;
for (auto i : ctorList)
{
Expand Down

0 comments on commit 5401494

Please sign in to comment.