summaryrefslogtreecommitdiff
path: root/codegen.py
diff options
context:
space:
mode:
Diffstat (limited to 'codegen.py')
-rw-r--r--codegen.py70
1 files changed, 56 insertions, 14 deletions
diff --git a/codegen.py b/codegen.py
index 2ff7842031..6c36753c9c 100644
--- a/codegen.py
+++ b/codegen.py
@@ -227,7 +227,7 @@ def genErl(spec):
print " {F%s, R%s} = {P%s =/= 0, R%s}," % \
(i, str(field.index + 1), i, i)
else:
- print " {F%s, R%s} = if P%s =:= 0 -> {undefined, R%s}; true -> %s_prop(R%s) end," % \
+ print " {F%s, R%s} = if P%s =:= 0 -> {undefined, R%s}; true -> %s_val(R%s) end," % \
(i, str(field.index + 1), i, i, erlType(field.domain), i)
if len(c.fields) == 0:
@@ -265,9 +265,27 @@ def genErl(spec):
print " <<%s>>;" % (', '.join([methodFieldFragment(f) for f in packedFields]))
def genEncodeProperties(c):
+ def presentBin(fields):
+ ps = ', '.join(['P' + str(f.index) + ':1' for f in fields])
+ return '<<' + ps + ', 0:%d>>' % (16 - len(fields),)
+ def writePropFieldLine(field):
+ i = str(field.index)
+ if field.domain == 'bit':
+ print " {P%s, R%s} = {F%s =:= 1, R%s}," % \
+ (i, str(field.index + 1), i, i)
+ else:
+ print " {P%s, R%s} = if F%s =:= undefined -> {0, R%s}; true -> {1, [?%s_PROP(F%s) | R%s]} end," % \
+ (i, str(field.index + 1), i, i, erlType(field.domain).upper(), i, i)
+
print "encode_properties(#'P_%s'{%s}) ->" % (erlangize(c.name), fieldMapList(c.fields))
- print " rabbit_binary_generator:encode_properties(%s, %s);" % \
- (fieldTypeList(c.fields), fieldTempList(c.fields))
+ if len(c.fields) == 0:
+ print " <<>>;"
+ else:
+ print " R0 = <<>>,"
+ for field in c.fields:
+ writePropFieldLine(field)
+ print " list_to_binary([%s | lists:reverse(R%s)]);" % \
+ (presentBin(c.fields), str(len(c.fields)))
def messageConstantClass(cls):
# We do this because 0.8 uses "soft error" and 8.1 uses "soft-error".
@@ -413,26 +431,50 @@ shortstr_size(S) ->
%% use of these functions by the codec depends on the protocol spec
-compile({nowarn_unused_function,
- [{shortstr_prop, 1}, {longstr_prop, 1},
- {short_prop, 1}, {long_prop, 1}, {longlong_prop, 1},
- {octet_prop, 1}, {table_prop, 1}, {timestamp_prop, 1}]}).
+ [{shortstr_val, 1}, {longstr_val, 1},
+ {short_val, 1}, {long_val, 1}, {longlong_val, 1},
+ {octet_val, 1}, {table_val, 1}, {timestamp_val, 1},
+ {shortstr_prop, 1}, {longstr_prop, 1}, {table_prop, 1}]}).
-shortstr_prop(<<L:8/unsigned, S:L/binary, X/binary>>) -> {S, X}.
+shortstr_val(<<L:8/unsigned, S:L/binary, X/binary>>) -> {S, X}.
-longstr_prop(<<L:32/unsigned, S:L/binary, X/binary>>) -> {S, X}.
+longstr_val(<<L:32/unsigned, S:L/binary, X/binary>>) -> {S, X}.
-short_prop(<<I:8/unsigned, X/binary>>) -> {I, X}.
+short_val(<<I:8/unsigned, X/binary>>) -> {I, X}.
-long_prop(<<I:32/unsigned, X/binary>>) -> {I, X}.
+long_val(<<I:32/unsigned, X/binary>>) -> {I, X}.
-longlong_prop(<<I:64/unsigned, X/binary>>) -> {I, X}.
+longlong_val(<<I:64/unsigned, X/binary>>) -> {I, X}.
-octet_prop(<<I:8/unsigned, X/binary>>) -> {I, X}.
+octet_val(<<I:8/unsigned, X/binary>>) -> {I, X}.
-table_prop(<<L:32/unsigned, T:L/binary, X/binary>>) ->
+table_val(<<L:32/unsigned, T:L/binary, X/binary>>) ->
{rabbit_binary_parser:parse_table(T), X}.
-timestamp_prop(<<I:64/unsigned, X/binary>>) -> {I, X}.
+timestamp_val(<<I:64/unsigned, X/binary>>) -> {I, X}.
+
+shortstr_prop(S) ->
+ L = size(S),
+ if L < 256 -> <<L:8, S:L/binary>>;
+ true -> exit(content_properties_shortstr_overflow)
+ end.
+
+longstr_prop(S) ->
+ L = size(S),
+ <<L:32, S:L/binary>>.
+
+table_prop(T) ->
+ BinT = rabbit_binary_generator:generate_table(T),
+ <<(size(BinT)):32, BinT/binary>>.
+
+-define(SHORTSTR_PROP(X), shortstr_prop(X)).
+-define(LONGSTR_PROP(X), longstr_prop(X)).
+-define(OCTET_PROP(X), <<X:8/unsigned>>).
+-define(SHORT_PROP(X), <<X:16/unsigned>>).
+-define(LONG_PROP(X), <<X:32/unsigned>>).
+-define(LONGLONG_PROP(X), <<X:64/unsigned>>).
+-define(TIMESTAMP_PROP(X), <<X:64/unsigned>>).
+-define(TABLE_PROP(X), table_prop(X)).
"""
version = "{%d, %d, %d}" % (spec.major, spec.minor, spec.revision)
if version == '{8, 0, 0}': version = '{0, 8, 0}'