summaryrefslogtreecommitdiff
path: root/codegen.py
diff options
context:
space:
mode:
authorVlad Ionescu <vlad@rabbitmq.com>2010-07-14 18:28:23 +0100
committerVlad Ionescu <vlad@rabbitmq.com>2010-07-14 18:28:23 +0100
commit7549aa28632606b314c4f2371900320a5a18d0d9 (patch)
tree6c382f61e2c2a1686b8c4fa8321c0f4c6469727f /codegen.py
parentba24177421fecdfb30defb32b46268b1ba60cfcb (diff)
parent507610f99444e187ed7d2c6216d2bbbd0325c138 (diff)
downloadrabbitmq-server-git-7549aa28632606b314c4f2371900320a5a18d0d9.tar.gz
merging in from default
Diffstat (limited to 'codegen.py')
-rw-r--r--codegen.py102
1 files changed, 101 insertions, 1 deletions
diff --git a/codegen.py b/codegen.py
index a24a32ff37..6042e78fcd 100644
--- a/codegen.py
+++ b/codegen.py
@@ -95,6 +95,27 @@ class PackedMethodBitField:
def full(self):
return self.count() == 8
+def multiLineFormat(things, prologue, separator, lineSeparator, epilogue, thingsPerLine = 4):
+ r = [prologue]
+ i = 0
+ for t in things:
+ if i != 0:
+ if i % thingsPerLine == 0:
+ r += [lineSeparator]
+ else:
+ r += [separator]
+ r += [t]
+ i += 1
+ r += [epilogue]
+ return "".join(r)
+
+def prettyType(typeName, subTypes, typesPerLine = 4):
+ """Pretty print a type signature made up of many alternative subtypes"""
+ sTs = multiLineFormat(subTypes,
+ "( ", " | ", "\n | ", " )",
+ thingsPerLine = typesPerLine)
+ return "-type(%s ::\n %s)." % (typeName, sTs)
+
def printFileHeader():
print """%% Autogenerated code. Do not edit.
%%
@@ -317,6 +338,83 @@ def genErl(spec):
-export([lookup_amqp_exception/1]).
-export([amqp_exception/1]).
+"""
+ print "%% Various types"
+ print "-ifdef(use_specs)."
+
+ print """-export_type([amqp_table/0, amqp_property_type/0, amqp_method_record/0,
+ amqp_method_name/0, amqp_method/0, amqp_class_id/0,
+ amqp_value/0, amqp_array/0, amqp_exception/0, amqp_property_record/0]).
+
+-type(amqp_field_type() ::
+ 'longstr' | 'signedint' | 'decimal' | 'timestamp' |
+ 'table' | 'byte' | 'double' | 'float' | 'long' |
+ 'short' | 'bool' | 'binary' | 'void').
+-type(amqp_property_type() ::
+ 'shortstr' | 'longstr' | 'octet' | 'shortint' | 'longint' |
+ 'longlongint' | 'timestamp' | 'bit' | 'table').
+
+-type(amqp_table() :: [{binary(), amqp_field_type(), amqp_value()}]).
+-type(amqp_array() :: [{amqp_field_type(), amqp_value()}]).
+-type(amqp_value() :: binary() | % longstr
+ integer() | % signedint
+ {non_neg_integer(), non_neg_integer()} | % decimal
+ amqp_table() |
+ amqp_array() |
+ byte() | % byte
+ float() | % double
+ integer() | % long
+ integer() | % short
+ boolean() | % bool
+ binary() | % binary
+ 'undefined' | % void
+ non_neg_integer() % timestamp
+ ).
+"""
+
+ print prettyType("amqp_method_name()",
+ [m.erlangName() for m in methods])
+ print prettyType("amqp_method()",
+ ["{%s, %s}" % (m.klass.index, m.index) for m in methods],
+ 6)
+ print prettyType("amqp_method_record()",
+ ["#%s{}" % (m.erlangName()) for m in methods])
+ fieldNames = set()
+ for m in methods:
+ fieldNames.update(m.arguments)
+ fieldNames = [erlangize(f.name) for f in fieldNames]
+ print prettyType("amqp_method_field_name()",
+ fieldNames)
+ print prettyType("amqp_property_record()",
+ ["#'P_%s'{}" % erlangize(c.name) for c in spec.allClasses()])
+ print prettyType("amqp_exception()",
+ ["'%s'" % erlangConstantName(c).lower() for (c, v, cls) in spec.constants])
+ print prettyType("amqp_exception_code()",
+ ["%i" % v for (c, v, cls) in spec.constants])
+ classIds = set()
+ for m in spec.allMethods():
+ classIds.add(m.klass.index)
+ print prettyType("amqp_class_id()",
+ ["%i" % ci for ci in classIds])
+ print "-endif. % use_specs"
+
+ print """
+%% Method signatures
+-ifdef(use_specs).
+-spec(lookup_method_name/1 :: (amqp_method()) -> amqp_method_name()).
+-spec(method_id/1 :: (amqp_method_name()) -> amqp_method()).
+-spec(method_has_content/1 :: (amqp_method_name()) -> boolean()).
+-spec(is_method_synchronous/1 :: (amqp_method_record()) -> boolean()).
+-spec(method_record/1 :: (amqp_method_name()) -> amqp_method_record()).
+-spec(method_fieldnames/1 :: (amqp_method_name()) -> [amqp_method_field_name()]).
+-spec(decode_method_fields/2 :: (amqp_method_name(), binary()) -> amqp_method_record()).
+-spec(decode_properties/2 :: (non_neg_integer(), binary()) -> amqp_property_record()).
+-spec(encode_method_fields/1 :: (amqp_method_record()) -> binary()).
+-spec(encode_properties/1 :: (amqp_method_record()) -> binary()).
+-spec(lookup_amqp_exception/1 :: (amqp_exception()) -> {boolean(), amqp_exception_code(), binary()}).
+-spec(amqp_exception/1 :: (amqp_exception_code()) -> amqp_exception()).
+-endif. % use_specs
+
bitvalue(true) -> 1;
bitvalue(false) -> 0;
bitvalue(undefined) -> 0.
@@ -397,6 +495,7 @@ def genHrl(spec):
for c in spec.allClasses():
print "-record('P_%s', {%s})." % (erlangize(c.name), fieldNameList(c.fields))
+
def generateErl(specPath):
genErl(AmqpSpec(specPath))
@@ -404,5 +503,6 @@ def generateHrl(specPath):
genHrl(AmqpSpec(specPath))
if __name__ == "__main__":
- do_main(generateHrl, generateErl)
+ do_main_dict({"header": generateHrl,
+ "body": generateErl})