Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
C
capnproto
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
capnproto
Commits
f966fc41
Commit
f966fc41
authored
Apr 09, 2013
by
Kenton Varda
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Store primitive fields XOR'd with their defaults.
parent
f3c4121b
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
99 additions
and
137 deletions
+99
-137
layout-test.c++
c++/src/capnproto/layout-test.c++
+0
-0
layout.c++
c++/src/capnproto/layout.c++
+0
-0
layout.h
c++/src/capnproto/layout.h
+0
-0
list.h
c++/src/capnproto/list.h
+2
-3
message.c++
c++/src/capnproto/message.c++
+7
-8
message.h
c++/src/capnproto/message.h
+7
-8
test.capnp
c++/src/capnproto/test.capnp
+1
-1
CxxGenerator.hs
compiler/src/CxxGenerator.hs
+39
-43
WireFormat.hs
compiler/src/WireFormat.hs
+30
-53
c++-header.mustache
compiler/src/c++-header.mustache
+12
-14
c++-source.mustache
compiler/src/c++-source.mustache
+1
-7
No files found.
c++/src/capnproto/layout-test.c++
View file @
f966fc41
This diff is collapsed.
Click to expand it.
c++/src/capnproto/layout.c++
View file @
f966fc41
This diff is collapsed.
Click to expand it.
c++/src/capnproto/layout.h
View file @
f966fc41
This diff is collapsed.
Click to expand it.
c++/src/capnproto/list.h
View file @
f966fc41
...
...
@@ -216,7 +216,7 @@ struct List<T, false> {
inline
uint
size
()
{
return
reader
.
size
()
/
ELEMENTS
;
}
inline
typename
T
::
Reader
operator
[](
uint
index
)
{
return
typename
T
::
Reader
(
reader
.
getStructElement
(
index
*
ELEMENTS
,
T
::
DEFAULT
.
words
));
return
typename
T
::
Reader
(
reader
.
getStructElement
(
index
*
ELEMENTS
));
}
typedef
internal
::
IndexingIterator
<
Reader
,
typename
T
::
Reader
>
iterator
;
...
...
@@ -235,8 +235,7 @@ struct List<T, false> {
inline
uint
size
()
{
return
builder
.
size
()
/
ELEMENTS
;
}
inline
typename
T
::
Builder
operator
[](
uint
index
)
{
return
typename
T
::
Builder
(
builder
.
getStructElement
(
index
*
ELEMENTS
,
(
T
::
DATA_SIZE
+
T
::
REFERENCE_COUNT
*
WORDS_PER_REFERENCE
)
/
ELEMENTS
,
T
::
DATA_SIZE
));
T
::
STRUCT_SIZE
.
total
()
/
ELEMENTS
,
T
::
STRUCT_SIZE
.
data
));
}
typedef
internal
::
IndexingIterator
<
Builder
,
typename
T
::
Builder
>
iterator
;
...
...
c++/src/capnproto/message.c++
View file @
f966fc41
...
...
@@ -38,7 +38,7 @@ MessageReader::~MessageReader() {
}
}
internal
::
StructReader
MessageReader
::
getRoot
(
const
word
*
defaultValue
)
{
internal
::
StructReader
MessageReader
::
getRoot
Internal
(
)
{
if
(
!
allocatedArena
)
{
static_assert
(
sizeof
(
internal
::
ReaderArena
)
<=
sizeof
(
arenaSpace
),
"arenaSpace is too small to hold a ReaderArena. Please increase it. This will break "
...
...
@@ -51,10 +51,9 @@ internal::StructReader MessageReader::getRoot(const word* defaultValue) {
if
(
segment
==
nullptr
||
!
segment
->
containsInterval
(
segment
->
getStartPtr
(),
segment
->
getStartPtr
()
+
1
))
{
arena
()
->
reportInvalidData
(
"Message did not contain a root pointer."
);
return
internal
::
StructReader
::
read
RootTrusted
(
defaultValue
,
defaultValue
);
return
internal
::
StructReader
::
read
Empty
(
);
}
else
{
return
internal
::
StructReader
::
readRoot
(
segment
->
getStartPtr
(),
defaultValue
,
segment
,
options
.
nestingLimit
);
return
internal
::
StructReader
::
readRoot
(
segment
->
getStartPtr
(),
segment
,
options
.
nestingLimit
);
}
}
...
...
@@ -88,16 +87,16 @@ internal::SegmentBuilder* MessageBuilder::getRootSegment() {
}
}
internal
::
StructBuilder
MessageBuilder
::
initRoot
(
const
word
*
defaultValu
e
)
{
internal
::
StructBuilder
MessageBuilder
::
initRoot
(
internal
::
StructSize
siz
e
)
{
internal
::
SegmentBuilder
*
rootSegment
=
getRootSegment
();
return
internal
::
StructBuilder
::
initRoot
(
rootSegment
,
rootSegment
->
getPtrUnchecked
(
0
*
WORDS
),
defaultValu
e
);
rootSegment
,
rootSegment
->
getPtrUnchecked
(
0
*
WORDS
),
siz
e
);
}
internal
::
StructBuilder
MessageBuilder
::
getRoot
(
const
word
*
defaultValu
e
)
{
internal
::
StructBuilder
MessageBuilder
::
getRoot
(
internal
::
StructSize
siz
e
)
{
internal
::
SegmentBuilder
*
rootSegment
=
getRootSegment
();
return
internal
::
StructBuilder
::
getRoot
(
rootSegment
,
rootSegment
->
getPtrUnchecked
(
0
*
WORDS
),
defaultValu
e
);
rootSegment
,
rootSegment
->
getPtrUnchecked
(
0
*
WORDS
),
siz
e
);
}
ArrayPtr
<
const
ArrayPtr
<
const
word
>>
MessageBuilder
::
getSegmentsForOutput
()
{
...
...
c++/src/capnproto/message.h
View file @
f966fc41
...
...
@@ -144,7 +144,7 @@ private:
bool
allocatedArena
;
internal
::
ReaderArena
*
arena
()
{
return
reinterpret_cast
<
internal
::
ReaderArena
*>
(
arenaSpace
);
}
internal
::
StructReader
getRoot
(
const
word
*
defaultValue
);
internal
::
StructReader
getRoot
Internal
(
);
};
class
MessageBuilder
{
...
...
@@ -175,8 +175,8 @@ private:
internal
::
BuilderArena
*
arena
()
{
return
reinterpret_cast
<
internal
::
BuilderArena
*>
(
arenaSpace
);
}
internal
::
SegmentBuilder
*
getRootSegment
();
internal
::
StructBuilder
initRoot
(
const
word
*
defaultValu
e
);
internal
::
StructBuilder
getRoot
(
const
word
*
defaultValu
e
);
internal
::
StructBuilder
initRoot
(
internal
::
StructSize
siz
e
);
internal
::
StructBuilder
getRoot
(
internal
::
StructSize
siz
e
);
};
template
<
typename
RootType
>
...
...
@@ -297,23 +297,22 @@ inline const ReaderOptions& MessageReader::getOptions() {
template
<
typename
RootType
>
inline
typename
RootType
::
Reader
MessageReader
::
getRoot
()
{
return
typename
RootType
::
Reader
(
getRoot
(
RootType
::
DEFAULT
.
words
));
return
typename
RootType
::
Reader
(
getRoot
Internal
(
));
}
template
<
typename
RootType
>
inline
typename
RootType
::
Builder
MessageBuilder
::
initRoot
()
{
return
typename
RootType
::
Builder
(
initRoot
(
RootType
::
DEFAULT
.
words
));
return
typename
RootType
::
Builder
(
initRoot
(
RootType
::
STRUCT_SIZE
));
}
template
<
typename
RootType
>
inline
typename
RootType
::
Builder
MessageBuilder
::
getRoot
()
{
return
typename
RootType
::
Builder
(
getRoot
(
RootType
::
DEFAULT
.
words
));
return
typename
RootType
::
Builder
(
getRoot
(
RootType
::
STRUCT_SIZE
));
}
template
<
typename
RootType
>
typename
RootType
::
Reader
readMessageTrusted
(
const
word
*
data
)
{
return
typename
RootType
::
Reader
(
internal
::
StructReader
::
readRootTrusted
(
data
,
RootType
::
DEFAULT
.
words
));
return
typename
RootType
::
Reader
(
internal
::
StructReader
::
readRootTrusted
(
data
));
}
}
// namespace capnproto
...
...
c++/src/capnproto/test.capnp
View file @
f966fc41
...
...
@@ -67,7 +67,7 @@ struct TestAllTypes {
dataList @30 : List(Data);
structList @31 : List(TestAllTypes);
enumList @32 : List(TestEnum);
interfaceList @33 :
Void
; # TODO
interfaceList @33 :
List(Void)
; # TODO
}
struct TestDefaults {
...
...
compiler/src/CxxGenerator.hs
View file @
f966fc41
...
...
@@ -29,6 +29,7 @@ import qualified Data.ByteString.UTF8 as ByteStringUTF8
import
Data.FileEmbed
(
embedFile
)
import
Data.Word
(
Word8
)
import
qualified
Data.Digest.MD5
as
MD5
import
Data.Binary.IEEE754
(
floatToWord
,
doubleToWord
)
import
Text.Printf
(
printf
)
import
Text.Hastache
import
Text.Hastache.Context
...
...
@@ -106,25 +107,41 @@ cxxFieldSizeString Size64 = "EIGHT_BYTES";
cxxFieldSizeString
SizeReference
=
"REFERENCE"
;
cxxFieldSizeString
(
SizeInlineComposite
_
_
)
=
"INLINE_COMPOSITE"
;
cxxValueString
VoidDesc
=
" ::capnproto::Void::VOID"
cxxValueString
(
BoolDesc
b
)
=
if
b
then
"true"
else
"false"
cxxValueString
(
Int8Desc
i
)
=
show
i
cxxValueString
(
Int16Desc
i
)
=
show
i
cxxValueString
(
Int32Desc
i
)
=
show
i
cxxValueString
(
Int64Desc
i
)
=
show
i
++
"ll"
cxxValueString
(
UInt8Desc
i
)
=
show
i
cxxValueString
(
UInt16Desc
i
)
=
show
i
cxxValueString
(
UInt32Desc
i
)
=
show
i
++
"u"
cxxValueString
(
UInt64Desc
i
)
=
show
i
++
"llu"
cxxValueString
(
Float32Desc
x
)
=
show
x
++
"f"
cxxValueString
(
Float64Desc
x
)
=
show
x
cxxValueString
(
EnumValueValueDesc
v
)
=
cxxTypeString
(
EnumType
$
enumValueParent
v
)
++
"::"
++
toUpperCaseWithUnderscores
(
enumValueName
v
)
cxxValueString
(
TextDesc
_
)
=
error
"No default value literal for aggregate type."
cxxValueString
(
DataDesc
_
)
=
error
"No default value literal for aggregate type."
cxxValueString
(
StructValueDesc
_
)
=
error
"No default value literal for aggregate type."
cxxValueString
(
ListDesc
_
)
=
error
"No default value literal for aggregate type."
isDefaultZero
VoidDesc
=
True
isDefaultZero
(
BoolDesc
b
)
=
not
b
isDefaultZero
(
Int8Desc
i
)
=
i
==
0
isDefaultZero
(
Int16Desc
i
)
=
i
==
0
isDefaultZero
(
Int32Desc
i
)
=
i
==
0
isDefaultZero
(
Int64Desc
i
)
=
i
==
0
isDefaultZero
(
UInt8Desc
i
)
=
i
==
0
isDefaultZero
(
UInt16Desc
i
)
=
i
==
0
isDefaultZero
(
UInt32Desc
i
)
=
i
==
0
isDefaultZero
(
UInt64Desc
i
)
=
i
==
0
isDefaultZero
(
Float32Desc
x
)
=
x
==
0
isDefaultZero
(
Float64Desc
x
)
=
x
==
0
isDefaultZero
(
EnumValueValueDesc
v
)
=
enumValueNumber
v
==
0
isDefaultZero
(
TextDesc
_
)
=
error
"Can't call isDefaultZero on aggregate types."
isDefaultZero
(
DataDesc
_
)
=
error
"Can't call isDefaultZero on aggregate types."
isDefaultZero
(
StructValueDesc
_
)
=
error
"Can't call isDefaultZero on aggregate types."
isDefaultZero
(
ListDesc
_
)
=
error
"Can't call isDefaultZero on aggregate types."
defaultMask
VoidDesc
=
"0"
defaultMask
(
BoolDesc
b
)
=
if
b
then
"true"
else
"false"
defaultMask
(
Int8Desc
i
)
=
show
i
defaultMask
(
Int16Desc
i
)
=
show
i
defaultMask
(
Int32Desc
i
)
=
show
i
defaultMask
(
Int64Desc
i
)
=
show
i
++
"ll"
defaultMask
(
UInt8Desc
i
)
=
show
i
defaultMask
(
UInt16Desc
i
)
=
show
i
defaultMask
(
UInt32Desc
i
)
=
show
i
++
"u"
defaultMask
(
UInt64Desc
i
)
=
show
i
++
"llu"
defaultMask
(
Float32Desc
x
)
=
show
(
floatToWord
x
)
++
"u"
defaultMask
(
Float64Desc
x
)
=
show
(
doubleToWord
x
)
++
"ul"
defaultMask
(
EnumValueValueDesc
v
)
=
show
(
enumValueNumber
v
)
defaultMask
(
TextDesc
_
)
=
error
"Can't call defaultMask on aggregate types."
defaultMask
(
DataDesc
_
)
=
error
"Can't call defaultMask on aggregate types."
defaultMask
(
StructValueDesc
_
)
=
error
"Can't call defaultMask on aggregate types."
defaultMask
(
ListDesc
_
)
=
error
"Can't call defaultMask on aggregate types."
defaultValueBytes
_
(
TextDesc
s
)
=
Just
(
UTF8
.
encode
s
++
[
0
])
defaultValueBytes
_
(
DataDesc
d
)
=
Just
d
...
...
@@ -132,25 +149,6 @@ defaultValueBytes t v@(StructValueDesc _) = Just $ encodeMessage t v
defaultValueBytes
t
v
@
(
ListDesc
_
)
=
Just
$
encodeMessage
t
v
defaultValueBytes
_
_
=
Nothing
cxxDefaultDefault
(
BuiltinType
BuiltinVoid
)
=
" ::capnproto::Void::VOID"
cxxDefaultDefault
(
BuiltinType
BuiltinBool
)
=
"false"
cxxDefaultDefault
(
BuiltinType
BuiltinInt8
)
=
"0"
cxxDefaultDefault
(
BuiltinType
BuiltinInt16
)
=
"0"
cxxDefaultDefault
(
BuiltinType
BuiltinInt32
)
=
"0"
cxxDefaultDefault
(
BuiltinType
BuiltinInt64
)
=
"0"
cxxDefaultDefault
(
BuiltinType
BuiltinUInt8
)
=
"0"
cxxDefaultDefault
(
BuiltinType
BuiltinUInt16
)
=
"0"
cxxDefaultDefault
(
BuiltinType
BuiltinUInt32
)
=
"0"
cxxDefaultDefault
(
BuiltinType
BuiltinUInt64
)
=
"0"
cxxDefaultDefault
(
BuiltinType
BuiltinFloat32
)
=
"0"
cxxDefaultDefault
(
BuiltinType
BuiltinFloat64
)
=
"0"
cxxDefaultDefault
(
BuiltinType
BuiltinText
)
=
"
\"\"
"
cxxDefaultDefault
(
EnumType
desc
)
=
cxxValueString
$
EnumValueValueDesc
$
head
$
enumValues
desc
cxxDefaultDefault
(
BuiltinType
BuiltinData
)
=
error
"No default value literal for aggregate type."
cxxDefaultDefault
(
StructType
_
)
=
error
"No default value literal for aggregate type."
cxxDefaultDefault
(
InterfaceType
_
)
=
error
"No default value literal for aggregate type."
cxxDefaultDefault
(
ListType
_
)
=
error
"No default value literal for aggregate type."
elementType
(
ListType
t
)
=
t
elementType
_
=
error
"Called elementType on non-list."
...
...
@@ -197,9 +195,9 @@ fieldContext parent desc = mkStrContext context where
context
"fieldType"
=
MuVariable
$
cxxTypeString
$
fieldType
desc
context
"fieldBlobType"
=
MuVariable
$
blobTypeString
$
fieldType
desc
context
"fieldOffset"
=
MuVariable
$
fieldOffset
desc
context
"fieldDefault
Value
"
=
case
fieldDefaultValue
desc
of
Just
v
->
MuVariable
$
cxxValueString
v
Nothing
->
MuVariable
$
cxxDefaultDefault
$
fieldType
desc
context
"fieldDefault
Mask
"
=
case
fieldDefaultValue
desc
of
Nothing
->
MuVariable
""
Just
v
->
MuVariable
(
if
isDefaultZero
v
then
""
else
", "
++
defaultMask
v
)
context
"fieldElementSize"
=
MuVariable
$
cxxFieldSizeString
$
elementSize
$
elementType
$
fieldType
desc
context
"fieldElementType"
=
...
...
@@ -212,8 +210,6 @@ structContext parent desc = mkStrContext context where
context
"structDataSize"
=
MuVariable
$
packingDataSize
$
structPacking
desc
context
"structReferenceCount"
=
MuVariable
$
packingReferenceCount
$
structPacking
desc
context
"structChildren"
=
MuList
[]
-- TODO
context
"structDefault"
=
MuList
[
defaultBytesContext
context
(
StructType
desc
)
(
encodeMessage
(
StructType
desc
)
(
StructValueDesc
[]
))]
context
s
=
parent
s
fileContext
desc
=
mkStrContext
context
where
...
...
compiler/src/WireFormat.hs
View file @
f966fc41
...
...
@@ -25,8 +25,7 @@ module WireFormat(encodeMessage) where
import
Data.List
(
sortBy
,
genericLength
,
genericReplicate
)
import
Data.Word
import
Data.Bits
(
shiftL
,
shiftR
,
Bits
,
setBit
)
import
qualified
Data.Set
as
Set
import
Data.Bits
(
shiftL
,
shiftR
,
Bits
,
setBit
,
xor
)
import
Semantics
import
Data.Binary.IEEE754
(
floatToWord
,
doubleToWord
)
import
qualified
Codec.Binary.UTF8.String
as
UTF8
...
...
@@ -69,27 +68,34 @@ encodeDataValue (EnumValueValueDesc v) = bytes (enumValueNumber v) 2
encodeDataValue
(
StructValueDesc
_
)
=
error
"Not fixed-width data."
encodeDataValue
(
ListDesc
_
)
=
error
"Not fixed-width data."
packBits
::
Bits
a
=>
Int
->
[
Bool
]
->
a
encodeMaskedDataValue
v
Nothing
=
encodeDataValue
v
encodeMaskedDataValue
v
(
Just
d
)
=
zipWith
xor
(
encodeDataValue
v
)
(
encodeDataValue
d
)
packBits
::
Bits
a
=>
Int
->
[(
Bool
,
Maybe
Bool
)]
->
a
packBits
_
[]
=
0
packBits
offset
(
True
:
bits
)
=
setBit
(
packBits
(
offset
+
1
)
bits
)
offset
packBits
offset
(
False
:
bits
)
=
packBits
(
offset
+
1
)
bits
packBits
offset
((
True
,
Nothing
)
:
bits
)
=
setBit
(
packBits
(
offset
+
1
)
bits
)
offset
packBits
offset
((
False
,
Nothing
)
:
bits
)
=
packBits
(
offset
+
1
)
bits
packBits
offset
((
b
,
Just
d
)
:
bits
)
=
packBits
offset
((
b
/=
d
,
Nothing
)
:
bits
)
encodeData
::
Integer
->
[(
Integer
,
TypeDesc
,
ValueDesc
)]
->
[
Word8
]
-- The tuples are (offsetInBits, type, value, defaultValue) for each field to encode.
encodeData
::
Integer
->
[(
Integer
,
TypeDesc
,
ValueDesc
,
Maybe
ValueDesc
)]
->
[
Word8
]
encodeData
size
=
loop
0
where
loop
bit
[]
|
bit
==
size
=
[]
loop
bit
[]
|
bit
>
size
=
error
"Data values overran size."
loop
bit
[]
=
0
:
loop
(
bit
+
8
)
[]
loop
bit
rest
@
((
valuePos
,
_
,
BoolDesc
_
)
:
_
)
|
valuePos
==
bit
=
let
loop
bit
rest
@
((
valuePos
,
_
,
BoolDesc
_
,
_
)
:
_
)
|
valuePos
==
bit
=
let
(
bits
,
rest2
)
=
popBits
(
bit
+
8
)
rest
in
packBits
0
bits
:
loop
(
bit
+
8
)
rest2
loop
bit
((
valuePos
,
_
,
value
)
:
rest
)
|
valuePos
==
bit
=
encodeDataValue
value
++
loop
(
bit
+
sizeInBits
(
fieldValueSize
value
))
rest
loop
bit
rest
@
((
valuePos
,
_
,
_
)
:
_
)
|
valuePos
>
bit
=
0
:
loop
(
bit
+
8
)
rest
loop
bit
((
valuePos
,
_
,
value
,
defaultValue
)
:
rest
)
|
valuePos
==
bit
=
encodeMaskedDataValue
value
defaultValue
++
loop
(
bit
+
sizeInBits
(
fieldValueSize
value
))
rest
loop
bit
rest
@
((
valuePos
,
_
,
_
,
_
)
:
_
)
|
valuePos
>
bit
=
0
:
loop
(
bit
+
8
)
rest
loop
_
_
=
error
"Data values were out-of-order."
popBits
limit
((
valuePos
,
_
,
BoolDesc
b
)
:
rest
)
|
valuePos
<
limit
=
let
popBits
limit
((
valuePos
,
_
,
BoolDesc
b
,
d
)
:
rest
)
|
valuePos
<
limit
=
let
(
restBits
,
rest2
)
=
popBits
limit
rest
in
(
b
:
restBits
,
rest2
)
defaultB
=
fmap
(
\
(
BoolDesc
b2
)
->
b2
)
d
in
((
b
,
defaultB
)
:
restBits
,
rest2
)
popBits
_
rest
=
(
[]
,
rest
)
encodeReferences
::
Integer
->
Integer
->
[(
Integer
,
TypeDesc
,
ValueDesc
)]
->
([
Word8
],
[
Word8
])
...
...
@@ -156,10 +162,6 @@ fieldSizeEnum (SizeInlineComposite _ _) = 7
structTag
=
0
listTag
=
1
-- Is this field a non-retroactive member of a union? If so, its default value is not written.
isNonRetroUnionMember
(
FieldDesc
{
fieldNumber
=
n
,
fieldUnion
=
Just
u
})
=
n
>
unionNumber
u
isNonRetroUnionMember
_
=
False
-- What is this union's default tag value? If there is a retroactive field, it is that field's
-- number, otherwise it is the union's number (meaning no field set).
unionDefault
desc
=
UInt8Desc
$
fromIntegral
$
...
...
@@ -168,49 +170,24 @@ unionDefault desc = UInt8Desc $ fromIntegral $
-- childOffset = number of words between the last reference and the location where children will
-- be allocated.
encodeStruct
desc
assignments
childOffset
=
(
dataBytes
,
referenceBytes
,
children
)
where
explicitlyAssignedNums
=
Set
.
fromList
[
fieldNumber
f
|
(
f
,
_
)
<-
assignments
]
explicitlyAssignedUnions
=
Set
.
fromList
[
unionNumber
u
|
(
FieldDesc
{
fieldUnion
=
Just
u
},
_
)
<-
assignments
]
-- Was this field explicitly assigned, or was another member of the same union explicitly
-- assigned? If so, its default value is not written.
isExplicitlyAssigned
(
FieldDesc
{
fieldNumber
=
n
,
fieldUnion
=
u
})
=
Set
.
member
n
explicitlyAssignedNums
||
maybe
False
(
flip
Set
.
member
explicitlyAssignedUnions
.
unionNumber
)
u
-- Values explicitly assigned.
explicitValues
=
[(
fieldOffset
f
,
fieldType
f
,
v
)
|
(
f
,
v
)
<-
assignments
]
-- Values from defaults.
defaultValues
=
[(
o
,
fieldType
field
,
v
)
|
field
@
(
FieldDesc
{
fieldOffset
=
o
,
fieldDefaultValue
=
Just
v
})
<-
structFields
desc
-- Don't include default values for fields that were explicitly assigned.
,
not
$
isExplicitlyAssigned
field
-- Don't encode defaults for union members since they'd overwrite each other, and anyway
-- they wouldn't be valid unless the union tag specified them, which by default it doesn't,
-- except of course in the case of retroactively-added fields. So do include retro fields.
,
not
$
isNonRetroUnionMember
field
-- Don't encode defaults for references. Setting them to null has the same effect.
,
isDataFieldSize
$
fieldValueSize
v
]
explicitValues
=
[(
fieldOffset
f
,
fieldType
f
,
v
,
fieldDefaultValue
f
)
|
(
f
,
v
)
<-
assignments
]
-- Values of union tags.
unionValues
=
[(
unionTagOffset
u
,
BuiltinType
BuiltinUInt8
,
UInt8Desc
$
fromIntegral
n
)
unionValues
=
[(
unionTagOffset
u
,
BuiltinType
BuiltinUInt8
,
UInt8Desc
$
fromIntegral
n
,
Just
$
unionDefault
u
)
|
(
FieldDesc
{
fieldUnion
=
Just
u
,
fieldNumber
=
n
},
_
)
<-
assignments
]
-- Default values of union tags.
unionDefaultValues
=
[(
unionTagOffset
u
,
BuiltinType
BuiltinUInt8
,
unionDefault
u
)
|
u
<-
structUnions
desc
,
not
$
Set
.
member
(
unionNumber
u
)
explicitlyAssignedUnions
]
allValues
=
explicitValues
++
defaultValues
++
unionValues
++
unionDefaultValues
allData
=
[
(
o
*
sizeInBits
(
fieldValueSize
v
),
t
,
v
)
|
(
o
,
t
,
v
)
<-
allValues
,
isDataFieldSize
$
fieldValueSize
v
]
allReferences
=
[
(
o
,
t
,
v
)
|
(
o
,
t
,
v
)
<-
allValues
allValues
=
explicitValues
++
unionValues
allData
=
[
(
o
*
sizeInBits
(
fieldValueSize
v
),
t
,
v
,
d
)
|
(
o
,
t
,
v
,
d
)
<-
allValues
,
isDataFieldSize
$
fieldValueSize
v
]
allReferences
=
[
(
o
,
t
,
v
)
|
(
o
,
t
,
v
,
_
)
<-
allValues
,
not
$
isDataFieldSize
$
fieldValueSize
v
]
sortedData
=
sortBy
compareValues
allData
sortedReferences
=
sortBy
compareValues
allReferences
compareValues
(
o1
,
_
,
_
)
(
o2
,
_
,
_
)
=
compare
o1
o2
sortedData
=
sortBy
compareDataValues
allData
compareDataValues
(
o1
,
_
,
_
,
_
)
(
o2
,
_
,
_
,
_
)
=
compare
o1
o2
sortedReferences
=
sortBy
compareReferenceValues
allReferences
compareReferenceValues
(
o1
,
_
,
_
)
(
o2
,
_
,
_
)
=
compare
o1
o2
dataBytes
=
encodeData
(
packingDataSize
(
structPacking
desc
)
*
64
)
sortedData
(
referenceBytes
,
children
)
=
encodeReferences
childOffset
...
...
@@ -228,7 +205,7 @@ encodeList elementType elements = case elementSize elementType of
(
refBytes
,
childBytes
)
=
encodeReferences
0
(
genericLength
elements
)
$
zipWith
(
\
i
v
->
(
i
,
elementType
,
v
))
[
0
..
]
elements
size
->
encodeData
(
roundUpToMultiple
64
(
genericLength
elements
*
sizeInBits
size
))
$
zipWith
(
\
i
v
->
(
i
*
sizeInBits
size
,
elementType
,
v
))
[
0
..
]
elements
$
zipWith
(
\
i
v
->
(
i
*
sizeInBits
size
,
elementType
,
v
,
Nothing
))
[
0
..
]
elements
encodeMessage
(
StructType
desc
)
(
StructValueDesc
assignments
)
=
let
(
dataBytes
,
refBytes
,
childBytes
)
=
encodeStruct
desc
assignments
0
...
...
compiler/src/c++-header.mustache
View file @
f966fc41
...
...
@@ -41,12 +41,9 @@ struct {{structName}} {
struct
{{
structChildName
}}
;
{{/
structChildren
}}
static constexpr ::capnproto::WordCount DATA_SIZE =
{{
structDataSize
}}
* ::capnproto::WORDS;
static constexpr ::capnproto::WireReferenceCount REFERENCE_COUNT =
{{
structReferenceCount
}}
* ::capnproto::REFERENCES;
{{#
structDefault
}}
static const ::capnproto::internal::AlignedData
<
{{
defaultWordCount
}}
>
DEFAULT;
{{/
structDefault
}}
static constexpr ::capnproto::internal::StructSize STRUCT_SIZE =
::capnproto::internal::StructSize(
{{
structDataSize
}}
* ::capnproto::WORDS,
{{
structReferenceCount
}}
* ::capnproto::REFERENCES);
{{#
structFields
}}
{{#
fieldDefaultBytes
}}
static const ::capnproto::internal::AlignedData
<
{{
defaultWordCount
}}
>
DEFAULT_
{{
fieldUpperCase
}}
;
...
...
@@ -140,7 +137,7 @@ private:
{{#
fieldIsPrimitive
}}
inline
{{
fieldType
}}
{{
structName
}}
::Reader::get
{{
fieldTitleCase
}}
() {
return _reader.getDataField
<
{{
fieldType
}}
>
(
{{
fieldOffset
}}
* ::capnproto::ELEMENTS
,
{{
fieldDefaultValue
}}
);
{{
fieldOffset
}}
* ::capnproto::ELEMENTS
{{
fieldDefaultMask
}}
);
}
{{/
fieldIsPrimitive
}}
{{#
fieldIsBlob
}}
...
...
@@ -159,7 +156,7 @@ inline {{fieldType}}::Reader {{structName}}::Reader::get{{fieldTitleCase}}() {
return
{{
fieldType
}}
::Reader(_reader.getStructField(
{{
fieldOffset
}}
* ::capnproto::REFERENCES,
{{#
fieldDefaultBytes
}}
DEFAULT_
{{
fieldUpperCase
}}
.words
{{/
fieldDefaultBytes
}}
{{^
fieldDefaultBytes
}}
{{
fieldType
}}
::DEFAULT.words
{{/
fieldDefaultBytes
}}
));
{{^
fieldDefaultBytes
}}
nullptr
{{/
fieldDefaultBytes
}}
));
}
{{/
fieldIsStruct
}}
{{#
fieldIsList
}}
...
...
@@ -179,11 +176,12 @@ inline {{fieldType}}::Reader {{structName}}::Reader::get{{fieldTitleCase}}() {
//
{{
structName
}}
.
{{
fieldDecl
}}
{{#
fieldIsPrimitive
}}
inline
{{
fieldType
}}
{{
structName
}}
::Builder::get
{{
fieldTitleCase
}}
() {
return _builder.getDataField
<
{{
fieldType
}}
>
(
{{
fieldOffset
}}
* ::capnproto::ELEMENTS);
return _builder.getDataField
<
{{
fieldType
}}
>
(
{{
fieldOffset
}}
* ::capnproto::ELEMENTS
{{
fieldDefaultMask
}}
);
}
inline void
{{
structName
}}
::Builder::set
{{
fieldTitleCase
}}
(
{{
fieldType
}}
value) {
return _builder.setDataField
<
{{
fieldType
}}
>
(
{{
fieldOffset
}}
* ::capnproto::ELEMENTS, value);
{{
fieldOffset
}}
* ::capnproto::ELEMENTS, value
{{
fieldDefaultMask
}}
);
}
{{/
fieldIsPrimitive
}}
{{#
fieldIsBlob
}}
...
...
@@ -205,14 +203,14 @@ inline {{fieldType}}::Builder {{structName}}::Builder::init{{fieldTitleCase}}(un
{{#
fieldIsStruct
}}
inline
{{
fieldType
}}
::Builder
{{
structName
}}
::Builder::init
{{
fieldTitleCase
}}
() {
return
{{
fieldType
}}
::Builder(_builder.initStructField(
{{
fieldOffset
}}
* ::capnproto::REFERENCES,
{{
fieldType
}}
::
DEFAULT.words
));
{{
fieldOffset
}}
* ::capnproto::REFERENCES,
{{
fieldType
}}
::
STRUCT_SIZE
));
}
inline
{{
fieldType
}}
::Builder
{{
structName
}}
::Builder::get
{{
fieldTitleCase
}}
() {
{{! TODO: Support per-field default values. }}
return
{{
fieldType
}}
::Builder(_builder.getStructField(
{{
fieldOffset
}}
* ::capnproto::REFERENCES,
{{
fieldOffset
}}
* ::capnproto::REFERENCES,
{{
fieldType
}}
::STRUCT_SIZE,
{{#
fieldDefaultBytes
}}
DEFAULT_
{{
fieldUpperCase
}}
.words
{{/
fieldDefaultBytes
}}
{{^
fieldDefaultBytes
}}
{{
fieldType
}}
::DEFAULT.words
{{/
fieldDefaultBytes
}}
));
{{^
fieldDefaultBytes
}}
nullptr
{{/
fieldDefaultBytes
}}
));
}
{{/
fieldIsStruct
}}
{{#
fieldIsNonStructList
}}
...
...
@@ -249,7 +247,7 @@ inline void {{structName}}::Builder::set{{fieldTitleCase}}(
inline
{{
fieldType
}}
::Builder
{{
structName
}}
::Builder::init
{{
fieldTitleCase
}}
(unsigned int size) {
return
{{
fieldType
}}
::Builder(_builder.initStructListField(
{{
fieldOffset
}}
* ::capnproto::REFERENCES, size * ::capnproto::ELEMENTS,
{{
fieldElementType
}}
::
DEFAULT.words
));
{{
fieldElementType
}}
::
STRUCT_SIZE
));
}
inline
{{
fieldType
}}
::Builder
{{
structName
}}
::Builder::get
{{
fieldTitleCase
}}
() {
return
{{
fieldType
}}
::Builder(_builder.getListField(
...
...
compiler/src/c++-source.mustache
View file @
f966fc41
...
...
@@ -32,13 +32,7 @@ namespace {{namespaceName}} {
{{/
fileNamespaces
}}
{{#
fileStructs
}}
constexpr ::capnproto::WordCount
{{
structName
}}
::DATA_SIZE;
constexpr ::capnproto::WireReferenceCount
{{
structName
}}
::REFERENCE_COUNT;
{{#
structDefault
}}
const ::capnproto::internal::AlignedData
<
{{
defaultWordCount
}}
>
{{
structName
}}
::DEFAULT = {
{
{{
defaultByteList
}}
}
};
{{/
structDefault
}}
constexpr ::capnproto::internal::StructSize
{{
structName
}}
::STRUCT_SIZE;
{{#
structFields
}}
{{#
fieldDefaultBytes
}}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment