Debian Codegraph @

c/src/protocol.h.py

raw
1 #!/usr/bin/python
2 #
3 # Licensed to the Apache Software Foundation (ASF) under one
4 # or more contributor license agreements. See the NOTICE file
5 # distributed with this work for additional information
6 # regarding copyright ownership. The ASF licenses this file
7 # to you under the Apache License, Version 2.0 (the
8 # "License"); you may not use this file except in compliance
9 # with the License. You may obtain a copy of the License at
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing,
14 # software distributed under the License is distributed on an
15 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 # KIND, either express or implied. See the License for the
17 # specific language governing permissions and limitations
18 # under the License.
19 #
20
21 from __future__ import print_function
22 from protocol import *
23
24 print("/* generated */")
25 print("#ifndef _PROTON_PROTOCOL_H")
26 print("#define _PROTON_PROTOCOL_H 1")
27 print()
28 print("#include \"proton/type_compat.h\"")
29
30 fields = {}
31
32 for type in TYPES:
33 fidx = 0
34 for f in type.query["field"]:
35 print("#define %s_%s (%s)" % (field_kw(type), field_kw(f), fidx))
36 fidx += 1
37 d = f["@default"]
38 if d:
39 ft = ftype(f)
40 # Don't bother to emit a boolean default that is False
41 if ft == "boolean" and d == "false":
42 continue
43 # Don't output non numerics unless symbol
44 # We should really fully resolve to actual restricted value
45 # this is really true for symbols too which accidentally work
46 if ft == "symbol":
47 d = '"' + d + '"'
48 elif d[0] not in '0123456789':
49 continue
50 print("#define %s_%s_DEFAULT (%s) /* %s */" % (field_kw(type), field_kw(f), d, ft))
51
52 idx = 0
53
54 for type in TYPES:
55 desc = type["descriptor"]
56 name = type["@name"].upper().replace("-", "_")
57 print("#define %s_SYM (\"%s\")" % (name, desc["@name"]))
58 hi, lo = [int(x, 0) for x in desc["@code"].split(":")]
59 code = (hi << 32) + lo
60 print("#define %s ((uint64_t) %s)" % (name, code))
61 fields[code] = (type["@name"], [f["@name"] for f in type.query["field"]])
62 idx += 1
63
64 print("""
65 #include <stddef.h>
66
67 typedef struct {
68 const unsigned char name_index;
69 const unsigned char first_field_index;
70 const unsigned char field_count;
71 } pn_fields_t;
72
73 extern const pn_fields_t FIELDS[];
74 extern const uint16_t FIELD_NAME[];
75 extern const uint16_t FIELD_FIELDS[];
76 """)
77
78 print('struct FIELD_STRINGS {')
79 print(' const char STRING0[sizeof("")];')
80 strings = set()
81 for name, fnames in fields.values():
82 strings.add(name)
83 strings.update(fnames)
84 for str in strings:
85 istr = str.replace("-", "_")
86 print(' const char FIELD_STRINGS_%s[sizeof("%s")];' % (istr, str))
87 print("};")
88 print()
89
90 print("extern const struct FIELD_STRINGS FIELD_STRINGPOOL;")
91 print("#ifdef DEFINE_FIELDS")
92 print()
93
94 print('const struct FIELD_STRINGS FIELD_STRINGPOOL = {')
95 print(' "",')
96 for str in strings:
97 print(' "%s",' % str)
98 print("};")
99 print()
100 print("/* This is an array of offsets into FIELD_STRINGPOOL */")
101 print("const uint16_t FIELD_NAME[] = {")
102 print(" offsetof(struct FIELD_STRINGS, STRING0),")
103 index = 1
104 for i in range(256):
105 if i in fields:
106 name, fnames = fields[i]
107 iname = name.replace("-", "_")
108 print(' offsetof(struct FIELD_STRINGS, FIELD_STRINGS_%s), /* %d */' % (iname, index))
109 index += 1
110 print("};")
111
112 print("/* This is an array of offsets into FIELD_STRINGPOOL */")
113 print("const uint16_t FIELD_FIELDS[] = {")
114 print(" offsetof(struct FIELD_STRINGS, STRING0),")
115 index = 1
116 for i in range(256):
117 if i in fields:
118 name, fnames = fields[i]
119 if fnames:
120 for f in fnames:
121 ifname = f.replace("-", "_")
122 print(' offsetof(struct FIELD_STRINGS, FIELD_STRINGS_%s), /* %d (%s) */' % (ifname, index, name))
123 index += 1
124 print("};")
125
126 print("const pn_fields_t FIELDS[] = {")
127
128 name_count = 1
129 field_count = 1
130 field_min = 256
131 field_max = 0
132 for i in range(256):
133 if i in fields:
134 if i > field_max:
135 field_max = i
136 if i < field_min:
137 field_min = i
138
139 for i in range(field_min, field_max + 1):
140 if i in fields:
141 name, fnames = fields[i]
142 if fnames:
143 print(' {%d, %d, %d}, /* %d (%s) */' % (name_count, field_count, len(fnames), i, name))
144 field_count += len(fnames)
145 else:
146 print(' {%d, 0, 0}, /* %d (%s) */' % (name_count, i, name))
147 name_count += 1
148 if i > field_max:
149 field_max = i
150 if i < field_min:
151 field_min = i
152 else:
153 print(' {0, 0, 0}, /* %d */' % i)
154
155 print("};")
156 print()
157 print("#endif")
158 print()
159 print('enum {')
160 print(' FIELD_MIN = %d,' % field_min)
161 print(' FIELD_MAX = %d' % field_max)
162 print('};')
163 print()
164 print("#endif /* protocol.h */")