blob: 6afdc7a51d5ecffdeda5ceff0128932a6998d4d4 [file] [log] [blame]
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001/*
2 * Copyright 2014 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef FLATBUFFERS_IDL_H_
18#define FLATBUFFERS_IDL_H_
19
20#include <map>
21#include <memory>
22#include <stack>
23
24#include "flatbuffers/base.h"
25#include "flatbuffers/flatbuffers.h"
26#include "flatbuffers/flexbuffers.h"
27#include "flatbuffers/hash.h"
28#include "flatbuffers/reflection.h"
29
30#if !defined(FLATBUFFERS_CPP98_STL)
31# include <functional>
32#endif // !defined(FLATBUFFERS_CPP98_STL)
33
34// This file defines the data types representing a parsed IDL (Interface
35// Definition Language) / schema file.
36
37// Limits maximum depth of nested objects.
38// Prevents stack overflow while parse flatbuffers or json.
39#if !defined(FLATBUFFERS_MAX_PARSING_DEPTH)
40# define FLATBUFFERS_MAX_PARSING_DEPTH 64
41#endif
42
43namespace flatbuffers {
44
45// The order of these matters for Is*() functions below.
46// Additionally, Parser::ParseType assumes bool..string is a contiguous range
47// of type tokens.
48// clang-format off
49#define FLATBUFFERS_GEN_TYPES_SCALAR(TD) \
Austin Schuh272c6132020-11-14 16:37:52 -080050 TD(NONE, "", uint8_t, byte, byte, byte, uint8, u8, UByte, UInt8) \
51 TD(UTYPE, "", uint8_t, byte, byte, byte, uint8, u8, UByte, UInt8) /* begin scalar/int */ \
52 TD(BOOL, "bool", uint8_t, boolean,bool, bool, bool, bool, Boolean, Bool) \
53 TD(CHAR, "byte", int8_t, byte, int8, sbyte, int8, i8, Byte, Int8) \
54 TD(UCHAR, "ubyte", uint8_t, byte, byte, byte, uint8, u8, UByte, UInt8) \
55 TD(SHORT, "short", int16_t, short, int16, short, int16, i16, Short, Int16) \
56 TD(USHORT, "ushort", uint16_t, short, uint16, ushort, uint16, u16, UShort, UInt16) \
57 TD(INT, "int", int32_t, int, int32, int, int32, i32, Int, Int32) \
58 TD(UINT, "uint", uint32_t, int, uint32, uint, uint32, u32, UInt, UInt32) \
59 TD(LONG, "long", int64_t, long, int64, long, int64, i64, Long, Int64) \
60 TD(ULONG, "ulong", uint64_t, long, uint64, ulong, uint64, u64, ULong, UInt64) /* end int */ \
61 TD(FLOAT, "float", float, float, float32, float, float32, f32, Float, Float32) /* begin float */ \
62 TD(DOUBLE, "double", double, double, float64, double, float64, f64, Double, Double) /* end float/scalar */
Austin Schuhe89fa2d2019-08-14 20:24:23 -070063#define FLATBUFFERS_GEN_TYPES_POINTER(TD) \
Austin Schuh272c6132020-11-14 16:37:52 -080064 TD(STRING, "string", Offset<void>, int, int, StringOffset, int, unused, Int, Offset<String>) \
65 TD(VECTOR, "", Offset<void>, int, int, VectorOffset, int, unused, Int, Offset<UOffset>) \
66 TD(STRUCT, "", Offset<void>, int, int, int, int, unused, Int, Offset<UOffset>) \
67 TD(UNION, "", Offset<void>, int, int, int, int, unused, Int, Offset<UOffset>)
Austin Schuhe89fa2d2019-08-14 20:24:23 -070068#define FLATBUFFERS_GEN_TYPE_ARRAY(TD) \
Austin Schuh272c6132020-11-14 16:37:52 -080069 TD(ARRAY, "", int, int, int, int, int, unused, Int, Offset<UOffset>)
Austin Schuhe89fa2d2019-08-14 20:24:23 -070070// The fields are:
71// - enum
72// - FlatBuffers schema type.
73// - C++ type.
74// - Java type.
75// - Go type.
76// - C# / .Net type.
77// - Python type.
78// - Rust type.
79// - Kotlin type.
80
81// using these macros, we can now write code dealing with types just once, e.g.
82
83/*
84switch (type) {
85 #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, \
86 RTYPE, KTYPE) \
87 case BASE_TYPE_ ## ENUM: \
88 // do something specific to CTYPE here
89 FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
90 #undef FLATBUFFERS_TD
91}
92*/
93
Austin Schuh272c6132020-11-14 16:37:52 -080094// If not all FLATBUFFERS_GEN_() arguments are necessary for implementation
95// of FLATBUFFERS_TD, you can use a variadic macro (with __VA_ARGS__ if needed).
96// In the above example, only CTYPE is used to generate the code, it can be rewritten:
97
98/*
99switch (type) {
100 #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, ...) \
101 case BASE_TYPE_ ## ENUM: \
102 // do something specific to CTYPE here
103 FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
104 #undef FLATBUFFERS_TD
105}
106*/
107
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700108#define FLATBUFFERS_GEN_TYPES(TD) \
109 FLATBUFFERS_GEN_TYPES_SCALAR(TD) \
110 FLATBUFFERS_GEN_TYPES_POINTER(TD) \
111 FLATBUFFERS_GEN_TYPE_ARRAY(TD)
112
113// Create an enum for all the types above.
114#ifdef __GNUC__
115__extension__ // Stop GCC complaining about trailing comma with -Wpendantic.
116#endif
117enum BaseType {
Austin Schuh272c6132020-11-14 16:37:52 -0800118 #define FLATBUFFERS_TD(ENUM, ...) \
119 BASE_TYPE_ ## ENUM,
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700120 FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
121 #undef FLATBUFFERS_TD
122};
123
Austin Schuh272c6132020-11-14 16:37:52 -0800124#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, ...) \
125 static_assert(sizeof(CTYPE) <= sizeof(largest_scalar_t), \
126 "define largest_scalar_t as " #CTYPE);
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700127 FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
128#undef FLATBUFFERS_TD
129
130inline bool IsScalar (BaseType t) { return t >= BASE_TYPE_UTYPE &&
131 t <= BASE_TYPE_DOUBLE; }
132inline bool IsInteger(BaseType t) { return t >= BASE_TYPE_UTYPE &&
133 t <= BASE_TYPE_ULONG; }
134inline bool IsFloat (BaseType t) { return t == BASE_TYPE_FLOAT ||
135 t == BASE_TYPE_DOUBLE; }
136inline bool IsLong (BaseType t) { return t == BASE_TYPE_LONG ||
137 t == BASE_TYPE_ULONG; }
138inline bool IsBool (BaseType t) { return t == BASE_TYPE_BOOL; }
139inline bool IsOneByte(BaseType t) { return t >= BASE_TYPE_UTYPE &&
140 t <= BASE_TYPE_UCHAR; }
141
142inline bool IsUnsigned(BaseType t) {
143 return (t == BASE_TYPE_UTYPE) || (t == BASE_TYPE_UCHAR) ||
144 (t == BASE_TYPE_USHORT) || (t == BASE_TYPE_UINT) ||
145 (t == BASE_TYPE_ULONG);
146}
147
148// clang-format on
149
150extern const char *const kTypeNames[];
151extern const char kTypeSizes[];
152
153inline size_t SizeOf(BaseType t) { return kTypeSizes[t]; }
154
155struct StructDef;
156struct EnumDef;
157class Parser;
158
159// Represents any type in the IDL, which is a combination of the BaseType
160// and additional information for vectors/structs_.
161struct Type {
162 explicit Type(BaseType _base_type = BASE_TYPE_NONE, StructDef *_sd = nullptr,
163 EnumDef *_ed = nullptr, uint16_t _fixed_length = 0)
164 : base_type(_base_type),
165 element(BASE_TYPE_NONE),
166 struct_def(_sd),
167 enum_def(_ed),
168 fixed_length(_fixed_length) {}
169
170 bool operator==(const Type &o) {
171 return base_type == o.base_type && element == o.element &&
172 struct_def == o.struct_def && enum_def == o.enum_def;
173 }
174
175 Type VectorType() const {
176 return Type(element, struct_def, enum_def, fixed_length);
177 }
178
179 Offset<reflection::Type> Serialize(FlatBufferBuilder *builder) const;
180
181 bool Deserialize(const Parser &parser, const reflection::Type *type);
182
183 BaseType base_type;
184 BaseType element; // only set if t == BASE_TYPE_VECTOR
185 StructDef *struct_def; // only set if t or element == BASE_TYPE_STRUCT
186 EnumDef *enum_def; // set if t == BASE_TYPE_UNION / BASE_TYPE_UTYPE,
187 // or for an integral type derived from an enum.
188 uint16_t fixed_length; // only set if t == BASE_TYPE_ARRAY
189};
190
191// Represents a parsed scalar value, it's type, and field offset.
192struct Value {
193 Value()
194 : constant("0"),
195 offset(static_cast<voffset_t>(~(static_cast<voffset_t>(0U)))) {}
196 Type type;
197 std::string constant;
198 voffset_t offset;
199};
200
201// Helper class that retains the original order of a set of identifiers and
202// also provides quick lookup.
203template<typename T> class SymbolTable {
204 public:
205 ~SymbolTable() {
206 for (auto it = vec.begin(); it != vec.end(); ++it) { delete *it; }
207 }
208
209 bool Add(const std::string &name, T *e) {
210 vector_emplace_back(&vec, e);
211 auto it = dict.find(name);
212 if (it != dict.end()) return true;
213 dict[name] = e;
214 return false;
215 }
216
217 void Move(const std::string &oldname, const std::string &newname) {
218 auto it = dict.find(oldname);
219 if (it != dict.end()) {
220 auto obj = it->second;
221 dict.erase(it);
222 dict[newname] = obj;
223 } else {
224 FLATBUFFERS_ASSERT(false);
225 }
226 }
227
228 T *Lookup(const std::string &name) const {
229 auto it = dict.find(name);
230 return it == dict.end() ? nullptr : it->second;
231 }
232
233 public:
234 std::map<std::string, T *> dict; // quick lookup
235 std::vector<T *> vec; // Used to iterate in order of insertion
236};
237
238// A name space, as set in the schema.
239struct Namespace {
240 Namespace() : from_table(0) {}
241
Austin Schuh272c6132020-11-14 16:37:52 -0800242 // Given a (potentially unqualified) name, return the "fully qualified" name
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700243 // which has a full namespaced descriptor.
244 // With max_components you can request less than the number of components
245 // the current namespace has.
246 std::string GetFullyQualifiedName(const std::string &name,
247 size_t max_components = 1000) const;
248
249 std::vector<std::string> components;
250 size_t from_table; // Part of the namespace corresponds to a message/table.
251};
252
253inline bool operator<(const Namespace &a, const Namespace &b) {
254 size_t min_size = std::min(a.components.size(), b.components.size());
255 for (size_t i = 0; i < min_size; ++i) {
256 if (a.components[i] != b.components[i])
257 return a.components[i] < b.components[i];
258 }
259 return a.components.size() < b.components.size();
260}
261
262// Base class for all definition types (fields, structs_, enums_).
263struct Definition {
264 Definition()
265 : generated(false),
266 defined_namespace(nullptr),
267 serialized_location(0),
268 index(-1),
269 refcount(1) {}
270
271 flatbuffers::Offset<
272 flatbuffers::Vector<flatbuffers::Offset<reflection::KeyValue>>>
273 SerializeAttributes(FlatBufferBuilder *builder, const Parser &parser) const;
274
275 bool DeserializeAttributes(Parser &parser,
276 const Vector<Offset<reflection::KeyValue>> *attrs);
277
278 std::string name;
279 std::string file;
280 std::vector<std::string> doc_comment;
281 SymbolTable<Value> attributes;
282 bool generated; // did we already output code for this definition?
283 Namespace *defined_namespace; // Where it was defined.
284
285 // For use with Serialize()
286 uoffset_t serialized_location;
287 int index; // Inside the vector it is stored.
288 int refcount;
289};
290
291struct FieldDef : public Definition {
292 FieldDef()
293 : deprecated(false),
294 required(false),
295 key(false),
296 shared(false),
297 native_inline(false),
298 flexbuffer(false),
Austin Schuh272c6132020-11-14 16:37:52 -0800299 optional(false),
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700300 nested_flatbuffer(NULL),
301 padding(0) {}
302
303 Offset<reflection::Field> Serialize(FlatBufferBuilder *builder, uint16_t id,
304 const Parser &parser) const;
305
306 bool Deserialize(Parser &parser, const reflection::Field *field);
307
Austin Schuh272c6132020-11-14 16:37:52 -0800308 bool IsScalarOptional() const {
309 return IsScalar(value.type.base_type) && optional;
310 }
311
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700312 Value value;
313 bool deprecated; // Field is allowed to be present in old data, but can't be.
314 // written in new data nor accessed in new code.
315 bool required; // Field must always be present.
316 bool key; // Field functions as a key for creating sorted vectors.
317 bool shared; // Field will be using string pooling (i.e. CreateSharedString)
318 // as default serialization behavior if field is a string.
319 bool native_inline; // Field will be defined inline (instead of as a pointer)
320 // for native tables if field is a struct.
321 bool flexbuffer; // This field contains FlexBuffer data.
Austin Schuh272c6132020-11-14 16:37:52 -0800322 bool optional; // If True, this field is Null (as opposed to default
323 // valued).
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700324 StructDef *nested_flatbuffer; // This field contains nested FlatBuffer data.
325 size_t padding; // Bytes to always pad after this field.
326};
327
328struct StructDef : public Definition {
329 StructDef()
330 : fixed(false),
331 predecl(true),
332 sortbysize(true),
333 has_key(false),
334 minalign(1),
335 bytesize(0) {}
336
337 void PadLastField(size_t min_align) {
338 auto padding = PaddingBytes(bytesize, min_align);
339 bytesize += padding;
340 if (fields.vec.size()) fields.vec.back()->padding = padding;
341 }
342
343 Offset<reflection::Object> Serialize(FlatBufferBuilder *builder,
344 const Parser &parser) const;
345
346 bool Deserialize(Parser &parser, const reflection::Object *object);
347
348 SymbolTable<FieldDef> fields;
349
350 bool fixed; // If it's struct, not a table.
351 bool predecl; // If it's used before it was defined.
352 bool sortbysize; // Whether fields come in the declaration or size order.
353 bool has_key; // It has a key field.
354 size_t minalign; // What the whole object needs to be aligned to.
355 size_t bytesize; // Size if fixed.
356
357 flatbuffers::unique_ptr<std::string> original_location;
358};
359
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700360struct EnumDef;
361struct EnumValBuilder;
362
363struct EnumVal {
Austin Schuh272c6132020-11-14 16:37:52 -0800364 Offset<reflection::EnumVal> Serialize(FlatBufferBuilder *builder,
365 const Parser &parser) const;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700366
367 bool Deserialize(const Parser &parser, const reflection::EnumVal *val);
368
369 uint64_t GetAsUInt64() const { return static_cast<uint64_t>(value); }
370 int64_t GetAsInt64() const { return value; }
371 bool IsZero() const { return 0 == value; }
372 bool IsNonZero() const { return !IsZero(); }
373
374 std::string name;
375 std::vector<std::string> doc_comment;
376 Type union_type;
377
378 private:
379 friend EnumDef;
380 friend EnumValBuilder;
381 friend bool operator==(const EnumVal &lhs, const EnumVal &rhs);
382
383 EnumVal(const std::string &_name, int64_t _val) : name(_name), value(_val) {}
384 EnumVal() : value(0) {}
385
386 int64_t value;
387};
388
389struct EnumDef : public Definition {
390 EnumDef() : is_union(false), uses_multiple_type_instances(false) {}
391
392 Offset<reflection::Enum> Serialize(FlatBufferBuilder *builder,
393 const Parser &parser) const;
394
395 bool Deserialize(Parser &parser, const reflection::Enum *values);
396
397 template<typename T> void ChangeEnumValue(EnumVal *ev, T new_val);
398 void SortByValue();
399 void RemoveDuplicates();
400
401 std::string AllFlags() const;
402 const EnumVal *MinValue() const;
403 const EnumVal *MaxValue() const;
404 // Returns the number of integer steps from v1 to v2.
405 uint64_t Distance(const EnumVal *v1, const EnumVal *v2) const;
406 // Returns the number of integer steps from Min to Max.
407 uint64_t Distance() const { return Distance(MinValue(), MaxValue()); }
408
409 EnumVal *ReverseLookup(int64_t enum_idx,
410 bool skip_union_default = false) const;
411 EnumVal *FindByValue(const std::string &constant) const;
412
413 std::string ToString(const EnumVal &ev) const {
414 return IsUInt64() ? NumToString(ev.GetAsUInt64())
415 : NumToString(ev.GetAsInt64());
416 }
417
418 size_t size() const { return vals.vec.size(); }
419
Austin Schuh272c6132020-11-14 16:37:52 -0800420 const std::vector<EnumVal *> &Vals() const { return vals.vec; }
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700421
422 const EnumVal *Lookup(const std::string &enum_name) const {
423 return vals.Lookup(enum_name);
424 }
425
426 bool is_union;
427 // Type is a union which uses type aliases where at least one type is
428 // available under two different names.
429 bool uses_multiple_type_instances;
430 Type underlying_type;
431
432 private:
433 bool IsUInt64() const {
434 return (BASE_TYPE_ULONG == underlying_type.base_type);
435 }
436
437 friend EnumValBuilder;
438 SymbolTable<EnumVal> vals;
439};
440
Austin Schuh272c6132020-11-14 16:37:52 -0800441inline bool IsString(const Type &type) {
442 return type.base_type == BASE_TYPE_STRING;
443}
444
445inline bool IsStruct(const Type &type) {
446 return type.base_type == BASE_TYPE_STRUCT && type.struct_def->fixed;
447}
448
449inline bool IsUnion(const Type &type) {
450 return type.enum_def != nullptr && type.enum_def->is_union;
451}
452
453inline bool IsVector(const Type &type) {
454 return type.base_type == BASE_TYPE_VECTOR;
455}
456
457inline bool IsArray(const Type &type) {
458 return type.base_type == BASE_TYPE_ARRAY;
459}
460
461inline bool IsSeries(const Type &type) {
462 return IsVector(type) || IsArray(type);
463}
464
465inline bool IsEnum(const Type &type) {
466 return type.enum_def != nullptr && IsInteger(type.base_type);
467}
468
469inline size_t InlineSize(const Type &type) {
470 return IsStruct(type)
471 ? type.struct_def->bytesize
472 : (IsArray(type)
473 ? InlineSize(type.VectorType()) * type.fixed_length
474 : SizeOf(type.base_type));
475}
476
477inline size_t InlineAlignment(const Type &type) {
478 if (IsStruct(type)) {
479 return type.struct_def->minalign;
480 } else if (IsArray(type)) {
481 return IsStruct(type.VectorType()) ? type.struct_def->minalign
482 : SizeOf(type.element);
483 } else {
484 return SizeOf(type.base_type);
485 }
486}
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700487inline bool operator==(const EnumVal &lhs, const EnumVal &rhs) {
488 return lhs.value == rhs.value;
489}
490inline bool operator!=(const EnumVal &lhs, const EnumVal &rhs) {
491 return !(lhs == rhs);
492}
493
494inline bool EqualByName(const Type &a, const Type &b) {
495 return a.base_type == b.base_type && a.element == b.element &&
496 (a.struct_def == b.struct_def ||
497 a.struct_def->name == b.struct_def->name) &&
498 (a.enum_def == b.enum_def || a.enum_def->name == b.enum_def->name);
499}
500
501struct RPCCall : public Definition {
Austin Schuh272c6132020-11-14 16:37:52 -0800502 Offset<reflection::RPCCall> Serialize(FlatBufferBuilder *builder,
503 const Parser &parser) const;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700504
505 bool Deserialize(Parser &parser, const reflection::RPCCall *call);
506
507 StructDef *request, *response;
508};
509
510struct ServiceDef : public Definition {
Austin Schuh272c6132020-11-14 16:37:52 -0800511 Offset<reflection::Service> Serialize(FlatBufferBuilder *builder,
512 const Parser &parser) const;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700513 bool Deserialize(Parser &parser, const reflection::Service *service);
514
515 SymbolTable<RPCCall> calls;
516};
517
518// Container of options that may apply to any of the source/text generators.
519struct IDLOptions {
Austin Schuh272c6132020-11-14 16:37:52 -0800520 bool gen_jvmstatic;
521 // Use flexbuffers instead for binary and text generation
522 bool use_flexbuffers;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700523 bool strict_json;
524 bool skip_js_exports;
525 bool use_goog_js_export_format;
526 bool use_ES6_js_export_format;
527 bool output_default_scalars_in_json;
528 int indent_step;
529 bool output_enum_identifiers;
530 bool prefixed_enums;
531 bool scoped_enums;
532 bool include_dependence_headers;
533 bool mutable_buffer;
534 bool one_file;
535 bool proto_mode;
536 bool proto_oneof_union;
537 bool generate_all;
538 bool skip_unexpected_fields_in_json;
539 bool generate_name_strings;
540 bool generate_object_based_api;
541 bool gen_compare;
542 std::string cpp_object_api_pointer_type;
543 std::string cpp_object_api_string_type;
544 bool cpp_object_api_string_flexible_constructor;
Austin Schuh272c6132020-11-14 16:37:52 -0800545 bool cpp_direct_copy;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700546 bool gen_nullable;
Austin Schuh272c6132020-11-14 16:37:52 -0800547 bool java_checkerframework;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700548 bool gen_generated;
549 std::string object_prefix;
550 std::string object_suffix;
551 bool union_value_namespacing;
552 bool allow_non_utf8;
553 bool natural_utf8;
554 std::string include_prefix;
555 bool keep_include_path;
556 bool binary_schema_comments;
557 bool binary_schema_builtins;
Austin Schuh272c6132020-11-14 16:37:52 -0800558 bool binary_schema_gen_embed;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700559 bool skip_flatbuffers_import;
560 std::string go_import;
561 std::string go_namespace;
562 bool reexport_ts_modules;
563 bool js_ts_short_names;
564 bool protobuf_ascii_alike;
565 bool size_prefixed;
566 std::string root_type;
567 bool force_defaults;
Austin Schuh272c6132020-11-14 16:37:52 -0800568 bool java_primitive_has_method;
569 bool cs_gen_json_serializer;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700570 std::vector<std::string> cpp_includes;
Austin Schuh272c6132020-11-14 16:37:52 -0800571 std::string cpp_std;
572 std::string proto_namespace_suffix;
573 std::string filename_suffix;
574 std::string filename_extension;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700575
576 // Possible options for the more general generator below.
577 enum Language {
578 kJava = 1 << 0,
579 kCSharp = 1 << 1,
580 kGo = 1 << 2,
581 kCpp = 1 << 3,
582 kJs = 1 << 4,
583 kPython = 1 << 5,
584 kPhp = 1 << 6,
585 kJson = 1 << 7,
586 kBinary = 1 << 8,
587 kTs = 1 << 9,
588 kJsonSchema = 1 << 10,
589 kDart = 1 << 11,
590 kLua = 1 << 12,
591 kLobster = 1 << 13,
592 kRust = 1 << 14,
593 kKotlin = 1 << 15,
Austin Schuh272c6132020-11-14 16:37:52 -0800594 kSwift = 1 << 16,
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700595 kMAX
596 };
597
598 Language lang;
599
600 enum MiniReflect { kNone, kTypes, kTypesAndNames };
601
602 MiniReflect mini_reflect;
603
Austin Schuh58b9b472020-11-25 19:12:44 -0800604 // If set, require all fields in a table to be explicitly numbered.
605 bool require_explicit_ids;
606
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700607 // The corresponding language bit will be set if a language is included
608 // for code generation.
609 unsigned long lang_to_generate;
610
Austin Schuh272c6132020-11-14 16:37:52 -0800611 // If set (default behavior), empty string fields will be set to nullptr to
612 // make the flatbuffer more compact.
613 bool set_empty_strings_to_null;
614
615 // If set (default behavior), empty vector fields will be set to nullptr to
616 // make the flatbuffer more compact.
617 bool set_empty_vectors_to_null;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700618
619 IDLOptions()
Austin Schuh272c6132020-11-14 16:37:52 -0800620 : gen_jvmstatic(false),
621 use_flexbuffers(false),
622 strict_json(false),
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700623 skip_js_exports(false),
624 use_goog_js_export_format(false),
625 use_ES6_js_export_format(false),
626 output_default_scalars_in_json(false),
627 indent_step(2),
628 output_enum_identifiers(true),
629 prefixed_enums(true),
630 scoped_enums(false),
631 include_dependence_headers(true),
632 mutable_buffer(false),
633 one_file(false),
634 proto_mode(false),
635 proto_oneof_union(false),
636 generate_all(false),
637 skip_unexpected_fields_in_json(false),
638 generate_name_strings(false),
639 generate_object_based_api(false),
640 gen_compare(false),
641 cpp_object_api_pointer_type("std::unique_ptr"),
642 cpp_object_api_string_flexible_constructor(false),
Austin Schuh272c6132020-11-14 16:37:52 -0800643 cpp_direct_copy(true),
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700644 gen_nullable(false),
Austin Schuh272c6132020-11-14 16:37:52 -0800645 java_checkerframework(false),
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700646 gen_generated(false),
647 object_suffix("T"),
648 union_value_namespacing(true),
649 allow_non_utf8(false),
650 natural_utf8(false),
651 keep_include_path(false),
652 binary_schema_comments(false),
653 binary_schema_builtins(false),
Austin Schuh272c6132020-11-14 16:37:52 -0800654 binary_schema_gen_embed(false),
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700655 skip_flatbuffers_import(false),
656 reexport_ts_modules(true),
657 js_ts_short_names(false),
658 protobuf_ascii_alike(false),
659 size_prefixed(false),
660 force_defaults(false),
Austin Schuh272c6132020-11-14 16:37:52 -0800661 java_primitive_has_method(false),
662 cs_gen_json_serializer(false),
663 filename_suffix("_generated"),
664 filename_extension(),
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700665 lang(IDLOptions::kJava),
666 mini_reflect(IDLOptions::kNone),
Austin Schuh58b9b472020-11-25 19:12:44 -0800667 require_explicit_ids(false),
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700668 lang_to_generate(0),
Austin Schuh272c6132020-11-14 16:37:52 -0800669 set_empty_strings_to_null(true),
670 set_empty_vectors_to_null(true) {}
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700671};
672
673// This encapsulates where the parser is in the current source file.
674struct ParserState {
675 ParserState()
676 : cursor_(nullptr),
677 line_start_(nullptr),
678 line_(0),
679 token_(-1),
680 attr_is_trivial_ascii_string_(true) {}
681
682 protected:
683 void ResetState(const char *source) {
684 cursor_ = source;
685 line_ = 0;
686 MarkNewLine();
687 }
688
689 void MarkNewLine() {
690 line_start_ = cursor_;
691 line_ += 1;
692 }
693
694 int64_t CursorPosition() const {
695 FLATBUFFERS_ASSERT(cursor_ && line_start_ && cursor_ >= line_start_);
696 return static_cast<int64_t>(cursor_ - line_start_);
697 }
698
699 const char *cursor_;
700 const char *line_start_;
701 int line_; // the current line being parsed
702 int token_;
703
704 // Flag: text in attribute_ is true ASCII string without escape
705 // sequences. Only printable ASCII (without [\t\r\n]).
706 // Used for number-in-string (and base64 string in future).
707 bool attr_is_trivial_ascii_string_;
708 std::string attribute_;
709 std::vector<std::string> doc_comment_;
710};
711
712// A way to make error propagation less error prone by requiring values to be
713// checked.
714// Once you create a value of this type you must either:
715// - Call Check() on it.
716// - Copy or assign it to another value.
717// Failure to do so leads to an assert.
718// This guarantees that this as return value cannot be ignored.
719class CheckedError {
720 public:
721 explicit CheckedError(bool error)
722 : is_error_(error), has_been_checked_(false) {}
723
724 CheckedError &operator=(const CheckedError &other) {
725 is_error_ = other.is_error_;
726 has_been_checked_ = false;
727 other.has_been_checked_ = true;
728 return *this;
729 }
730
731 CheckedError(const CheckedError &other) {
732 *this = other; // Use assignment operator.
733 }
734
735 ~CheckedError() { FLATBUFFERS_ASSERT(has_been_checked_); }
736
737 bool Check() {
738 has_been_checked_ = true;
739 return is_error_;
740 }
741
742 private:
743 bool is_error_;
744 mutable bool has_been_checked_;
745};
746
747// Additionally, in GCC we can get these errors statically, for additional
748// assurance:
749// clang-format off
750#ifdef __GNUC__
751#define FLATBUFFERS_CHECKED_ERROR CheckedError \
752 __attribute__((warn_unused_result))
753#else
754#define FLATBUFFERS_CHECKED_ERROR CheckedError
755#endif
756// clang-format on
757
758class Parser : public ParserState {
759 public:
760 explicit Parser(const IDLOptions &options = IDLOptions())
761 : current_namespace_(nullptr),
762 empty_namespace_(nullptr),
Austin Schuh272c6132020-11-14 16:37:52 -0800763 flex_builder_(256, flexbuffers::BUILDER_FLAG_SHARE_ALL),
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700764 root_struct_def_(nullptr),
765 opts(options),
766 uses_flexbuffers_(false),
767 source_(nullptr),
768 anonymous_counter(0),
769 recurse_protection_counter(0) {
Austin Schuh272c6132020-11-14 16:37:52 -0800770 if (opts.force_defaults) { builder_.ForceDefaults(true); }
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700771 // Start out with the empty namespace being current.
772 empty_namespace_ = new Namespace();
773 namespaces_.push_back(empty_namespace_);
774 current_namespace_ = empty_namespace_;
775 known_attributes_["deprecated"] = true;
776 known_attributes_["required"] = true;
777 known_attributes_["key"] = true;
778 known_attributes_["shared"] = true;
779 known_attributes_["hash"] = true;
780 known_attributes_["id"] = true;
781 known_attributes_["force_align"] = true;
782 known_attributes_["bit_flags"] = true;
783 known_attributes_["original_order"] = true;
784 known_attributes_["nested_flatbuffer"] = true;
785 known_attributes_["csharp_partial"] = true;
786 known_attributes_["streaming"] = true;
787 known_attributes_["idempotent"] = true;
788 known_attributes_["cpp_type"] = true;
789 known_attributes_["cpp_ptr_type"] = true;
790 known_attributes_["cpp_ptr_type_get"] = true;
791 known_attributes_["cpp_str_type"] = true;
792 known_attributes_["cpp_str_flex_ctor"] = true;
793 known_attributes_["native_inline"] = true;
794 known_attributes_["native_custom_alloc"] = true;
795 known_attributes_["native_type"] = true;
796 known_attributes_["native_default"] = true;
797 known_attributes_["flexbuffer"] = true;
798 known_attributes_["private"] = true;
799 }
800
801 ~Parser() {
802 for (auto it = namespaces_.begin(); it != namespaces_.end(); ++it) {
803 delete *it;
804 }
805 }
806
Austin Schuh58b9b472020-11-25 19:12:44 -0800807#ifdef FLATBUFFERS_DEFAULT_DECLARATION
808 Parser(Parser&&) = default;
809 Parser& operator=(Parser&&) = default;
810#endif
811
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700812 // Parse the string containing either schema or JSON data, which will
813 // populate the SymbolTable's or the FlatBufferBuilder above.
814 // include_paths is used to resolve any include statements, and typically
815 // should at least include the project path (where you loaded source_ from).
816 // include_paths must be nullptr terminated if specified.
817 // If include_paths is nullptr, it will attempt to load from the current
818 // directory.
819 // If the source was loaded from a file and isn't an include file,
820 // supply its name in source_filename.
821 // All paths specified in this call must be in posix format, if you accept
822 // paths from user input, please call PosixPath on them first.
823 bool Parse(const char *_source, const char **include_paths = nullptr,
824 const char *source_filename = nullptr);
825
Austin Schuh58b9b472020-11-25 19:12:44 -0800826 bool ParseJson(const char *json, const char *json_filename = nullptr);
827
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700828 // Set the root type. May override the one set in the schema.
829 bool SetRootType(const char *name);
830
831 // Mark all definitions as already having code generated.
832 void MarkGenerated();
833
834 // Get the files recursively included by the given file. The returned
835 // container will have at least the given file.
836 std::set<std::string> GetIncludedFilesRecursive(
837 const std::string &file_name) const;
838
839 // Fills builder_ with a binary version of the schema parsed.
840 // See reflection/reflection.fbs
841 void Serialize();
842
843 // Deserialize a schema buffer
844 bool Deserialize(const uint8_t *buf, const size_t size);
845
846 // Fills internal structure as if the schema passed had been loaded by parsing
847 // with Parse except that included filenames will not be populated.
Austin Schuh272c6132020-11-14 16:37:52 -0800848 bool Deserialize(const reflection::Schema *schema);
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700849
Austin Schuh272c6132020-11-14 16:37:52 -0800850 Type *DeserializeType(const reflection::Type *type);
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700851
852 // Checks that the schema represented by this parser is a safe evolution
853 // of the schema provided. Returns non-empty error on any problems.
854 std::string ConformTo(const Parser &base);
855
856 // Similar to Parse(), but now only accepts JSON to be parsed into a
857 // FlexBuffer.
858 bool ParseFlexBuffer(const char *source, const char *source_filename,
859 flexbuffers::Builder *builder);
860
861 StructDef *LookupStruct(const std::string &id) const;
862
863 std::string UnqualifiedName(const std::string &fullQualifiedName);
864
865 FLATBUFFERS_CHECKED_ERROR Error(const std::string &msg);
866
Austin Schuh272c6132020-11-14 16:37:52 -0800867 // @brief Verify that any of 'opts.lang_to_generate' supports Optional scalars
868 // in a schema.
869 // @param opts Options used to parce a schema and generate code.
870 static bool SupportsOptionalScalars(const flatbuffers::IDLOptions &opts);
871
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700872 private:
873 void Message(const std::string &msg);
874 void Warning(const std::string &msg);
875 FLATBUFFERS_CHECKED_ERROR ParseHexNum(int nibbles, uint64_t *val);
876 FLATBUFFERS_CHECKED_ERROR Next();
877 FLATBUFFERS_CHECKED_ERROR SkipByteOrderMark();
878 bool Is(int t) const;
879 bool IsIdent(const char *id) const;
880 FLATBUFFERS_CHECKED_ERROR Expect(int t);
881 std::string TokenToStringId(int t) const;
882 EnumDef *LookupEnum(const std::string &id);
883 FLATBUFFERS_CHECKED_ERROR ParseNamespacing(std::string *id,
884 std::string *last);
885 FLATBUFFERS_CHECKED_ERROR ParseTypeIdent(Type &type);
886 FLATBUFFERS_CHECKED_ERROR ParseType(Type &type);
887 FLATBUFFERS_CHECKED_ERROR AddField(StructDef &struct_def,
888 const std::string &name, const Type &type,
889 FieldDef **dest);
890 FLATBUFFERS_CHECKED_ERROR ParseField(StructDef &struct_def);
Austin Schuh272c6132020-11-14 16:37:52 -0800891 FLATBUFFERS_CHECKED_ERROR ParseString(Value &val, bool use_string_pooling);
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700892 FLATBUFFERS_CHECKED_ERROR ParseComma();
893 FLATBUFFERS_CHECKED_ERROR ParseAnyValue(Value &val, FieldDef *field,
894 size_t parent_fieldn,
895 const StructDef *parent_struct_def,
896 uoffset_t count,
897 bool inside_vector = false);
898 template<typename F>
899 FLATBUFFERS_CHECKED_ERROR ParseTableDelimiters(size_t &fieldn,
900 const StructDef *struct_def,
901 F body);
902 FLATBUFFERS_CHECKED_ERROR ParseTable(const StructDef &struct_def,
903 std::string *value, uoffset_t *ovalue);
904 void SerializeStruct(const StructDef &struct_def, const Value &val);
905 void SerializeStruct(FlatBufferBuilder &builder, const StructDef &struct_def,
906 const Value &val);
907 template<typename F>
908 FLATBUFFERS_CHECKED_ERROR ParseVectorDelimiters(uoffset_t &count, F body);
909 FLATBUFFERS_CHECKED_ERROR ParseVector(const Type &type, uoffset_t *ovalue,
910 FieldDef *field, size_t fieldn);
911 FLATBUFFERS_CHECKED_ERROR ParseArray(Value &array);
Austin Schuh272c6132020-11-14 16:37:52 -0800912 FLATBUFFERS_CHECKED_ERROR ParseNestedFlatbuffer(
913 Value &val, FieldDef *field, size_t fieldn,
914 const StructDef *parent_struct_def);
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700915 FLATBUFFERS_CHECKED_ERROR ParseMetaData(SymbolTable<Value> *attributes);
Austin Schuh272c6132020-11-14 16:37:52 -0800916 FLATBUFFERS_CHECKED_ERROR TryTypedValue(const std::string *name, int dtoken,
917 bool check, Value &e, BaseType req,
918 bool *destmatch);
919 FLATBUFFERS_CHECKED_ERROR ParseHash(Value &e, FieldDef *field);
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700920 FLATBUFFERS_CHECKED_ERROR TokenError();
Austin Schuh272c6132020-11-14 16:37:52 -0800921 FLATBUFFERS_CHECKED_ERROR ParseSingleValue(const std::string *name, Value &e,
922 bool check_now);
923 FLATBUFFERS_CHECKED_ERROR ParseFunction(const std::string *name, Value &e);
924 FLATBUFFERS_CHECKED_ERROR ParseEnumFromString(const Type &type,
925 std::string *result);
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700926 StructDef *LookupCreateStruct(const std::string &name,
927 bool create_if_new = true,
928 bool definition = false);
929 FLATBUFFERS_CHECKED_ERROR ParseEnum(bool is_union, EnumDef **dest);
930 FLATBUFFERS_CHECKED_ERROR ParseNamespace();
931 FLATBUFFERS_CHECKED_ERROR StartStruct(const std::string &name,
932 StructDef **dest);
Austin Schuh272c6132020-11-14 16:37:52 -0800933 FLATBUFFERS_CHECKED_ERROR StartEnum(const std::string &name, bool is_union,
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700934 EnumDef **dest);
935 FLATBUFFERS_CHECKED_ERROR ParseDecl();
936 FLATBUFFERS_CHECKED_ERROR ParseService();
937 FLATBUFFERS_CHECKED_ERROR ParseProtoFields(StructDef *struct_def,
938 bool isextend, bool inside_oneof);
939 FLATBUFFERS_CHECKED_ERROR ParseProtoOption();
940 FLATBUFFERS_CHECKED_ERROR ParseProtoKey();
941 FLATBUFFERS_CHECKED_ERROR ParseProtoDecl();
942 FLATBUFFERS_CHECKED_ERROR ParseProtoCurliesOrIdent();
943 FLATBUFFERS_CHECKED_ERROR ParseTypeFromProtoType(Type *type);
944 FLATBUFFERS_CHECKED_ERROR SkipAnyJsonValue();
945 FLATBUFFERS_CHECKED_ERROR ParseFlexBufferValue(flexbuffers::Builder *builder);
946 FLATBUFFERS_CHECKED_ERROR StartParseFile(const char *source,
947 const char *source_filename);
948 FLATBUFFERS_CHECKED_ERROR ParseRoot(const char *_source,
Austin Schuh272c6132020-11-14 16:37:52 -0800949 const char **include_paths,
950 const char *source_filename);
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700951 FLATBUFFERS_CHECKED_ERROR DoParse(const char *_source,
Austin Schuh272c6132020-11-14 16:37:52 -0800952 const char **include_paths,
953 const char *source_filename,
954 const char *include_filename);
Austin Schuh58b9b472020-11-25 19:12:44 -0800955 FLATBUFFERS_CHECKED_ERROR DoParseJson();
Austin Schuh272c6132020-11-14 16:37:52 -0800956 FLATBUFFERS_CHECKED_ERROR CheckClash(std::vector<FieldDef *> &fields,
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700957 StructDef *struct_def,
Austin Schuh272c6132020-11-14 16:37:52 -0800958 const char *suffix, BaseType baseType);
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700959
960 bool SupportsAdvancedUnionFeatures() const;
961 bool SupportsAdvancedArrayFeatures() const;
Austin Schuh272c6132020-11-14 16:37:52 -0800962 bool SupportsOptionalScalars() const;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700963 Namespace *UniqueNamespace(Namespace *ns);
964
965 FLATBUFFERS_CHECKED_ERROR RecurseError();
966 template<typename F> CheckedError Recurse(F f);
967
968 public:
969 SymbolTable<Type> types_;
970 SymbolTable<StructDef> structs_;
971 SymbolTable<EnumDef> enums_;
972 SymbolTable<ServiceDef> services_;
973 std::vector<Namespace *> namespaces_;
974 Namespace *current_namespace_;
975 Namespace *empty_namespace_;
Austin Schuh272c6132020-11-14 16:37:52 -0800976 std::string error_; // User readable error_ if Parse() == false
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700977
978 FlatBufferBuilder builder_; // any data contained in the file
Austin Schuh272c6132020-11-14 16:37:52 -0800979 flexbuffers::Builder flex_builder_;
980 flexbuffers::Reference flex_root_;
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700981 StructDef *root_struct_def_;
982 std::string file_identifier_;
983 std::string file_extension_;
984
985 std::map<std::string, std::string> included_files_;
986 std::map<std::string, std::set<std::string>> files_included_per_file_;
987 std::vector<std::string> native_included_files_;
988
989 std::map<std::string, bool> known_attributes_;
990
991 IDLOptions opts;
992 bool uses_flexbuffers_;
993
994 private:
995 const char *source_;
996
997 std::string file_being_parsed_;
998
999 std::vector<std::pair<Value, FieldDef *>> field_stack_;
1000
1001 int anonymous_counter;
1002 int recurse_protection_counter;
1003};
1004
1005// Utility functions for multiple generators:
1006
1007extern std::string MakeCamel(const std::string &in, bool first = true);
1008
Austin Schuh272c6132020-11-14 16:37:52 -08001009extern std::string MakeScreamingCamel(const std::string &in);
1010
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001011// Generate text (JSON) from a given FlatBuffer, and a given Parser
1012// object that has been populated with the corresponding schema.
1013// If ident_step is 0, no indentation will be generated. Additionally,
1014// if it is less than 0, no linefeeds will be generated either.
1015// See idl_gen_text.cpp.
1016// strict_json adds "quotes" around field names if true.
1017// If the flatbuffer cannot be encoded in JSON (e.g., it contains non-UTF-8
1018// byte arrays in String values), returns false.
Austin Schuh272c6132020-11-14 16:37:52 -08001019extern bool GenerateTextFromTable(const Parser &parser, const void *table,
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001020 const std::string &tablename,
1021 std::string *text);
Austin Schuh272c6132020-11-14 16:37:52 -08001022extern bool GenerateText(const Parser &parser, const void *flatbuffer,
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001023 std::string *text);
Austin Schuh272c6132020-11-14 16:37:52 -08001024extern bool GenerateTextFile(const Parser &parser, const std::string &path,
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001025 const std::string &file_name);
1026
Austin Schuh272c6132020-11-14 16:37:52 -08001027// Generate Json schema to string
1028// See idl_gen_json_schema.cpp.
1029extern bool GenerateJsonSchema(const Parser &parser, std::string *json);
1030
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001031// Generate binary files from a given FlatBuffer, and a given Parser
1032// object that has been populated with the corresponding schema.
Austin Schuh272c6132020-11-14 16:37:52 -08001033// See code_generators.cpp.
1034extern bool GenerateBinary(const Parser &parser, const std::string &path,
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001035 const std::string &file_name);
1036
1037// Generate a C++ header from the definitions in the Parser object.
1038// See idl_gen_cpp.
Austin Schuh272c6132020-11-14 16:37:52 -08001039extern bool GenerateCPP(const Parser &parser, const std::string &path,
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001040 const std::string &file_name);
1041
Austin Schuh272c6132020-11-14 16:37:52 -08001042// Generate C# files from the definitions in the Parser object.
1043// See idl_gen_csharp.cpp.
1044extern bool GenerateCSharp(const Parser &parser, const std::string &path,
1045 const std::string &file_name);
1046
1047extern bool GenerateDart(const Parser &parser, const std::string &path,
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001048 const std::string &file_name);
1049
Austin Schuh272c6132020-11-14 16:37:52 -08001050// Generate Java files from the definitions in the Parser object.
1051// See idl_gen_java.cpp.
1052extern bool GenerateJava(const Parser &parser, const std::string &path,
1053 const std::string &file_name);
1054
1055// Generate JavaScript or TypeScript code from the definitions in the Parser
1056// object. See idl_gen_js.
1057extern bool GenerateJSTS(const Parser &parser, const std::string &path,
1058 const std::string &file_name);
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001059
1060// Generate Go files from the definitions in the Parser object.
1061// See idl_gen_go.cpp.
Austin Schuh272c6132020-11-14 16:37:52 -08001062extern bool GenerateGo(const Parser &parser, const std::string &path,
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001063 const std::string &file_name);
1064
1065// Generate Php code from the definitions in the Parser object.
1066// See idl_gen_php.
Austin Schuh272c6132020-11-14 16:37:52 -08001067extern bool GeneratePhp(const Parser &parser, const std::string &path,
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001068 const std::string &file_name);
1069
1070// Generate Python files from the definitions in the Parser object.
1071// See idl_gen_python.cpp.
Austin Schuh272c6132020-11-14 16:37:52 -08001072extern bool GeneratePython(const Parser &parser, const std::string &path,
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001073 const std::string &file_name);
1074
1075// Generate Lobster files from the definitions in the Parser object.
1076// See idl_gen_lobster.cpp.
Austin Schuh272c6132020-11-14 16:37:52 -08001077extern bool GenerateLobster(const Parser &parser, const std::string &path,
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001078 const std::string &file_name);
1079
1080// Generate Lua files from the definitions in the Parser object.
1081// See idl_gen_lua.cpp.
Austin Schuh272c6132020-11-14 16:37:52 -08001082extern bool GenerateLua(const Parser &parser, const std::string &path,
1083 const std::string &file_name);
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001084
1085// Generate Rust files from the definitions in the Parser object.
1086// See idl_gen_rust.cpp.
Austin Schuh272c6132020-11-14 16:37:52 -08001087extern bool GenerateRust(const Parser &parser, const std::string &path,
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001088 const std::string &file_name);
1089
1090// Generate Json schema file
1091// See idl_gen_json_schema.cpp.
Austin Schuh272c6132020-11-14 16:37:52 -08001092extern bool GenerateJsonSchema(const Parser &parser, const std::string &path,
1093 const std::string &file_name);
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001094
1095extern bool GenerateKotlin(const Parser &parser, const std::string &path,
1096 const std::string &file_name);
1097
Austin Schuh272c6132020-11-14 16:37:52 -08001098// Generate Swift classes.
1099// See idl_gen_swift.cpp
1100extern bool GenerateSwift(const Parser &parser, const std::string &path,
1101 const std::string &file_name);
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001102
1103// Generate a schema file from the internal representation, useful after
1104// parsing a .proto schema.
1105extern std::string GenerateFBS(const Parser &parser,
1106 const std::string &file_name);
Austin Schuh272c6132020-11-14 16:37:52 -08001107extern bool GenerateFBS(const Parser &parser, const std::string &path,
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001108 const std::string &file_name);
1109
1110// Generate a make rule for the generated JavaScript or TypeScript code.
1111// See idl_gen_js.cpp.
Austin Schuh272c6132020-11-14 16:37:52 -08001112extern std::string JSTSMakeRule(const Parser &parser, const std::string &path,
1113 const std::string &file_name);
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001114
1115// Generate a make rule for the generated C++ header.
1116// See idl_gen_cpp.cpp.
Austin Schuh272c6132020-11-14 16:37:52 -08001117extern std::string CPPMakeRule(const Parser &parser, const std::string &path,
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001118 const std::string &file_name);
1119
1120// Generate a make rule for the generated Dart code
1121// see idl_gen_dart.cpp
Austin Schuh272c6132020-11-14 16:37:52 -08001122extern std::string DartMakeRule(const Parser &parser, const std::string &path,
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001123 const std::string &file_name);
1124
1125// Generate a make rule for the generated Rust code.
1126// See idl_gen_rust.cpp.
Austin Schuh272c6132020-11-14 16:37:52 -08001127extern std::string RustMakeRule(const Parser &parser, const std::string &path,
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001128 const std::string &file_name);
1129
Austin Schuh272c6132020-11-14 16:37:52 -08001130// Generate a make rule for generated Java or C# files.
1131// See code_generators.cpp.
1132extern std::string JavaCSharpMakeRule(const Parser &parser,
1133 const std::string &path,
1134 const std::string &file_name);
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001135
1136// Generate a make rule for the generated text (JSON) files.
1137// See idl_gen_text.cpp.
Austin Schuh272c6132020-11-14 16:37:52 -08001138extern std::string TextMakeRule(const Parser &parser, const std::string &path,
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001139 const std::string &file_names);
1140
1141// Generate a make rule for the generated binary files.
Austin Schuh272c6132020-11-14 16:37:52 -08001142// See code_generators.cpp.
1143extern std::string BinaryMakeRule(const Parser &parser, const std::string &path,
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001144 const std::string &file_name);
1145
1146// Generate GRPC Cpp interfaces.
1147// See idl_gen_grpc.cpp.
Austin Schuh272c6132020-11-14 16:37:52 -08001148bool GenerateCppGRPC(const Parser &parser, const std::string &path,
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001149 const std::string &file_name);
1150
1151// Generate GRPC Go interfaces.
1152// See idl_gen_grpc.cpp.
Austin Schuh272c6132020-11-14 16:37:52 -08001153bool GenerateGoGRPC(const Parser &parser, const std::string &path,
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001154 const std::string &file_name);
1155
1156// Generate GRPC Java classes.
1157// See idl_gen_grpc.cpp
Austin Schuh272c6132020-11-14 16:37:52 -08001158bool GenerateJavaGRPC(const Parser &parser, const std::string &path,
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001159 const std::string &file_name);
1160
Austin Schuh272c6132020-11-14 16:37:52 -08001161// Generate GRPC Python interfaces.
1162// See idl_gen_grpc.cpp.
1163bool GeneratePythonGRPC(const Parser &parser, const std::string &path,
1164 const std::string &file_name);
1165
1166// Generate GRPC Swift interfaces.
1167// See idl_gen_grpc.cpp.
1168extern bool GenerateSwiftGRPC(const Parser &parser, const std::string &path,
1169 const std::string &file_name);
1170
1171extern bool GenerateTSGRPC(const Parser &parser, const std::string &path,
1172 const std::string &file_name);
Austin Schuhe89fa2d2019-08-14 20:24:23 -07001173} // namespace flatbuffers
1174
1175#endif // FLATBUFFERS_IDL_H_