aboutsummaryrefslogtreecommitdiff
path: root/flatcc/config/config.h
blob: b07fa6cc8b4ca12e2732da4975ffdc423614f704 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#ifndef CONFIG_H
#define CONFIG_H

#include "flatcc/flatcc_version.h"

/*
 * This is an alternative implementation to googles `flatc` compiler.
 * This configuration enables or disable features which can be used
 * to closer match the flatc behavior. The current flatc version
 * may evolve over time, so the comments on flatc behavior should
 * be seen as how things were when this file was last updated.
 *
 * One notable, but convenient, difference is that structs and enums are
 * not required to be defined in a specific order here (as of June 2020
 * flatc supports out of order enums). The schema can be rearranged by
 * the user to support the flatc compiler without changing the
 * underlying. This does put an extra restraint on struct depth as
 * recursion must have set maximum level, but this also applies to
 * target C compilers, e.g. clang has a limit of about 100-200 nestings.
 *
 * Another more significant difference is that structs are allowed as
 * root objects. This is important if a query should return a partial
 * result from a stored buffer but it isn't currently supported by
 * flatc, although the documentation hints that it might be possible.
 *
 * Value overflows and conversions of enums and default values are
 * checked more than by flatc, and this could break some schemas.
 * However, hopefully it will not break any production schemas since the
 * results would not be well-defined.
 *
 * Struct fields may be deprecated by padding. This isn't supported by
 * flatc but was added because it seemed inconsistent to not have it
 * when deprecated table fields are possible.
 *
 * Enums can be used in structs, but this is not supported by flatc.
 * The reason might be either the additional work in the parser, or
 * because it adds a burden to code generators, for example, C output
 * must place enums before structs. However, they make perfect sense in
 * the format.
 */

#define FLATCC_INFO_TEXT "FlatBuffers schema compiler for C by dvide.com"
#define FLATCC_TITLE_TEXT "flatcc " FLATCC_INFO_TEXT
#define FLATCC_GENERATED_BY "Generated by flatcc " FLATCC_VERSION_TEXT " " FLATCC_INFO_TEXT

/* Include portability layer here since all other files depend on it. */
#ifdef FLATCC_PORTABLE
#include "flatcc/portable/portable.h"
#endif

/*
 * Limit the input buffer or file size being parsed to prevent brining
 * the whole system down on invalid or malicous content. This limit
 * is covers the accumulated size of all included files. 0 is unlimited.
 */
#ifndef FLATCC_MAX_SCHEMA_SIZE
#define FLATCC_MAX_SCHEMA_SIZE 1000000
#endif

/*
 * Max nesting level of includes - the affects recursion on the call
 * stack. 0 is unlimited.
 */
#ifndef FLATCC_MAX_INCLUDE_DEPTH
#define FLATCC_MAX_INCLUDE_DEPTH 100
#endif

/*
 * Max number of include files allowed regardless of how they are
 * nested. This affects memory usage but it may be very large. It should
 * not normally affect the number of open file handles since files are
 * opened and closed individually, depending on the interfacing driver
 * logic. 0 is unlimited.
 */
#ifndef FLATCC_MAX_INCLUDE_COUNT
#define FLATCC_MAX_INCLUDE_COUNT 100
#endif

/*
 * Limit parser recursion depth when analyzing dependent structs.
 * The grammar itself does not contain recursion.
 */
#ifndef FLATCC_NESTING_MAX
#define FLATCC_NESTING_MAX 100
#endif

#ifndef FLATCC_MAX_ERRORS
#define FLATCC_MAX_ERRORS 10
#endif

/* Used when truncating some error messages. */
#ifndef FLATCC_MAX_IDENT_SHOW
#define FLATCC_MAX_IDENT_SHOW 50
#endif

/*
 * Allow FlatBuffer schema reserved keywords
 * to be used as field identifiers. This can
 * be confusing, but also important for use with
 * JSON where the field names are visible
 * externally.
 */
#ifndef FLATCC_ALLOW_KW_FIELDS
#define FLATCC_ALLOW_KW_FIELDS 1
#endif

/*
 * Applies to the members of an enum. These
 * names a visible in JSON. Using reserved
 * names prevent them from being used in
 * field default values.
 */
#ifndef FLATCC_ALLOW_KW_ENUMS
#define FLATCC_ALLOW_KW_ENUMS 1
#endif

/*
 * Artificial limit on grammar attributes to prevent abuse.
 */
#ifndef FLATCC_ATTR_MAX
#define FLATCC_ATTR_MAX 100
#endif

#ifndef FLATCC_FORCE_ALIGN_MAX
#define FLATCC_FORCE_ALIGN_MAX 256
#endif

/*
 * sizeof(uoffset_t), sizeof(soffset_t) in FlatBuffers file format.
 * Permissible values are 2, 4 (default), and 8.
 */
#ifndef FLATCC_OFFSET_SIZE
#define FLATCC_OFFSET_SIZE 4
#endif

/*
 * sizeof(voffset_t) in FlatBuffers file format.
 * Permissible values are 2 (default), 4, and 8.
 */
#ifndef FLATCC_VOFFSET_SIZE
#define FLATCC_VOFFSET_SIZE 2
#endif

/*
 * DO NOT CHANGE
 * The union type field type.
 * This is hardcoded in many places and difficult
 * to change since size = 1 does not require
 * endian conversion. Config only placed here
 * for consistency and clarity.
 */
#ifndef FLATCC_UTYPE_SIZE
#define FLATCC_UTYPE_SIZE 1
#endif

/*
 * DO NOT CHANGE, see also FLATCC_UTYPE_SIZE
 */
#ifndef FLATCC_BOOL_SIZE
#define FLATCC_BOOL_SIZE 1
#endif

/*
 * There are no hard limits on structs, but
 * they cannot be effectively handled by tables
 * if larger than 64K and 64K-1 allows us to store
 * the size in short where needed. Internally
 * we can handle 64 bits.
 */
#ifndef FLATCC_STRUCT_MAX_SIZE
#define FLATCC_STRUCT_MAX_SIZE ((1 << 16) - 1)
#endif

#ifndef FLATCC_NAMESPACE_MAX
#define FLATCC_NAMESPACE_MAX 100
#endif

/* Reserve space for trailing _ and \0. */
#define FLATCC_NAMESPACE_BUFSIZ (FLATCC_NAMESPACE_MAX + 2)
#define FLATCC_NAME_BUFSIZ (2 * FLATCC_NAMESPACE_BUFSIZ)

#ifndef FLATCC_FILENAME_MAX
#define FLATCC_FILENAME_MAX 256
#endif

/*
 * If set, enum fields initialized with a numeric constant must match an
 * existing enum value. This only applies to the schema intializers, not
 * values assigned during buffer construction. It may be useful to allow
 * non-existing values (and expensive to enforce), but for schema
 * intializers, it could easily break future changes if inconsistent
 * values are being used.
 */
#ifndef FLATCC_STRICT_ENUM_INIT
#define FLATCC_STRICT_ENUM_INIT 1
#endif

/*
 * flatc requires ascending enums, and it does simplify code generation
 * to multiple languages, and name mapping of enums, so we require this
 * by default, but C code generator handles both cases fine.
 * As of June 2020 flatc supports out of order enums.
 */
#ifndef FLATCC_ASCENDING_ENUM
#define FLATCC_ASCENDING_ENUM 0
#endif

/*
 * flatc tool accepts any integer as bool because it
 * doesn't do range check. By enabling this flag we get more compatible,
 * but still only accepts 1 or 0 as bool, and also allow assignment of
 * bool to other types (useful in enums that are not bool because
 * official flatc has no boolean enum type.
 *
 * However, there appears to be no strong reason for mixing boolean types
 * with other types, but since `flatc` supports, it is enabled by
 * default.
 */
#ifndef FLATCC_ALLOW_BOOLEAN_CONVERSION
#define FLATCC_ALLOW_BOOLEAN_CONVERSION 1
#endif

/* flatc does not recognize later defined enums, be we do by default. */
#ifndef FLATCC_HIDE_LATER_ENUM
#define FLATCC_HIDE_LATER_ENUM 0
#endif

/*
 * The flatc parser does not recognize later structs.
 * If disabled dependent structs will be topologically
 * sorted to the set maximum recursion depth.
 */
#ifndef FLATCC_HIDE_LATER_STRUCT
#define FLATCC_HIDE_LATER_STRUCT 0
#endif

/*
 * In all cases we permit at most one root type per file.
 * flatc allows none, or multiple, but apparently only generates
 * accessors for the last seen.
 */
#ifndef FLATCC_REQUIRE_ROOT_TYPE
#define FLATCC_REQUIRE_ROOT_TYPE 0
#endif

/*
 * This also applies to nested_flatbuffers.
 * flatc does not allow structs.
 */
#ifndef FLATCC_ALLOW_STRUCT_ROOT
#define FLATCC_ALLOW_STRUCT_ROOT 1
#endif

/*
 * pad deprecated struct fields with __depracedx[size] i C output.
 * flatc does not permit deprecated struct fields.
 */
#ifndef FLATCC_ALLOW_STRUCT_FIELD_DEPRECATE
#define FLATCC_ALLOW_STRUCT_FIELD_DEPRECATE 1
#endif

/*
 * flatc only allows one key on table fields, which makes sense in
 * C++ STL where a vector can have on comparison operator,
 * but in C output we can easily have multiple <structname>_find_by_<fieldname>.
 * Vectors can only be sorted by one field at a time, but e.g. an
 * octree- or trie-like data-structure might choose to sort on different
 * coordinates at different levels.
 */
#ifndef FLATCC_ALLOW_MULTIPLE_KEY_FIELDS
#define FLATCC_ALLOW_MULTIPLE_KEY_FIELDS 1
#endif

/*
 * flatc does not have a primary_key field attribute but it is useful
 * when having multiple keys. Without a primary_key the field with the
 * lowest id becomes the default, or primary, key. This key has a
 * simpler find syntax and is also used by default when sorting.
 * When there is only one keyed field on a table it is better to use
 * the key attribute for compatibility reasons. It will automatically
 * become primary.
 */
#ifndef FLATCC_ALLOW_PRIMARY_KEY
#define FLATCC_ALLOW_PRIMARY_KEY 1
#endif

/*
 * flatcc has scan functions which are equivalent to find except they
 * don't require arrays to be sorted by field and have O(N) complexity.
 * It makes sense to generate these functions by default for all fields,
 * not just ones marked as key.
 */

#ifndef FLATCC_ALLOW_SCAN_FOR_ALL_FIELDS
#define FLATCC_ALLOW_SCAN_FOR_ALL_FIELDS 1
#endif

/* flatc currently does not allow keys on structs, but it makes sense to have them. */
#ifndef FLATCC_ALLOW_STRUCT_FIELD_KEY
#define FLATCC_ALLOW_STRUCT_FIELD_KEY 1
#endif

/*
 * Structs cannot have defaults. If an enum does not define a 0 value
 * this is not considered an error when used with a struct.
 * The flatc compiler does not accept enums on structs.
 */
#ifndef FLATCC_ALLOW_ENUM_STRUCT_FIELD
#define FLATCC_ALLOW_ENUM_STRUCT_FIELD 1
#endif

/*
 * flatc currrently only allows scalar and strings as keys,
 * but enums are really a kind of scalar and it makes sense to support
 * them.
 */
#ifndef FLATCC_ALLOW_ENUM_KEY
#define FLATCC_ALLOW_ENUM_KEY 1
#endif

/*
 * String keys are allowed (and actively used in refletion.fbs) by
 * flatc, but they are more tricky to support in code generation
 * so allow them to be disabled.
 */
#ifndef FLATCC_ALLOW_STRING_KEY
#define FLATCC_ALLOW_STRING_KEY 1
#endif

/* Code gen specific. */

#ifndef FLATCC_DEFAULT_SCHEMA_EXT
#define FLATCC_DEFAULT_SCHEMA_EXT ".fbs"
#endif

#ifndef FLATCC_DEFAULT_BIN_SCHEMA_EXT
#define FLATCC_DEFAULT_BIN_SCHEMA_EXT ".bfbs"
#endif

/* Schema file extensions do not carry a dot by convention, do the same here. */
#ifndef FLATCC_DEFAULT_BIN_EXT
#define FLATCC_DEFAULT_BIN_EXT "bin"
#endif

#ifndef FLATCC_DEFAULT_DEP_EXT
#define FLATCC_DEFAULT_DEP_EXT ".fbs.d"
#endif

/*
 * A depends file sometimes needs a single file to hook
 * the include file dependencies onto.
 */
#ifndef FLATCC_DEFAULT_DEP_TARGET_SUFFIX
#define FLATCC_DEFAULT_DEP_TARGET_SUFFIX "_reader.h"
#endif

#ifndef FLATCC_DEFAULT_FILENAME
#define FLATCC_DEFAULT_FILENAME "test"
#endif

#ifndef FLATCC_DEFAULT_NAMESPACE
#define FLATCC_DEFAULT_NAMESPACE ""
#endif

#ifndef FLATCC_DEFAULT_NAMESPACE_COMMON
#define FLATCC_DEFAULT_NAMESPACE_COMMON "flatbuffers"
#endif

/* rpc attributes aren't spec'ed at this point, so we flag the code. */
/* Similar to attributes after table name (no runtime option). */
#ifndef FLATCC_ALLOW_RPC_SERVICE_ATTRIBUTES
#define FLATCC_ALLOW_RPC_ATTRIBUTES 1
#endif
 
/* Similar to attributes after a tables field name (no runtime option). */
#ifndef FLATCC_ALLOW_RPC_METHOD_ATTRIBUTES
#define FLATCC_ALLOW_RPC_METHOD_ATTRIBUTES 1
#endif

/* Only takes effect if rpc method attributes are allowed (no runtime option). */
#ifndef FLATCC_ALLOW_DEPRECATED_RPC_METHOD
#define FLATCC_ALLOW_DEPRECATED_RPC_METHOD 1
#endif


/*
 * This inserts #pragma pack(1) and padding fields instead
 * of using alignas on field members.
 */
#define FLATCC_CGEN_PAD 0


/* ---- CGEN are flagss specific the C code generator. ---- */

/*
 * Generate support code for sorting arrays.
 */
#ifndef FLATCC_CGEN_SORT
#define FLATCC_CGEN_SORT 1
#endif

/* Disable warnings for known compilers */
#ifndef FLATCC_CGEN_PRAGMAS
#define FLATCC_CGEN_PRAGMAS 1
#endif

/* Default spacing when generating auto idented code. */
#ifndef FLATCC_CGEN_SPACING
#define FLATCC_CGEN_SPACING 4
#endif

/*
 * Field names risk conflict with other names when
 * generated symbols do not all have a suffix.
 * This can avoid by only generating accessors
 * with the `_get` suffix, rather than both with
 * and without like:
 * `Monster_name(monster)`
 * `Monster_name_get(monster)`.
 *
 * Note: test code might break if enabled.
 */
#ifndef FLATCC_CGEN_NO_CONFLICTS
#define FLATCC_CGEN_NO_CONFLICTS 0
#endif

/* ---- BGEN are flags specific to the binary schema generator. ---- */

/*
 * If disabled, no binary schema support is compiled in.
 * This may be useful when generection reflection headers
 * are no longer compatible with the current build. Disabling
 * reflection makes it possible to still build the compiler and
 * thus generate new reflection headers.
 *
 * This flag is enabled by the build system so files can also be
 * excluded or included.
 *
 * #ifndef FLATCC_REFLECTION
 * #define FLATCC_REFLECTION 0
 * #endif
*/

/* Export option for --schema. */
#ifndef FLATCC_BGEN_BFBS
#define FLATCC_BGEN_BFBS 0
#endif

/*
 * Add uoffset length field before buffer. This is not a valid buffer
 * then but can be used to outfileenate multiple files and it is
 * compatible with the layout of nested buffers that have a similar
 * prefix. The prefix is a uoffset_t field.
 */
#ifndef FLATCC_BGEN_LENGTH_PREFIX
#define FLATCC_BGEN_LENGTH_PREFIX 0
#endif

/* Prefix type names with namespace during schema export.
 * flatc does not include the namespace prefix. */
#ifndef FLATCC_BGEN_QUALIFY_NAMES
#define FLATCC_BGEN_QUALIFY_NAMES 1
#endif


/* ---- JSON related code generation. ---- */

/*
 * Translate enum values to symbolic names. Ditto union types.
 * This is also a runtime option but disabling it here uses
 * a faster code path.
 */
#ifndef FLATCC_JSON_PRINT_MAP_ENUMS
#define FLATCC_JSON_PRINT_MAP_ENUMS 1
#endif

#endif /* CONFIG_H */