Skip to content

Commit

Permalink
Add support for 'autoBitcopiable'
Browse files Browse the repository at this point in the history
An autoBitcopiable datatype will be bitcopiable if all the inner types
are bitcopiable.
  • Loading branch information
lucteo committed Apr 26, 2019
1 parent 8647cd8 commit 2088839
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 9 deletions.
18 changes: 9 additions & 9 deletions SparrowImplicitLib/std/tuple.spr
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,33 @@ concept TupleType(x) if (

using oper_precedence_~ = oper_precedence_*

[initCtor] datatype Tuple(t1, t2: Type)
[initCtor, autoBitcopiable] datatype Tuple(t1, t2: Type)
using arity = 2
v1: t1
v2: t2

[initCtor] datatype Tuple(t1, t2, t3: Type)
[initCtor, autoBitcopiable] datatype Tuple(t1, t2, t3: Type)
using arity = 3
v1: t1
v2: t2
v3: t3

[initCtor] datatype Tuple(t1, t2, t3, t4: Type)
[initCtor, autoBitcopiable] datatype Tuple(t1, t2, t3, t4: Type)
using arity = 4
v1: t1
v2: t2
v3: t3
v4: t4

[initCtor] datatype Tuple(t1, t2, t3, t4, t5: Type)
[initCtor, autoBitcopiable] datatype Tuple(t1, t2, t3, t4, t5: Type)
using arity = 5
v1: t1
v2: t2
v3: t3
v4: t4
v5: t5

[initCtor] datatype Tuple(t1, t2, t3, t4, t5, t6: Type)
[initCtor, autoBitcopiable] datatype Tuple(t1, t2, t3, t4, t5, t6: Type)
using arity = 6
v1: t1
v2: t2
Expand All @@ -43,7 +43,7 @@ using oper_precedence_~ = oper_precedence_*
v5: t5
v6: t6

[initCtor] datatype Tuple(t1, t2, t3, t4, t5, t6, t7: Type)
[initCtor, autoBitcopiable] datatype Tuple(t1, t2, t3, t4, t5, t6, t7: Type)
using arity = 7
v1: t1
v2: t2
Expand All @@ -53,7 +53,7 @@ using oper_precedence_~ = oper_precedence_*
v6: t6
v7: t7

[initCtor] datatype Tuple(t1, t2, t3, t4, t5, t6, t7, t8: Type)
[initCtor, autoBitcopiable] datatype Tuple(t1, t2, t3, t4, t5, t6, t7, t8: Type)
using arity = 8
v1: t1
v2: t2
Expand All @@ -64,7 +64,7 @@ using oper_precedence_~ = oper_precedence_*
v7: t7
v8: t8

[initCtor] datatype Tuple(t1, t2, t3, t4, t5, t6, t7, t8, t9: Type)
[initCtor, autoBitcopiable] datatype Tuple(t1, t2, t3, t4, t5, t6, t7, t8, t9: Type)
using arity = 9
v1: t1
v2: t2
Expand All @@ -76,7 +76,7 @@ using oper_precedence_~ = oper_precedence_*
v8: t8
v9: t9

[initCtor] datatype Tuple(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10: Type)
[initCtor, autoBitcopiable] datatype Tuple(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10: Type)
using arity = 10
v1: t1
v2: t2
Expand Down
14 changes: 14 additions & 0 deletions src/SparrowFrontend/Mods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,18 @@ void ModBitCopiable_beforeComputeType(Nest_Modifier*, Node* node) {
Nest_setPropertyInt(node, propBitCopiable, 1);
}

void ModAutoBitCopiable_beforeComputeType(Nest_Modifier*, Node* node) {
/// Check to apply only to datatypes
if (node->nodeKind != nkSparrowDeclSprDatatype) {
REP_ERROR(node->location,
"autoBitcopiable modifier can be applied only to datatypes (applied to %1%)") %
Nest_nodeKindName(node);
return;
}

Nest_setPropertyInt(node, propAutoBitCopiable, 1);
}

void ModMacro_beforeComputeType(Nest_Modifier*, Node* node) {
/// Check to apply only to functions
if (node->nodeKind != nkSparrowDeclSprFunction) {
Expand Down Expand Up @@ -140,6 +152,7 @@ Nest_Modifier _convertMod = {modTypeBeforeComputeType, &ModConvert_beforeCompute
Nest_Modifier _noDefaultMod = {modTypeBeforeComputeType, &ModNoDefault_beforeComputeType};
Nest_Modifier _initCtorMod = {modTypeBeforeComputeType, ModInitCtor_beforeComputeType};
Nest_Modifier _bitCopiableMod = {modTypeBeforeComputeType, &ModBitCopiable_beforeComputeType};
Nest_Modifier _autoBitCopiableMod = {modTypeBeforeComputeType, &ModAutoBitCopiable_beforeComputeType};
Nest_Modifier _macroMod = {modTypeBeforeComputeType, &ModMacro_beforeComputeType};
Nest_Modifier _noInlineMod = {modTypeBeforeComputeType, &ModNoInline_beforeComputeType};

Expand All @@ -154,6 +167,7 @@ Nest_Modifier* SprFe_getConvertMod() { return &_convertMod; }
Nest_Modifier* SprFe_getNoDefaultMod() { return &_noDefaultMod; }
Nest_Modifier* SprFe_getInitCtorMod() { return &_initCtorMod; }
Nest_Modifier* SprFe_getBitCopiableMod() { return &_bitCopiableMod; }
Nest_Modifier* SprFe_getAutoBitCopiableMod() { return &_autoBitCopiableMod; }
Nest_Modifier* SprFe_getMacroMod() { return &_macroMod; }
Nest_Modifier* SprFe_getNoInlineMod() { return &_noInlineMod; }

Expand Down
1 change: 1 addition & 0 deletions src/SparrowFrontend/Mods.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Nest_Modifier* SprFe_getConvertMod();
Nest_Modifier* SprFe_getNoDefaultMod();
Nest_Modifier* SprFe_getInitCtorMod();
Nest_Modifier* SprFe_getBitCopiableMod();
Nest_Modifier* SprFe_getAutoBitCopiableMod();
Nest_Modifier* SprFe_getMacroMod();
Nest_Modifier* SprFe_getNoInlineMod();

Expand Down
13 changes: 13 additions & 0 deletions src/SparrowFrontend/Nodes/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ void applyModifier(NodeHandle base, NodeHandle modNode) {
mod = SprFe_getInitCtorMod();
else if (name == "bitcopiable")
mod = SprFe_getBitCopiableMod();
else if (name == "autoBitcopiable")
mod = SprFe_getAutoBitCopiableMod();
else if (name == "macro")
mod = SprFe_getMacroMod();
else if (name == "noInline")
Expand Down Expand Up @@ -661,6 +663,17 @@ Type DataTypeDecl::computeTypeImpl(DataTypeDecl node) {
NodeVector fields = getFields(node.childrenContext()->currentSymTab);
resultingStruct.addChildren(all(fields));

// Check for autoBitcopiable
if ( node.hasProperty(propAutoBitCopiable)) {
bool allAreBitcopiable = true;
for (auto f : fields) {
ASSERT(f->type);
allAreBitcopiable = allAreBitcopiable && isBitCopiable(f->type);
}
if (allAreBitcopiable)
resultingStruct.setProperty(propBitCopiable, 1);
}

// Check all the members
if (body) {
body.computeType(); // Ignore local errors
Expand Down
1 change: 1 addition & 0 deletions src/SparrowFrontend/Nodes/SprProperties.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ constexpr const char* propSprLiteralType = "spr.literalType";
constexpr const char* propSprLiteralData = "spr.literalData";
constexpr const char* propSprOperation = "spr.operation";
constexpr const char* propBitCopiable = "spr.bitcopiable";
constexpr const char* propAutoBitCopiable = "spr.autoBitcopiable";
} // namespace SprFrontend
49 changes: 49 additions & 0 deletions tests/Basic/datatype/autoBitcopiable.spr
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//!! -t "../SparrowImplicitLib.spr" -fno-main
// -dump-assembly

// Test purpose: checks 'autoBitcopiable' modifier works as expected

[ct, native("$typeDescription")] fun description(t: Type): StringRef
[ct, native("$typeIsBitcopiable")] fun isBitcopiable(t: Type): Bool

[initCtor]
datatype Data1
x: Int

[initCtor, bitcopiable]
datatype Data2
x: Int

[initCtor, autoBitcopiable]
datatype Pair(t1, t2: Type)
v1: t1
v2: t2

fun printBitcopiable(t: Type)
write(description(t))
[ct] if isBitcopiable(t)
writeLn(': bitcopiable')
else
writeLn(': regular')

[native("test")] fun test(n: Int)
printBitcopiable(Data1)
printBitcopiable(Data2)
printBitcopiable(Pair(Data1, Data1))
printBitcopiable(Pair(Data1, Data2))
printBitcopiable(Pair(Data2, Data1))
printBitcopiable(Pair(Data2, Data2))
printBitcopiable(Int)
printBitcopiable(StringRef)
printBitcopiable(Pair(Int, StringRef))
/*<<<Running()
Data1: regular
Data2: bitcopiable
Pair[Data1, Data1]: regular
Pair[Data1, Data2]: regular
Pair[Data2, Data1]: regular
Pair[Data2, Data2]: bitcopiable
Int: bitcopiable
StringRef: bitcopiable
Pair[Int, StringRef]: bitcopiable
>>>*/
1 change: 1 addition & 0 deletions tests/tests.in
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ Basic/datatype/generic.spr: Basic features - datatype - basic generi
Basic/datatype/initCtor.spr: Basic features - datatype - initCtor
Basic/datatype/bitcopiable.spr: Basic features - datatype - bitcopiable
Basic/datatype/queryBitcopiable.spr: Basic features - datatype - query bitcopiable
Basic/datatype/autoBitcopiable.spr: Basic features - datatype - auto bitcopiable
Basic/exp/dotOper.spr: Basic features - expressions - dot operator

Imports/ImportTwice.spr: Import twice the same file
Expand Down

0 comments on commit 2088839

Please sign in to comment.