1
0

idl.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. /*
  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. #ifndef FLATBUFFERS_IDL_H_
  17. #define FLATBUFFERS_IDL_H_
  18. #include <map>
  19. #include <stack>
  20. #include <memory>
  21. #include <functional>
  22. #include "flatbuffers/flatbuffers.h"
  23. #include "flatbuffers/hash.h"
  24. #include "flatbuffers/reflection.h"
  25. // This file defines the data types representing a parsed IDL (Interface
  26. // Definition Language) / schema file.
  27. namespace flatbuffers {
  28. // The order of these matters for Is*() functions below.
  29. // Additionally, Parser::ParseType assumes bool..string is a contiguous range
  30. // of type tokens.
  31. #define FLATBUFFERS_GEN_TYPES_SCALAR(TD) \
  32. TD(NONE, "", uint8_t, byte, byte, byte, uint8) \
  33. TD(UTYPE, "", uint8_t, byte, byte, byte, uint8) /* begin scalar/int */ \
  34. TD(BOOL, "bool", uint8_t, boolean,byte, bool, bool) \
  35. TD(CHAR, "byte", int8_t, byte, int8, sbyte, int8) \
  36. TD(UCHAR, "ubyte", uint8_t, byte, byte, byte, uint8) \
  37. TD(SHORT, "short", int16_t, short, int16, short, int16) \
  38. TD(USHORT, "ushort", uint16_t, short, uint16, ushort, uint16) \
  39. TD(INT, "int", int32_t, int, int32, int, int32) \
  40. TD(UINT, "uint", uint32_t, int, uint32, uint, uint32) \
  41. TD(LONG, "long", int64_t, long, int64, long, int64) \
  42. TD(ULONG, "ulong", uint64_t, long, uint64, ulong, uint64) /* end int */ \
  43. TD(FLOAT, "float", float, float, float32, float, float32) /* begin float */ \
  44. TD(DOUBLE, "double", double, double, float64, double, float64) /* end float/scalar */
  45. #define FLATBUFFERS_GEN_TYPES_POINTER(TD) \
  46. TD(STRING, "string", Offset<void>, int, int, StringOffset, int) \
  47. TD(VECTOR, "", Offset<void>, int, int, VectorOffset, int) \
  48. TD(STRUCT, "", Offset<void>, int, int, int, int) \
  49. TD(UNION, "", Offset<void>, int, int, int, int)
  50. // The fields are:
  51. // - enum
  52. // - FlatBuffers schema type.
  53. // - C++ type.
  54. // - Java type.
  55. // - Go type.
  56. // - C# / .Net type.
  57. // - Python type.
  58. // using these macros, we can now write code dealing with types just once, e.g.
  59. /*
  60. switch (type) {
  61. #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \
  62. case BASE_TYPE_ ## ENUM: \
  63. // do something specific to CTYPE here
  64. FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
  65. #undef FLATBUFFERS_TD
  66. }
  67. */
  68. #define FLATBUFFERS_GEN_TYPES(TD) \
  69. FLATBUFFERS_GEN_TYPES_SCALAR(TD) \
  70. FLATBUFFERS_GEN_TYPES_POINTER(TD)
  71. // Create an enum for all the types above.
  72. #ifdef __GNUC__
  73. __extension__ // Stop GCC complaining about trailing comma with -Wpendantic.
  74. #endif
  75. enum BaseType {
  76. #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \
  77. BASE_TYPE_ ## ENUM,
  78. FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
  79. #undef FLATBUFFERS_TD
  80. };
  81. #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \
  82. static_assert(sizeof(CTYPE) <= sizeof(largest_scalar_t), \
  83. "define largest_scalar_t as " #CTYPE);
  84. FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
  85. #undef FLATBUFFERS_TD
  86. inline bool IsScalar (BaseType t) { return t >= BASE_TYPE_UTYPE &&
  87. t <= BASE_TYPE_DOUBLE; }
  88. inline bool IsInteger(BaseType t) { return t >= BASE_TYPE_UTYPE &&
  89. t <= BASE_TYPE_ULONG; }
  90. inline bool IsFloat (BaseType t) { return t == BASE_TYPE_FLOAT ||
  91. t == BASE_TYPE_DOUBLE; }
  92. inline bool IsLong (BaseType t) { return t == BASE_TYPE_LONG ||
  93. t == BASE_TYPE_ULONG; }
  94. extern const char *const kTypeNames[];
  95. extern const char kTypeSizes[];
  96. inline size_t SizeOf(BaseType t) {
  97. return kTypeSizes[t];
  98. }
  99. struct StructDef;
  100. struct EnumDef;
  101. class Parser;
  102. // Represents any type in the IDL, which is a combination of the BaseType
  103. // and additional information for vectors/structs_.
  104. struct Type {
  105. explicit Type(BaseType _base_type = BASE_TYPE_NONE,
  106. StructDef *_sd = nullptr, EnumDef *_ed = nullptr)
  107. : base_type(_base_type),
  108. element(BASE_TYPE_NONE),
  109. struct_def(_sd),
  110. enum_def(_ed)
  111. {}
  112. bool operator==(const Type &o) {
  113. return base_type == o.base_type && element == o.element &&
  114. struct_def == o.struct_def && enum_def == o.enum_def;
  115. }
  116. Type VectorType() const { return Type(element, struct_def, enum_def); }
  117. Offset<reflection::Type> Serialize(FlatBufferBuilder *builder) const;
  118. BaseType base_type;
  119. BaseType element; // only set if t == BASE_TYPE_VECTOR
  120. StructDef *struct_def; // only set if t or element == BASE_TYPE_STRUCT
  121. EnumDef *enum_def; // set if t == BASE_TYPE_UNION / BASE_TYPE_UTYPE,
  122. // or for an integral type derived from an enum.
  123. };
  124. // Represents a parsed scalar value, it's type, and field offset.
  125. struct Value {
  126. Value() : constant("0"), offset(static_cast<voffset_t>(
  127. ~(static_cast<voffset_t>(0U)))) {}
  128. Type type;
  129. std::string constant;
  130. voffset_t offset;
  131. };
  132. // Helper class that retains the original order of a set of identifiers and
  133. // also provides quick lookup.
  134. template<typename T> class SymbolTable {
  135. public:
  136. ~SymbolTable() {
  137. for (auto it = vec.begin(); it != vec.end(); ++it) {
  138. delete *it;
  139. }
  140. }
  141. bool Add(const std::string &name, T *e) {
  142. vec.emplace_back(e);
  143. auto it = dict.find(name);
  144. if (it != dict.end()) return true;
  145. dict[name] = e;
  146. return false;
  147. }
  148. void Move(const std::string &oldname, const std::string &newname) {
  149. auto it = dict.find(oldname);
  150. if (it != dict.end()) {
  151. auto obj = it->second;
  152. dict.erase(it);
  153. dict[newname] = obj;
  154. } else {
  155. assert(false);
  156. }
  157. }
  158. T *Lookup(const std::string &name) const {
  159. auto it = dict.find(name);
  160. return it == dict.end() ? nullptr : it->second;
  161. }
  162. public:
  163. std::map<std::string, T *> dict; // quick lookup
  164. std::vector<T *> vec; // Used to iterate in order of insertion
  165. };
  166. // A name space, as set in the schema.
  167. struct Namespace {
  168. std::vector<std::string> components;
  169. // Given a (potentally unqualified) name, return the "fully qualified" name
  170. // which has a full namespaced descriptor.
  171. // With max_components you can request less than the number of components
  172. // the current namespace has.
  173. std::string GetFullyQualifiedName(const std::string &name,
  174. size_t max_components = 1000) const;
  175. };
  176. // Base class for all definition types (fields, structs_, enums_).
  177. struct Definition {
  178. Definition() : generated(false), defined_namespace(nullptr),
  179. serialized_location(0), index(-1) {}
  180. flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<
  181. reflection::KeyValue>>>
  182. SerializeAttributes(FlatBufferBuilder *builder,
  183. const Parser &parser) const;
  184. std::string name;
  185. std::string file;
  186. std::vector<std::string> doc_comment;
  187. SymbolTable<Value> attributes;
  188. bool generated; // did we already output code for this definition?
  189. Namespace *defined_namespace; // Where it was defined.
  190. // For use with Serialize()
  191. uoffset_t serialized_location;
  192. int index; // Inside the vector it is stored.
  193. };
  194. struct FieldDef : public Definition {
  195. FieldDef() : deprecated(false), required(false), key(false), padding(0) {}
  196. Offset<reflection::Field> Serialize(FlatBufferBuilder *builder, uint16_t id,
  197. const Parser &parser) const;
  198. Value value;
  199. bool deprecated; // Field is allowed to be present in old data, but can't be
  200. // written in new data nor accessed in new code.
  201. bool required; // Field must always be present.
  202. bool key; // Field functions as a key for creating sorted vectors.
  203. bool native_inline; // Field will be defined inline (instead of as a pointer)
  204. // for native tables if field is a struct.
  205. size_t padding; // Bytes to always pad after this field.
  206. };
  207. struct StructDef : public Definition {
  208. StructDef()
  209. : fixed(false),
  210. predecl(true),
  211. sortbysize(true),
  212. has_key(false),
  213. minalign(1),
  214. bytesize(0)
  215. {}
  216. void PadLastField(size_t min_align) {
  217. auto padding = PaddingBytes(bytesize, min_align);
  218. bytesize += padding;
  219. if (fields.vec.size()) fields.vec.back()->padding = padding;
  220. }
  221. Offset<reflection::Object> Serialize(FlatBufferBuilder *builder,
  222. const Parser &parser) const;
  223. SymbolTable<FieldDef> fields;
  224. bool fixed; // If it's struct, not a table.
  225. bool predecl; // If it's used before it was defined.
  226. bool sortbysize; // Whether fields come in the declaration or size order.
  227. bool has_key; // It has a key field.
  228. size_t minalign; // What the whole object needs to be aligned to.
  229. size_t bytesize; // Size if fixed.
  230. };
  231. inline bool IsStruct(const Type &type) {
  232. return type.base_type == BASE_TYPE_STRUCT && type.struct_def->fixed;
  233. }
  234. inline size_t InlineSize(const Type &type) {
  235. return IsStruct(type) ? type.struct_def->bytesize : SizeOf(type.base_type);
  236. }
  237. inline size_t InlineAlignment(const Type &type) {
  238. return IsStruct(type) ? type.struct_def->minalign : SizeOf(type.base_type);
  239. }
  240. struct EnumVal {
  241. EnumVal(const std::string &_name, int64_t _val)
  242. : name(_name), value(_val), struct_def(nullptr) {}
  243. Offset<reflection::EnumVal> Serialize(FlatBufferBuilder *builder) const;
  244. std::string name;
  245. std::vector<std::string> doc_comment;
  246. int64_t value;
  247. StructDef *struct_def; // only set if this is a union
  248. };
  249. struct EnumDef : public Definition {
  250. EnumDef() : is_union(false) {}
  251. EnumVal *ReverseLookup(int enum_idx, bool skip_union_default = true) {
  252. for (auto it = vals.vec.begin() + static_cast<int>(is_union &&
  253. skip_union_default);
  254. it != vals.vec.end(); ++it) {
  255. if ((*it)->value == enum_idx) {
  256. return *it;
  257. }
  258. }
  259. return nullptr;
  260. }
  261. Offset<reflection::Enum> Serialize(FlatBufferBuilder *builder,
  262. const Parser &parser) const;
  263. SymbolTable<EnumVal> vals;
  264. bool is_union;
  265. Type underlying_type;
  266. };
  267. inline bool EqualByName(const Type &a, const Type &b) {
  268. return a.base_type == b.base_type && a.element == b.element &&
  269. (a.struct_def == b.struct_def ||
  270. a.struct_def->name == b.struct_def->name) &&
  271. (a.enum_def == b.enum_def ||
  272. a.enum_def->name == b.enum_def->name);
  273. }
  274. struct RPCCall {
  275. std::string name;
  276. SymbolTable<Value> attributes;
  277. StructDef *request, *response;
  278. };
  279. struct ServiceDef : public Definition {
  280. SymbolTable<RPCCall> calls;
  281. };
  282. // Container of options that may apply to any of the source/text generators.
  283. struct IDLOptions {
  284. bool strict_json;
  285. bool skip_js_exports;
  286. bool output_default_scalars_in_json;
  287. int indent_step;
  288. bool output_enum_identifiers;
  289. bool prefixed_enums;
  290. bool scoped_enums;
  291. bool include_dependence_headers;
  292. bool mutable_buffer;
  293. bool one_file;
  294. bool proto_mode;
  295. bool generate_all;
  296. bool skip_unexpected_fields_in_json;
  297. bool generate_name_strings;
  298. bool escape_proto_identifiers;
  299. bool generate_object_based_api;
  300. std::string cpp_object_api_pointer_type;
  301. bool union_value_namespacing;
  302. bool allow_non_utf8;
  303. // Possible options for the more general generator below.
  304. enum Language { kJava, kCSharp, kGo, kMAX };
  305. Language lang;
  306. IDLOptions()
  307. : strict_json(false),
  308. skip_js_exports(false),
  309. output_default_scalars_in_json(false),
  310. indent_step(2),
  311. output_enum_identifiers(true), prefixed_enums(true), scoped_enums(false),
  312. include_dependence_headers(true),
  313. mutable_buffer(false),
  314. one_file(false),
  315. proto_mode(false),
  316. generate_all(false),
  317. skip_unexpected_fields_in_json(false),
  318. generate_name_strings(false),
  319. escape_proto_identifiers(false),
  320. generate_object_based_api(false),
  321. cpp_object_api_pointer_type("std::unique_ptr"),
  322. union_value_namespacing(true),
  323. allow_non_utf8(false),
  324. lang(IDLOptions::kJava) {}
  325. };
  326. // This encapsulates where the parser is in the current source file.
  327. struct ParserState {
  328. ParserState() : cursor_(nullptr), line_(1), token_(-1) {}
  329. protected:
  330. const char *cursor_;
  331. int line_; // the current line being parsed
  332. int token_;
  333. std::string attribute_;
  334. std::vector<std::string> doc_comment_;
  335. };
  336. // A way to make error propagation less error prone by requiring values to be
  337. // checked.
  338. // Once you create a value of this type you must either:
  339. // - Call Check() on it.
  340. // - Copy or assign it to another value.
  341. // Failure to do so leads to an assert.
  342. // This guarantees that this as return value cannot be ignored.
  343. class CheckedError {
  344. public:
  345. explicit CheckedError(bool error)
  346. : is_error_(error), has_been_checked_(false) {}
  347. CheckedError &operator=(const CheckedError &other) {
  348. is_error_ = other.is_error_;
  349. has_been_checked_ = false;
  350. other.has_been_checked_ = true;
  351. return *this;
  352. }
  353. CheckedError(const CheckedError &other) {
  354. *this = other; // Use assignment operator.
  355. }
  356. ~CheckedError() { assert(has_been_checked_); }
  357. bool Check() { has_been_checked_ = true; return is_error_; }
  358. private:
  359. bool is_error_;
  360. mutable bool has_been_checked_;
  361. };
  362. // Additionally, in GCC we can get these errors statically, for additional
  363. // assurance:
  364. #ifdef __GNUC__
  365. #define FLATBUFFERS_CHECKED_ERROR CheckedError \
  366. __attribute__((warn_unused_result))
  367. #else
  368. #define FLATBUFFERS_CHECKED_ERROR CheckedError
  369. #endif
  370. class Parser : public ParserState {
  371. public:
  372. explicit Parser(const IDLOptions &options = IDLOptions())
  373. : root_struct_def_(nullptr),
  374. opts(options),
  375. source_(nullptr),
  376. anonymous_counter(0) {
  377. // Just in case none are declared:
  378. namespaces_.push_back(new Namespace());
  379. known_attributes_["deprecated"] = true;
  380. known_attributes_["required"] = true;
  381. known_attributes_["key"] = true;
  382. known_attributes_["hash"] = true;
  383. known_attributes_["id"] = true;
  384. known_attributes_["force_align"] = true;
  385. known_attributes_["bit_flags"] = true;
  386. known_attributes_["original_order"] = true;
  387. known_attributes_["nested_flatbuffer"] = true;
  388. known_attributes_["csharp_partial"] = true;
  389. known_attributes_["streaming"] = true;
  390. known_attributes_["idempotent"] = true;
  391. known_attributes_["cpp_type"] = true;
  392. known_attributes_["cpp_ptr_type"] = true;
  393. known_attributes_["native_inline"] = true;
  394. }
  395. ~Parser() {
  396. for (auto it = namespaces_.begin(); it != namespaces_.end(); ++it) {
  397. delete *it;
  398. }
  399. }
  400. // Parse the string containing either schema or JSON data, which will
  401. // populate the SymbolTable's or the FlatBufferBuilder above.
  402. // include_paths is used to resolve any include statements, and typically
  403. // should at least include the project path (where you loaded source_ from).
  404. // include_paths must be nullptr terminated if specified.
  405. // If include_paths is nullptr, it will attempt to load from the current
  406. // directory.
  407. // If the source was loaded from a file and isn't an include file,
  408. // supply its name in source_filename.
  409. bool Parse(const char *_source, const char **include_paths = nullptr,
  410. const char *source_filename = nullptr);
  411. // Set the root type. May override the one set in the schema.
  412. bool SetRootType(const char *name);
  413. // Mark all definitions as already having code generated.
  414. void MarkGenerated();
  415. // Get the files recursively included by the given file. The returned
  416. // container will have at least the given file.
  417. std::set<std::string> GetIncludedFilesRecursive(
  418. const std::string &file_name) const;
  419. // Fills builder_ with a binary version of the schema parsed.
  420. // See reflection/reflection.fbs
  421. void Serialize();
  422. // Checks that the schema represented by this parser is a safe evolution
  423. // of the schema provided. Returns non-empty error on any problems.
  424. std::string ConformTo(const Parser &base);
  425. FLATBUFFERS_CHECKED_ERROR CheckBitsFit(int64_t val, size_t bits);
  426. private:
  427. FLATBUFFERS_CHECKED_ERROR Error(const std::string &msg);
  428. FLATBUFFERS_CHECKED_ERROR ParseHexNum(int nibbles, int64_t *val);
  429. FLATBUFFERS_CHECKED_ERROR Next();
  430. FLATBUFFERS_CHECKED_ERROR SkipByteOrderMark();
  431. bool Is(int t);
  432. FLATBUFFERS_CHECKED_ERROR Expect(int t);
  433. std::string TokenToStringId(int t);
  434. EnumDef *LookupEnum(const std::string &id);
  435. FLATBUFFERS_CHECKED_ERROR ParseNamespacing(std::string *id,
  436. std::string *last);
  437. FLATBUFFERS_CHECKED_ERROR ParseTypeIdent(Type &type);
  438. FLATBUFFERS_CHECKED_ERROR ParseType(Type &type);
  439. FLATBUFFERS_CHECKED_ERROR AddField(StructDef &struct_def,
  440. const std::string &name, const Type &type,
  441. FieldDef **dest);
  442. FLATBUFFERS_CHECKED_ERROR ParseField(StructDef &struct_def);
  443. FLATBUFFERS_CHECKED_ERROR ParseAnyValue(Value &val, FieldDef *field,
  444. size_t parent_fieldn,
  445. const StructDef *parent_struct_def);
  446. FLATBUFFERS_CHECKED_ERROR ParseTable(const StructDef &struct_def,
  447. std::string *value, uoffset_t *ovalue);
  448. void SerializeStruct(const StructDef &struct_def, const Value &val);
  449. void AddVector(bool sortbysize, int count);
  450. FLATBUFFERS_CHECKED_ERROR ParseVector(const Type &type, uoffset_t *ovalue);
  451. FLATBUFFERS_CHECKED_ERROR ParseMetaData(SymbolTable<Value> *attributes);
  452. FLATBUFFERS_CHECKED_ERROR TryTypedValue(int dtoken, bool check, Value &e,
  453. BaseType req, bool *destmatch);
  454. FLATBUFFERS_CHECKED_ERROR ParseHash(Value &e, FieldDef* field);
  455. FLATBUFFERS_CHECKED_ERROR ParseSingleValue(Value &e);
  456. FLATBUFFERS_CHECKED_ERROR ParseEnumFromString(Type &type, int64_t *result);
  457. StructDef *LookupCreateStruct(const std::string &name,
  458. bool create_if_new = true,
  459. bool definition = false);
  460. FLATBUFFERS_CHECKED_ERROR ParseEnum(bool is_union, EnumDef **dest);
  461. FLATBUFFERS_CHECKED_ERROR ParseNamespace();
  462. FLATBUFFERS_CHECKED_ERROR StartStruct(const std::string &name,
  463. StructDef **dest);
  464. FLATBUFFERS_CHECKED_ERROR ParseDecl();
  465. FLATBUFFERS_CHECKED_ERROR ParseService();
  466. FLATBUFFERS_CHECKED_ERROR ParseProtoFields(StructDef *struct_def,
  467. bool isextend, bool inside_oneof);
  468. FLATBUFFERS_CHECKED_ERROR ParseProtoOption();
  469. FLATBUFFERS_CHECKED_ERROR ParseProtoKey();
  470. FLATBUFFERS_CHECKED_ERROR ParseProtoDecl();
  471. FLATBUFFERS_CHECKED_ERROR ParseProtoCurliesOrIdent();
  472. FLATBUFFERS_CHECKED_ERROR ParseTypeFromProtoType(Type *type);
  473. FLATBUFFERS_CHECKED_ERROR SkipAnyJsonValue();
  474. FLATBUFFERS_CHECKED_ERROR SkipJsonObject();
  475. FLATBUFFERS_CHECKED_ERROR SkipJsonArray();
  476. FLATBUFFERS_CHECKED_ERROR SkipJsonString();
  477. FLATBUFFERS_CHECKED_ERROR DoParse(const char *_source,
  478. const char **include_paths,
  479. const char *source_filename);
  480. FLATBUFFERS_CHECKED_ERROR CheckClash(std::vector<FieldDef*> &fields,
  481. StructDef *struct_def,
  482. const char *suffix,
  483. BaseType baseType);
  484. public:
  485. SymbolTable<StructDef> structs_;
  486. SymbolTable<EnumDef> enums_;
  487. SymbolTable<ServiceDef> services_;
  488. std::vector<Namespace *> namespaces_;
  489. std::string error_; // User readable error_ if Parse() == false
  490. FlatBufferBuilder builder_; // any data contained in the file
  491. StructDef *root_struct_def_;
  492. std::string file_identifier_;
  493. std::string file_extension_;
  494. std::map<std::string, bool> included_files_;
  495. std::map<std::string, std::set<std::string>> files_included_per_file_;
  496. std::map<std::string, bool> known_attributes_;
  497. IDLOptions opts;
  498. private:
  499. const char *source_;
  500. std::string file_being_parsed_;
  501. std::vector<std::pair<Value, FieldDef *>> field_stack_;
  502. int anonymous_counter;
  503. };
  504. // Utility functions for multiple generators:
  505. extern std::string MakeCamel(const std::string &in, bool first = true);
  506. struct CommentConfig;
  507. extern void GenComment(const std::vector<std::string> &dc,
  508. std::string *code_ptr,
  509. const CommentConfig *config,
  510. const char *prefix = "");
  511. // Generate text (JSON) from a given FlatBuffer, and a given Parser
  512. // object that has been populated with the corresponding schema.
  513. // If ident_step is 0, no indentation will be generated. Additionally,
  514. // if it is less than 0, no linefeeds will be generated either.
  515. // See idl_gen_text.cpp.
  516. // strict_json adds "quotes" around field names if true.
  517. // If the flatbuffer cannot be encoded in JSON (e.g., it contains non-UTF-8
  518. // byte arrays in String values), returns false.
  519. extern bool GenerateText(const Parser &parser,
  520. const void *flatbuffer,
  521. std::string *text);
  522. extern bool GenerateTextFile(const Parser &parser,
  523. const std::string &path,
  524. const std::string &file_name);
  525. // Generate binary files from a given FlatBuffer, and a given Parser
  526. // object that has been populated with the corresponding schema.
  527. // See idl_gen_general.cpp.
  528. extern bool GenerateBinary(const Parser &parser,
  529. const std::string &path,
  530. const std::string &file_name);
  531. // Generate a C++ header from the definitions in the Parser object.
  532. // See idl_gen_cpp.
  533. extern std::string GenerateCPP(const Parser &parser,
  534. const std::string &include_guard_ident);
  535. extern bool GenerateCPP(const Parser &parser,
  536. const std::string &path,
  537. const std::string &file_name);
  538. // Generate JavaScript code from the definitions in the Parser object.
  539. // See idl_gen_js.
  540. extern std::string GenerateJS(const Parser &parser);
  541. extern bool GenerateJS(const Parser &parser,
  542. const std::string &path,
  543. const std::string &file_name);
  544. // Generate Go files from the definitions in the Parser object.
  545. // See idl_gen_go.cpp.
  546. extern bool GenerateGo(const Parser &parser,
  547. const std::string &path,
  548. const std::string &file_name);
  549. // Generate Java files from the definitions in the Parser object.
  550. // See idl_gen_java.cpp.
  551. extern bool GenerateJava(const Parser &parser,
  552. const std::string &path,
  553. const std::string &file_name);
  554. // Generate Php code from the definitions in the Parser object.
  555. // See idl_gen_php.
  556. extern bool GeneratePhp(const Parser &parser,
  557. const std::string &path,
  558. const std::string &file_name);
  559. // Generate Python files from the definitions in the Parser object.
  560. // See idl_gen_python.cpp.
  561. extern bool GeneratePython(const Parser &parser,
  562. const std::string &path,
  563. const std::string &file_name);
  564. // Generate C# files from the definitions in the Parser object.
  565. // See idl_gen_csharp.cpp.
  566. extern bool GenerateCSharp(const Parser &parser,
  567. const std::string &path,
  568. const std::string &file_name);
  569. // Generate Java/C#/.. files from the definitions in the Parser object.
  570. // See idl_gen_general.cpp.
  571. extern bool GenerateGeneral(const Parser &parser,
  572. const std::string &path,
  573. const std::string &file_name);
  574. // Generate a schema file from the internal representation, useful after
  575. // parsing a .proto schema.
  576. extern std::string GenerateFBS(const Parser &parser,
  577. const std::string &file_name);
  578. extern bool GenerateFBS(const Parser &parser,
  579. const std::string &path,
  580. const std::string &file_name);
  581. // Generate a make rule for the generated JavaScript code.
  582. // See idl_gen_js.cpp.
  583. extern std::string JSMakeRule(const Parser &parser,
  584. const std::string &path,
  585. const std::string &file_name);
  586. // Generate a make rule for the generated C++ header.
  587. // See idl_gen_cpp.cpp.
  588. extern std::string CPPMakeRule(const Parser &parser,
  589. const std::string &path,
  590. const std::string &file_name);
  591. // Generate a make rule for the generated Java/C#/... files.
  592. // See idl_gen_general.cpp.
  593. extern std::string GeneralMakeRule(const Parser &parser,
  594. const std::string &path,
  595. const std::string &file_name);
  596. // Generate a make rule for the generated text (JSON) files.
  597. // See idl_gen_text.cpp.
  598. extern std::string TextMakeRule(const Parser &parser,
  599. const std::string &path,
  600. const std::string &file_names);
  601. // Generate a make rule for the generated binary files.
  602. // See idl_gen_general.cpp.
  603. extern std::string BinaryMakeRule(const Parser &parser,
  604. const std::string &path,
  605. const std::string &file_name);
  606. // Generate GRPC Cpp interfaces.
  607. // See idl_gen_grpc.cpp.
  608. bool GenerateCppGRPC(const Parser &parser,
  609. const std::string &path,
  610. const std::string &file_name);
  611. // Generate GRPC Go interfaces.
  612. // See idl_gen_grpc.cpp.
  613. bool GenerateGoGRPC(const Parser &parser,
  614. const std::string &path,
  615. const std::string &file_name);
  616. } // namespace flatbuffers
  617. #endif // FLATBUFFERS_IDL_H_