codegen.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. # Copyright 2007 - 2021, Alan Antonuk and the rabbitmq-c contributors.
  2. # SPDX-License-Identifier: mit
  3. from amqp_codegen import *
  4. import string
  5. import re
  6. class Emitter(object):
  7. """An object the trivially emits generated code lines.
  8. This largely exists to be wrapped by more sophisticated emitter
  9. classes.
  10. """
  11. def __init__(self, prefix):
  12. self.prefix = prefix
  13. def emit(self, line):
  14. """Emit a line of generated code."""
  15. print(self.prefix + line)
  16. class BitDecoder(object):
  17. """An emitter object that keeps track of the state involved in
  18. decoding the AMQP bit type."""
  19. def __init__(self, emitter):
  20. self.emitter = emitter
  21. self.bit = 0
  22. def emit(self, line):
  23. self.bit = 0
  24. self.emitter.emit(line)
  25. def decode_bit(self, lvalue):
  26. """Generate code to decode a value of the AMQP bit type into
  27. the given lvalue."""
  28. if self.bit == 0:
  29. self.emitter.emit("if (!amqp_decode_8(encoded, &offset, &bit_buffer)) return AMQP_STATUS_BAD_AMQP_DATA;")
  30. self.emitter.emit("%s = (bit_buffer & (1 << %d)) ? 1 : 0;"
  31. % (lvalue, self.bit))
  32. self.bit += 1
  33. if self.bit == 8:
  34. self.bit = 0
  35. class BitEncoder(object):
  36. """An emitter object that keeps track of the state involved in
  37. encoding the AMQP bit type."""
  38. def __init__(self, emitter):
  39. self.emitter = emitter
  40. self.bit = 0
  41. def flush(self):
  42. """Flush the state associated with AMQP bit types."""
  43. if self.bit:
  44. self.emitter.emit("if (!amqp_encode_8(encoded, &offset, bit_buffer)) return AMQP_STATUS_BAD_AMQP_DATA;")
  45. self.bit = 0
  46. def emit(self, line):
  47. self.flush()
  48. self.emitter.emit(line)
  49. def encode_bit(self, value):
  50. """Generate code to encode a value of the AMQP bit type from
  51. the given value."""
  52. if self.bit == 0:
  53. self.emitter.emit("bit_buffer = 0;")
  54. self.emitter.emit("if (%s) bit_buffer |= (1 << %d);"
  55. % (value, self.bit))
  56. self.bit += 1
  57. if self.bit == 8:
  58. self.flush()
  59. class SimpleType(object):
  60. """A AMQP type that corresponds to a simple scalar C value of a
  61. certain width."""
  62. def __init__(self, bits):
  63. self.bits = bits
  64. self.ctype = "uint%d_t" % (bits,)
  65. def decode(self, emitter, lvalue):
  66. emitter.emit("if (!amqp_decode_%d(encoded, &offset, &%s)) return AMQP_STATUS_BAD_AMQP_DATA;" % (self.bits, lvalue))
  67. def encode(self, emitter, value):
  68. emitter.emit("if (!amqp_encode_%d(encoded, &offset, %s)) return AMQP_STATUS_BAD_AMQP_DATA;" % (self.bits, value))
  69. def literal(self, value):
  70. return value
  71. class StrType(object):
  72. """The AMQP shortstr or longstr types."""
  73. def __init__(self, lenbits):
  74. self.lenbits = lenbits
  75. self.ctype = "amqp_bytes_t"
  76. def decode(self, emitter, lvalue):
  77. emitter.emit("{")
  78. emitter.emit(" uint%d_t len;" % (self.lenbits,))
  79. emitter.emit(" if (!amqp_decode_%d(encoded, &offset, &len)" % (self.lenbits,))
  80. emitter.emit(" || !amqp_decode_bytes(encoded, &offset, &%s, len))" % (lvalue,))
  81. emitter.emit(" return AMQP_STATUS_BAD_AMQP_DATA;")
  82. emitter.emit("}")
  83. def encode(self, emitter, value):
  84. emitter.emit("if (UINT%d_MAX < %s.len" % (self.lenbits, value))
  85. emitter.emit(" || !amqp_encode_%d(encoded, &offset, (uint%d_t)%s.len)" %
  86. (self.lenbits, self.lenbits, value))
  87. emitter.emit(" || !amqp_encode_bytes(encoded, &offset, %s))" % (value,))
  88. emitter.emit(" return AMQP_STATUS_BAD_AMQP_DATA;")
  89. def literal(self, value):
  90. if value != '':
  91. raise NotImplementedError()
  92. return "amqp_empty_bytes"
  93. class BitType(object):
  94. """The AMQP bit type."""
  95. def __init__(self):
  96. self.ctype = "amqp_boolean_t"
  97. def decode(self, emitter, lvalue):
  98. emitter.decode_bit(lvalue)
  99. def encode(self, emitter, value):
  100. emitter.encode_bit(value)
  101. def literal(self, value):
  102. return {True: 1, False: 0}[value]
  103. class TableType(object):
  104. """The AMQP table type."""
  105. def __init__(self):
  106. self.ctype = "amqp_table_t"
  107. def decode(self, emitter, lvalue):
  108. emitter.emit("{")
  109. emitter.emit(" int res = amqp_decode_table(encoded, pool, &(%s), &offset);" % (lvalue,))
  110. emitter.emit(" if (res < 0) return res;")
  111. emitter.emit("}")
  112. def encode(self, emitter, value):
  113. emitter.emit("{")
  114. emitter.emit(" int res = amqp_encode_table(encoded, &(%s), &offset);" % (value,))
  115. emitter.emit(" if (res < 0) return res;")
  116. emitter.emit("}")
  117. def literal(self, value):
  118. raise NotImplementedError()
  119. types = {
  120. 'octet': SimpleType(8),
  121. 'short': SimpleType(16),
  122. 'long': SimpleType(32),
  123. 'longlong': SimpleType(64),
  124. 'shortstr': StrType(8),
  125. 'longstr': StrType(32),
  126. 'bit': BitType(),
  127. 'table': TableType(),
  128. 'timestamp': SimpleType(64),
  129. }
  130. def typeFor(spec, f):
  131. """Get a representation of the AMQP type of a field."""
  132. return types[spec.resolveDomain(f.domain)]
  133. def c_ize(s):
  134. s = s.replace('-', '_')
  135. s = s.replace(' ', '_')
  136. return s
  137. # When generating API functions corresponding to synchronous methods,
  138. # we need some information that isn't in the protocol def: Some
  139. # methods should not be exposed, indicated here by a False value.
  140. # Some methods should be exposed but certain fields should not be
  141. # exposed as parameters.
  142. apiMethodInfo = {
  143. "amqp_connection_start": False, # application code should not use this
  144. "amqp_connection_secure": False, # application code should not use this
  145. "amqp_connection_tune": False, # application code should not use this
  146. "amqp_connection_open": False, # application code should not use this
  147. "amqp_connection_close": False, # needs special handling
  148. "amqp_channel_open": ["out_of_band"],
  149. "amqp_channel_close": False, # needs special handling
  150. "amqp_access_request": False, # huh?
  151. "amqp_basic_get": False, # get-ok has content
  152. }
  153. # When generating API functions corresponding to synchronous methods,
  154. # some fields should be suppressed everywhere. This dict names those
  155. # fields, and the fixed values to use for them.
  156. apiMethodsSuppressArgs = {"ticket": 0, "nowait": False}
  157. AmqpMethod.defName = lambda m: cConstantName(c_ize(m.klass.name) + '_' + c_ize(m.name) + "_method")
  158. AmqpMethod.fullName = lambda m: "amqp_%s_%s" % (c_ize(m.klass.name), c_ize(m.name))
  159. AmqpMethod.structName = lambda m: m.fullName() + "_t"
  160. AmqpClass.structName = lambda c: "amqp_" + c_ize(c.name) + "_properties_t"
  161. def methodApiPrototype(m):
  162. fn = m.fullName()
  163. info = apiMethodInfo.get(fn, [])
  164. docs = "/**\n * %s\n *\n" % (fn)
  165. docs += " * @param [in] state connection state\n"
  166. docs += " * @param [in] channel the channel to do the RPC on\n"
  167. args = []
  168. for f in m.arguments:
  169. n = c_ize(f.name)
  170. if n in apiMethodsSuppressArgs or n in info:
  171. continue
  172. args.append(", ")
  173. args.append(typeFor(m.klass.spec, f).ctype)
  174. args.append(" ")
  175. args.append(n)
  176. docs += " * @param [in] %s %s\n" % (n, n)
  177. docs += " * @returns %s_ok_t\n" % (fn)
  178. docs += " */\n"
  179. return "%sAMQP_EXPORT\n%s_ok_t *\nAMQP_CALL %s(amqp_connection_state_t state, amqp_channel_t channel%s)" % (docs, fn, fn, ''.join(args))
  180. AmqpMethod.apiPrototype = methodApiPrototype
  181. def cConstantName(s):
  182. return 'AMQP_' + '_'.join(re.split('[- ]', s.upper()))
  183. def cFlagName(c, f):
  184. return cConstantName(c.name + '_' + f.name) + '_FLAG'
  185. def genErl(spec):
  186. def fieldTempList(fields):
  187. return '[' + ', '.join(['F' + str(f.index) for f in fields]) + ']'
  188. def fieldMapList(fields):
  189. return ', '.join([c_ize(f.name) + " = F" + str(f.index) for f in fields])
  190. def genLookupMethodName(m):
  191. print(' case %s: return "%s";' % (m.defName(), m.defName()))
  192. def genDecodeMethodFields(m):
  193. print(" case %s: {" % (m.defName(),))
  194. print(" %s *m = (%s *) amqp_pool_alloc(pool, sizeof(%s));" % \
  195. (m.structName(), m.structName(), m.structName()))
  196. print(" if (m == NULL) { return AMQP_STATUS_NO_MEMORY; }")
  197. emitter = BitDecoder(Emitter(" "))
  198. for f in m.arguments:
  199. typeFor(spec, f).decode(emitter, "m->"+c_ize(f.name))
  200. print(" *decoded = m;")
  201. print(" return 0;")
  202. print(" }")
  203. def genDecodeProperties(c):
  204. print(" case %d: {" % (c.index,))
  205. print(" %s *p = (%s *) amqp_pool_alloc(pool, sizeof(%s));" % \
  206. (c.structName(), c.structName(), c.structName()))
  207. print(" if (p == NULL) { return AMQP_STATUS_NO_MEMORY; }")
  208. print(" p->_flags = flags;")
  209. emitter = Emitter(" ")
  210. for f in c.fields:
  211. emitter.emit("if (flags & %s) {" % (cFlagName(c, f),))
  212. typeFor(spec, f).decode(emitter, "p->"+c_ize(f.name))
  213. emitter.emit("}")
  214. print(" *decoded = p;")
  215. print(" return 0;")
  216. print(" }")
  217. def genEncodeMethodFields(m):
  218. print(" case %s: {" % (m.defName(),))
  219. if m.arguments:
  220. print(" %s *m = (%s *) decoded;" % (m.structName(), m.structName()))
  221. emitter = BitEncoder(Emitter(" "))
  222. for f in m.arguments:
  223. typeFor(spec, f).encode(emitter, "m->"+c_ize(f.name))
  224. emitter.flush()
  225. print(" return (int)offset;")
  226. print(" }")
  227. def genEncodeProperties(c):
  228. print(" case %d: {" % (c.index,))
  229. if c.fields:
  230. print(" %s *p = (%s *) decoded;" % (c.structName(), c.structName()))
  231. emitter = Emitter(" ")
  232. for f in c.fields:
  233. emitter.emit(" if (flags & %s) {" % (cFlagName(c, f),))
  234. typeFor(spec, f).encode(emitter, "p->"+c_ize(f.name))
  235. emitter.emit("}")
  236. print(" return (int)offset;")
  237. print(" }")
  238. methods = spec.allMethods()
  239. print("""// Copyright 2007 - 2021, Alan Antonuk and the rabbitmq-c contributors.
  240. // SPDX-License-Identifier: mit
  241. /* Generated code. Do not edit. Edit and re-run codegen.py instead. */
  242. #ifdef HAVE_CONFIG_H
  243. #include "config.h"
  244. #endif
  245. #include "amqp_private.h"
  246. #include <stdint.h>
  247. #include <stdio.h>
  248. #include <stdlib.h>
  249. #include <string.h>
  250. """)
  251. print("""
  252. char const *amqp_constant_name(int constantNumber) {
  253. switch (constantNumber) {""")
  254. for (c,v,cls) in spec.constants:
  255. print(" case %s: return \"%s\";" % (cConstantName(c), cConstantName(c)))
  256. print(""" default: return "(unknown)";
  257. }
  258. }""")
  259. print("""
  260. amqp_boolean_t amqp_constant_is_hard_error(int constantNumber) {
  261. switch (constantNumber) {""")
  262. for (c,v,cls) in spec.constants:
  263. if cls == 'hard-error':
  264. print(" case %s: return 1;" % (cConstantName(c),))
  265. print(""" default: return 0;
  266. }
  267. }""")
  268. print("""
  269. char const *amqp_method_name(amqp_method_number_t methodNumber) {
  270. switch (methodNumber) {""")
  271. for m in methods: genLookupMethodName(m)
  272. print(""" default: return NULL;
  273. }
  274. }""")
  275. print("""
  276. amqp_boolean_t amqp_method_has_content(amqp_method_number_t methodNumber) {
  277. switch (methodNumber) {""")
  278. for m in methods:
  279. if m.hasContent:
  280. print(' case %s: return 1;' % (m.defName()))
  281. print(""" default: return 0;
  282. }
  283. }""")
  284. print("""
  285. int amqp_decode_method(amqp_method_number_t methodNumber,
  286. amqp_pool_t *pool,
  287. amqp_bytes_t encoded,
  288. void **decoded)
  289. {
  290. size_t offset = 0;
  291. uint8_t bit_buffer;
  292. switch (methodNumber) {""")
  293. for m in methods: genDecodeMethodFields(m)
  294. print(""" default: return AMQP_STATUS_UNKNOWN_METHOD;
  295. }
  296. }""")
  297. print("""
  298. int amqp_decode_properties(uint16_t class_id,
  299. amqp_pool_t *pool,
  300. amqp_bytes_t encoded,
  301. void **decoded)
  302. {
  303. size_t offset = 0;
  304. amqp_flags_t flags = 0;
  305. int flagword_index = 0;
  306. uint16_t partial_flags;
  307. do {
  308. if (!amqp_decode_16(encoded, &offset, &partial_flags))
  309. return AMQP_STATUS_BAD_AMQP_DATA;
  310. flags |= (partial_flags << (flagword_index * 16));
  311. flagword_index++;
  312. } while (partial_flags & 1);
  313. switch (class_id) {""")
  314. for c in spec.allClasses(): genDecodeProperties(c)
  315. print(""" default: return AMQP_STATUS_UNKNOWN_CLASS;
  316. }
  317. }""")
  318. print("""
  319. int amqp_encode_method(amqp_method_number_t methodNumber,
  320. void *decoded,
  321. amqp_bytes_t encoded)
  322. {
  323. size_t offset = 0;
  324. uint8_t bit_buffer;
  325. switch (methodNumber) {""")
  326. for m in methods: genEncodeMethodFields(m)
  327. print(""" default: return AMQP_STATUS_UNKNOWN_METHOD;
  328. }
  329. }""")
  330. print("""
  331. int amqp_encode_properties(uint16_t class_id,
  332. void *decoded,
  333. amqp_bytes_t encoded)
  334. {
  335. size_t offset = 0;
  336. /* Cheat, and get the flags out generically, relying on the
  337. similarity of structure between classes */
  338. amqp_flags_t flags = * (amqp_flags_t *) decoded; /* cheating! */
  339. {
  340. /* We take a copy of flags to avoid destroying it, as it is used
  341. in the autogenerated code below. */
  342. amqp_flags_t remaining_flags = flags;
  343. do {
  344. amqp_flags_t remainder = remaining_flags >> 16;
  345. uint16_t partial_flags = remaining_flags & 0xFFFE;
  346. if (remainder != 0) { partial_flags |= 1; }
  347. if (!amqp_encode_16(encoded, &offset, partial_flags))
  348. return AMQP_STATUS_BAD_AMQP_DATA;
  349. remaining_flags = remainder;
  350. } while (remaining_flags != 0);
  351. }
  352. switch (class_id) {""")
  353. for c in spec.allClasses(): genEncodeProperties(c)
  354. print(""" default: return AMQP_STATUS_UNKNOWN_CLASS;
  355. }
  356. }""")
  357. for m in methods:
  358. if not m.isSynchronous:
  359. continue
  360. info = apiMethodInfo.get(m.fullName(), [])
  361. if info is False:
  362. continue
  363. print("")
  364. print(m.apiPrototype())
  365. print("{")
  366. print(" %s req;" % (m.structName(),))
  367. for f in m.arguments:
  368. n = c_ize(f.name)
  369. val = apiMethodsSuppressArgs.get(n)
  370. if val is None and n in info:
  371. val = f.defaultvalue
  372. if val is None:
  373. val = n
  374. else:
  375. val = typeFor(spec, f).literal(val)
  376. print(" req.%s = %s;" % (n, val))
  377. reply = cConstantName(c_ize(m.klass.name) + '_' + c_ize(m.name)
  378. + "_ok_method")
  379. print("""
  380. return amqp_simple_rpc_decoded(state, channel, %s, %s, &req);
  381. }
  382. """ % (m.defName(), reply))
  383. def genHrl(spec):
  384. def fieldDeclList(fields):
  385. if fields:
  386. return ''.join([" %s %s; /**< %s */\n" % (typeFor(spec, f).ctype,
  387. c_ize(f.name), f.name)
  388. for f in fields])
  389. else:
  390. return " char dummy; /**< Dummy field to avoid empty struct */\n"
  391. def propDeclList(fields):
  392. return ''.join([" %s %s;\n" % (typeFor(spec, f).ctype, c_ize(f.name))
  393. for f in fields
  394. if spec.resolveDomain(f.domain) != 'bit'])
  395. methods = spec.allMethods()
  396. print("""// Copyright 2007 - 2021, Alan Antonuk and the rabbitmq-c contributors.
  397. // SPDX-License-Identifier: mit
  398. /* Generated code. Do not edit. Edit and re-run codegen.py instead. */
  399. /** @file rabbitmq-c/framing.h */
  400. #ifndef RABBITMQ_C_FRAMING_H
  401. #define RABBITMQ_C_FRAMING_H
  402. #include <rabbitmq-c/amqp.h>
  403. #include <rabbitmq-c/export.h>
  404. AMQP_BEGIN_DECLS
  405. """)
  406. print("#define AMQP_PROTOCOL_VERSION_MAJOR %d /**< AMQP protocol version major */" % (spec.major))
  407. print("#define AMQP_PROTOCOL_VERSION_MINOR %d /**< AMQP protocol version minor */" % (spec.minor))
  408. print("#define AMQP_PROTOCOL_VERSION_REVISION %d /**< AMQP protocol version revision */" % (spec.revision))
  409. print("#define AMQP_PROTOCOL_PORT %d /**< Default AMQP Port */" % (spec.port))
  410. for (c,v,cls) in spec.constants:
  411. print("#define %s %s /**< Constant: %s */" % (cConstantName(c), v, c))
  412. print("")
  413. print("""/* Function prototypes. */
  414. /**
  415. * Get constant name string from constant
  416. *
  417. * @param [in] constantNumber constant to get the name of
  418. * @returns string describing the constant. String is managed by
  419. * the library and should not be free()'d by the program
  420. */
  421. AMQP_EXPORT
  422. char const *
  423. AMQP_CALL amqp_constant_name(int constantNumber);
  424. /**
  425. * Checks to see if a constant is a hard error
  426. *
  427. * A hard error occurs when something severe enough
  428. * happens that the connection must be closed.
  429. *
  430. * @param [in] constantNumber the error constant
  431. * @returns true if its a hard error, false otherwise
  432. */
  433. AMQP_EXPORT
  434. amqp_boolean_t
  435. AMQP_CALL amqp_constant_is_hard_error(int constantNumber);
  436. /**
  437. * Get method name string from method number
  438. *
  439. * @param [in] methodNumber the method number
  440. * @returns method name string. String is managed by the library
  441. * and should not be freed()'d by the program
  442. */
  443. AMQP_EXPORT
  444. char const *
  445. AMQP_CALL amqp_method_name(amqp_method_number_t methodNumber);
  446. /**
  447. * Check whether a method has content
  448. *
  449. * A method that has content will receive the method frame
  450. * a properties frame, then 1 to N body frames
  451. *
  452. * @param [in] methodNumber the method number
  453. * @returns true if method has content, false otherwise
  454. */
  455. AMQP_EXPORT
  456. amqp_boolean_t
  457. AMQP_CALL amqp_method_has_content(amqp_method_number_t methodNumber);
  458. /**
  459. * Decodes a method from AMQP wireformat
  460. *
  461. * @param [in] methodNumber the method number for the decoded parameter
  462. * @param [in] pool the memory pool to allocate the decoded method from
  463. * @param [in] encoded the encoded byte string buffer
  464. * @param [out] decoded pointer to the decoded method struct
  465. * @returns 0 on success, an error code otherwise
  466. */
  467. AMQP_EXPORT
  468. int
  469. AMQP_CALL amqp_decode_method(amqp_method_number_t methodNumber,
  470. amqp_pool_t *pool,
  471. amqp_bytes_t encoded,
  472. void **decoded);
  473. /**
  474. * Decodes a header frame properties structure from AMQP wireformat
  475. *
  476. * @param [in] class_id the class id for the decoded parameter
  477. * @param [in] pool the memory pool to allocate the decoded properties from
  478. * @param [in] encoded the encoded byte string buffer
  479. * @param [out] decoded pointer to the decoded properties struct
  480. * @returns 0 on success, an error code otherwise
  481. */
  482. AMQP_EXPORT
  483. int
  484. AMQP_CALL amqp_decode_properties(uint16_t class_id,
  485. amqp_pool_t *pool,
  486. amqp_bytes_t encoded,
  487. void **decoded);
  488. /**
  489. * Encodes a method structure in AMQP wireformat
  490. *
  491. * @param [in] methodNumber the method number for the decoded parameter
  492. * @param [in] decoded the method structure (e.g., amqp_connection_start_t)
  493. * @param [in] encoded an allocated byte buffer for the encoded method
  494. * structure to be written to. If the buffer isn't large enough
  495. * to hold the encoded method, an error code will be returned.
  496. * @returns 0 on success, an error code otherwise.
  497. */
  498. AMQP_EXPORT
  499. int
  500. AMQP_CALL amqp_encode_method(amqp_method_number_t methodNumber,
  501. void *decoded,
  502. amqp_bytes_t encoded);
  503. /**
  504. * Encodes a properties structure in AMQP wireformat
  505. *
  506. * @param [in] class_id the class id for the decoded parameter
  507. * @param [in] decoded the properties structure (e.g., amqp_basic_properties_t)
  508. * @param [in] encoded an allocated byte buffer for the encoded properties to written to.
  509. * If the buffer isn't large enough to hold the encoded method, an
  510. * an error code will be returned
  511. * @returns 0 on success, an error code otherwise.
  512. */
  513. AMQP_EXPORT
  514. int
  515. AMQP_CALL amqp_encode_properties(uint16_t class_id,
  516. void *decoded,
  517. amqp_bytes_t encoded);
  518. """)
  519. print("/* Method field records. */\n")
  520. for m in methods:
  521. methodid = m.klass.index << 16 | m.index
  522. print("#define %s ((amqp_method_number_t) 0x%.08X) /**< %s.%s method id @internal %d, %d; %d */" % \
  523. (m.defName(),
  524. methodid,
  525. m.klass.name,
  526. m.name,
  527. m.klass.index,
  528. m.index,
  529. methodid))
  530. print("/** %s.%s method fields */\ntypedef struct %s_ {\n%s} %s;\n" % \
  531. (m.klass.name, m.name, m.structName(), fieldDeclList(m.arguments), m.structName()))
  532. print("/* Class property records. */")
  533. for c in spec.allClasses():
  534. print("#define %s (0x%.04X) /**< %s class id @internal %d */" % \
  535. (cConstantName(c.name + "_class"), c.index, c.name, c.index))
  536. index = 0
  537. for f in c.fields:
  538. if index % 16 == 15:
  539. index = index + 1
  540. shortnum = index // 16
  541. partialindex = 15 - (index % 16)
  542. bitindex = shortnum * 16 + partialindex
  543. print('#define %s (1 << %d) /**< %s.%s property flag */' % (cFlagName(c, f), bitindex, c.name, f.name))
  544. index = index + 1
  545. print("/** %s class properties */\ntypedef struct %s_ {\n amqp_flags_t _flags; /**< bit-mask of set fields */\n%s} %s;\n" % \
  546. (c.name,
  547. c.structName(),
  548. fieldDeclList(c.fields),
  549. c.structName()))
  550. print("/* API functions for methods */\n")
  551. for m in methods:
  552. if m.isSynchronous and apiMethodInfo.get(m.fullName()) is not False:
  553. print("%s;" % (m.apiPrototype(),))
  554. print("""
  555. AMQP_END_DECLS
  556. #endif /* RABBITMQ_C_FRAMING_H */""")
  557. def generateErl(specPath):
  558. genErl(AmqpSpec(specPath))
  559. def generateHrl(specPath):
  560. genHrl(AmqpSpec(specPath))
  561. if __name__ == "__main__":
  562. do_main(generateHrl, generateErl)