~ubuntu-branches/ubuntu/saucy/rabbitmq-server/saucy

« back to all changes in this revision

Viewing changes to codegen.py

  • Committer: Package Import Robot
  • Author(s): Emile Joubert
  • Date: 2012-11-19 11:42:31 UTC
  • mfrom: (0.2.18) (0.1.32 sid)
  • Revision ID: package-import@ubuntu.com-20121119114231-hvapkn4akng09etr
Tags: 3.0.0-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
import string
25
25
import re
26
26
 
27
 
erlangTypeMap = {
28
 
    'octet': 'octet',
29
 
    'shortstr': 'shortstr',
30
 
    'longstr': 'longstr',
31
 
    'short': 'shortint',
32
 
    'long': 'longint',
33
 
    'longlong': 'longlongint',
34
 
    'bit': 'bit',
35
 
    'table': 'table',
36
 
    'timestamp': 'timestamp',
37
 
}
38
 
 
39
27
# Coming up with a proper encoding of AMQP tables in JSON is too much
40
28
# hassle at this stage. Given that the only default value we are
41
29
# interested in is for the empty table, we only support that.
123
111
 
124
112
def genErl(spec):
125
113
    def erlType(domain):
126
 
        return erlangTypeMap[spec.resolveDomain(domain)]
 
114
        return erlangize(spec.resolveDomain(domain))
127
115
 
128
116
    def fieldTypeList(fields):
129
117
        return '[' + ', '.join([erlType(f.domain) for f in fields]) + ']'
186
174
            return p+'Len:32/unsigned, '+p+':'+p+'Len/binary'
187
175
        elif type == 'octet':
188
176
            return p+':8/unsigned'
189
 
        elif type == 'shortint':
 
177
        elif type == 'short':
190
178
            return p+':16/unsigned'
191
 
        elif type == 'longint':
 
179
        elif type == 'long':
192
180
            return p+':32/unsigned'
193
 
        elif type == 'longlongint':
 
181
        elif type == 'longlong':
194
182
            return p+':64/unsigned'
195
183
        elif type == 'timestamp':
196
184
            return p+':64/unsigned'
233
221
        def presentBin(fields):
234
222
            ps = ', '.join(['P' + str(f.index) + ':1' for f in fields])
235
223
            return '<<' + ps + ', _:%d, R0/binary>>' % (16 - len(fields),)
236
 
        def mkMacroName(field):
237
 
            return '?' + field.domain.upper() + '_PROP'
238
 
        def writePropFieldLine(field, bin_next = None):
 
224
        def writePropFieldLine(field):
239
225
            i = str(field.index)
240
 
            if not bin_next:
241
 
                bin_next = 'R' + str(field.index + 1)
242
 
            if field.domain in ['octet', 'timestamp']:
243
 
                print ("  {%s, %s} = %s(%s, %s, %s, %s)," %
244
 
                       ('F' + i, bin_next, mkMacroName(field), 'P' + i,
245
 
                        'R' + i, 'I' + i, 'X' + i))
 
226
            if field.domain == 'bit':
 
227
                print "  {F%s, R%s} = {P%s =/= 0, R%s}," % \
 
228
                    (i, str(field.index + 1), i, i)
246
229
            else:
247
 
                print ("  {%s, %s} = %s(%s, %s, %s, %s, %s)," %
248
 
                       ('F' + i, bin_next, mkMacroName(field), 'P' + i,
249
 
                        'R' + i, 'L' + i, 'S' + i, 'X' + i))
 
230
                print "  {F%s, R%s} = if P%s =:= 0 -> {undefined, R%s}; true -> ?%s_VAL(R%s, L%s, V%s, X%s) end," % \
 
231
                    (i, str(field.index + 1), i, i, erlType(field.domain).upper(), i, i, i, i)
250
232
 
251
233
        if len(c.fields) == 0:
252
 
            print "decode_properties(%d, _) ->" % (c.index,)
 
234
            print "decode_properties(%d, <<>>) ->" % (c.index,)
253
235
        else:
254
236
            print ("decode_properties(%d, %s) ->" %
255
237
                   (c.index, presentBin(c.fields)))
256
 
            for field in c.fields[:-1]:
 
238
            for field in c.fields:
257
239
                writePropFieldLine(field)
258
 
            writePropFieldLine(c.fields[-1], "<<>>")
 
240
            print "  <<>> = %s," % ('R' + str(len(c.fields)))
259
241
        print "  #'P_%s'{%s};" % (erlangize(c.name), fieldMapList(c.fields))
260
242
 
261
243
    def genFieldPreprocessing(packed):
283
265
        print "  <<%s>>;" % (', '.join([methodFieldFragment(f) for f in packedFields]))
284
266
 
285
267
    def genEncodeProperties(c):
 
268
        def presentBin(fields):
 
269
            ps = ', '.join(['P' + str(f.index) + ':1' for f in fields])
 
270
            return '<<' + ps + ', 0:%d>>' % (16 - len(fields),)
 
271
        def writePropFieldLine(field):
 
272
            i = str(field.index)
 
273
            if field.domain == 'bit':
 
274
                print "  {P%s, R%s} = {F%s =:= 1, R%s}," % \
 
275
                    (i, str(field.index + 1), i, i)
 
276
            else:
 
277
                print "  {P%s, R%s} = if F%s =:= undefined -> {0, R%s}; true -> {1, [?%s_PROP(F%s, L%s) | R%s]} end," % \
 
278
                    (i, str(field.index + 1), i, i, erlType(field.domain).upper(), i, i, i)
 
279
 
286
280
        print "encode_properties(#'P_%s'{%s}) ->" % (erlangize(c.name), fieldMapList(c.fields))
287
 
        print "  rabbit_binary_generator:encode_properties(%s, %s);" % \
288
 
              (fieldTypeList(c.fields), fieldTempList(c.fields))
 
281
        if len(c.fields) == 0:
 
282
            print "  <<>>;"
 
283
        else:
 
284
            print "  R0 = [<<>>],"
 
285
            for field in c.fields:
 
286
                writePropFieldLine(field)
 
287
            print "  list_to_binary([%s | lists:reverse(R%s)]);" % \
 
288
                (presentBin(c.fields), str(len(c.fields)))
289
289
 
290
290
    def messageConstantClass(cls):
291
291
        # We do this because 0.8 uses "soft error" and 8.1 uses "soft-error".
350
350
      'table' | 'byte' | 'double' | 'float' | 'long' |
351
351
      'short' | 'bool' | 'binary' | 'void' | 'array').
352
352
-type(amqp_property_type() ::
353
 
      'shortstr' | 'longstr' | 'octet' | 'shortint' | 'longint' |
354
 
      'longlongint' | 'timestamp' | 'bit' | 'table').
 
353
      'shortstr' | 'longstr' | 'octet' | 'short' | 'long' |
 
354
      'longlong' | 'timestamp' | 'bit' | 'table').
355
355
 
356
356
-type(amqp_table() :: [{binary(), amqp_field_type(), amqp_value()}]).
357
357
-type(amqp_array() :: [{amqp_field_type(), amqp_value()}]).
429
429
        _                   -> exit(method_field_shortstr_overflow)
430
430
    end.
431
431
 
432
 
-define(SHORTSTR_PROP(P, R, L, S, X),
433
 
        if P =:= 0 -> {undefined, R};
434
 
           true    -> <<L:8/unsigned, S:L/binary, X/binary>> = R,
435
 
                      {S, X}
436
 
        end).
437
 
-define(TABLE_PROP(P, R, L, T, X),
438
 
        if P =:= 0 -> {undefined, R};
439
 
           true    -> <<L:32/unsigned, T:L/binary, X/binary>> = R,
440
 
                      {rabbit_binary_parser:parse_table(T), X}
441
 
        end).
442
 
-define(OCTET_PROP(P, R, I, X),
443
 
        if P =:= 0 -> {undefined, R};
444
 
           true    -> <<I:8/unsigned, X/binary>> = R,
445
 
                      {I, X}
446
 
        end).
447
 
-define(TIMESTAMP_PROP(P, R, I, X),
448
 
        if P =:= 0 -> {undefined, R};
449
 
           true    -> <<I:64/unsigned, X/binary>> = R,
450
 
                      {I, X}
 
432
-define(SHORTSTR_VAL(R, L, V, X),
 
433
        begin
 
434
            <<L:8/unsigned, V:L/binary, X/binary>> = R,
 
435
            {V, X}
 
436
        end).
 
437
 
 
438
-define(LONGSTR_VAL(R, L, V, X),
 
439
        begin
 
440
            <<L:32/unsigned, V:L/binary, X/binary>> = R,
 
441
            {V, X}
 
442
        end).
 
443
 
 
444
-define(SHORT_VAL(R, L, V, X),
 
445
        begin
 
446
            <<V:8/unsigned, X/binary>> = R,
 
447
            {V, X}
 
448
        end).
 
449
 
 
450
-define(LONG_VAL(R, L, V, X),
 
451
        begin
 
452
            <<V:32/unsigned, X/binary>> = R,
 
453
            {V, X}
 
454
        end).
 
455
 
 
456
-define(LONGLONG_VAL(R, L, V, X),
 
457
        begin
 
458
            <<V:64/unsigned, X/binary>> = R,
 
459
            {V, X}
 
460
        end).
 
461
 
 
462
-define(OCTET_VAL(R, L, V, X),
 
463
        begin
 
464
            <<V:8/unsigned, X/binary>> = R,
 
465
            {V, X}
 
466
        end).
 
467
 
 
468
-define(TABLE_VAL(R, L, V, X),
 
469
        begin
 
470
            <<L:32/unsigned, V:L/binary, X/binary>> = R,
 
471
            {rabbit_binary_parser:parse_table(V), X}
 
472
        end).
 
473
 
 
474
-define(TIMESTAMP_VAL(R, L, V, X),
 
475
        begin
 
476
            <<V:64/unsigned, X/binary>> = R,
 
477
            {V, X}
 
478
        end).
 
479
 
 
480
-define(SHORTSTR_PROP(X, L),
 
481
        begin
 
482
            L = size(X),
 
483
            if L < 256 -> <<L:8, X:L/binary>>;
 
484
               true    -> exit(content_properties_shortstr_overflow)
 
485
            end
 
486
        end).
 
487
 
 
488
-define(LONGSTR_PROP(X, L),
 
489
        begin
 
490
            L = size(X),
 
491
            <<L:32, X:L/binary>>
 
492
        end).
 
493
 
 
494
-define(OCTET_PROP(X, L),     <<X:8/unsigned>>).
 
495
-define(SHORT_PROP(X, L),     <<X:16/unsigned>>).
 
496
-define(LONG_PROP(X, L),      <<X:32/unsigned>>).
 
497
-define(LONGLONG_PROP(X, L),  <<X:64/unsigned>>).
 
498
-define(TIMESTAMP_PROP(X, L), <<X:64/unsigned>>).
 
499
 
 
500
-define(TABLE_PROP(X, T),
 
501
        begin
 
502
            T = rabbit_binary_generator:generate_table(X),
 
503
            <<(size(T)):32, T/binary>>
451
504
        end).
452
505
"""
453
506
    version = "{%d, %d, %d}" % (spec.major, spec.minor, spec.revision)
497
550
    print "amqp_exception(_Code) -> undefined."
498
551
 
499
552
def genHrl(spec):
500
 
    def erlType(domain):
501
 
        return erlangTypeMap[spec.resolveDomain(domain)]
502
 
 
503
553
    def fieldNameList(fields):
504
554
        return ', '.join([erlangize(f.name) for f in fields])
505
555