Skip to content

Commit

Permalink
Fixes for some problems with assignments from const references
Browse files Browse the repository at this point in the history
  • Loading branch information
julianstorer committed Nov 6, 2020
1 parent d1bed3c commit 45327b3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
14 changes: 8 additions & 6 deletions source/modules/soul_core/compiler/soul_HeartGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,11 @@ struct HEARTGenerator : public ASTVisitor
}

heart::Variable& createVariableDeclaration (AST::VariableDeclaration& v,
heart::Variable::Role role)
heart::Variable::Role role,
bool canBeReference)
{
auto& av = module.allocate<heart::Variable> (v.context.location, v.getType(),
auto& av = module.allocate<heart::Variable> (v.context.location,
canBeReference ? v.getType() : v.getType().removeReferenceIfPresent(),
convertIdentifier (v.name), role);
v.generatedVariable = av;

Expand All @@ -118,7 +120,7 @@ struct HEARTGenerator : public ASTVisitor
heart::Variable& addExternalVariable (AST::VariableDeclaration& v)
{
SOUL_ASSERT (v.isExternal);
auto& hv = createVariableDeclaration (v, heart::Variable::Role::external);
auto& hv = createVariableDeclaration (v, heart::Variable::Role::external, false);
module.stateVariables.add (hv);
return hv;
}
Expand Down Expand Up @@ -399,7 +401,7 @@ struct HEARTGenerator : public ASTVisitor

for (auto p : f.parameters)
{
auto& v = createVariableDeclaration (p, heart::Variable::Role::parameter);
auto& v = createVariableDeclaration (p, heart::Variable::Role::parameter, true);

if (af.functionType.isEvent() && v.getType().isNonConstReference())
p->context.throwError (Errors::eventParamsCannotBeNonConstReference());
Expand Down Expand Up @@ -897,12 +899,12 @@ struct HEARTGenerator : public ASTVisitor

// Skip writing constant or unwritten-to variables to the state
if (! (v.numWrites == 0 && (type.isPrimitive() || type.isBoundedInt())))
module.stateVariables.add (createVariableDeclaration (v, heart::Variable::Role::state));
module.stateVariables.add (createVariableDeclaration (v, heart::Variable::Role::state, false));
}
}
else
{
auto& target = createVariableDeclaration (v, heart::Variable::Role::mutableLocal);
auto& target = createVariableDeclaration (v, heart::Variable::Role::mutableLocal, false);

if (v.initialValue != nullptr)
visitWithDestination (target, *v.initialValue);
Expand Down
4 changes: 1 addition & 3 deletions source/modules/soul_core/compiler/soul_ResolutionPass.h
Original file line number Diff line number Diff line change
Expand Up @@ -1482,9 +1482,7 @@ struct ResolutionPass final
void resolveVariableDeclarationInitialValue (AST::VariableDeclaration& v, const Type& type)
{
if (AST::isResolvedAsValue (v.initialValue))
SanityCheckPass::expectSilentCastPossible (v.context,
type,
*v.initialValue);
SanityCheckPass::expectSilentCastPossible (v.context, type, *v.initialValue);

if (! (AST::isResolvedAsValue (v.initialValue) && v.initialValue->getResultType().isIdentical (type)))
v.initialValue = allocator.allocate<AST::TypeCast> (v.initialValue->context, type, *v.initialValue);
Expand Down
17 changes: 13 additions & 4 deletions source/modules/soul_core/types/soul_TypeRules.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,19 @@ struct TypeRules
if ((dest.isPrimitive() || dest.isVectorOfSize1()) && source.isPrimitive() && ! dest.isReference())
return getCastType (dest.getPrimitiveType(), source.getPrimitiveType());

if (source.isEqual (dest, Type::ignoreConst))
if (! (dest.isReference() || source.isReference()))
return CastType::identity;

if (! dest.isReference())
{
if (source.isReference())
{
if (source.isConst() && source.isEqual (dest, Type::ignoreConst | Type::ignoreReferences))
return CastType::identity;
}
else
{
if (source.isEqual (dest, Type::ignoreConst))
return CastType::identity;
}
}

if (dest.isArray())
{
Expand Down

0 comments on commit 45327b3

Please sign in to comment.