2 * Copyright 2014 Google Inc. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 // independent from idl_parser, since this code is not needed for most clients
21 #include "flatbuffers/code_generators.h"
22 #include "flatbuffers/flatbuffers.h"
23 #include "flatbuffers/idl.h"
24 #include "flatbuffers/util.h"
26 namespace flatbuffers {
28 // Hardcode spaces per indentation.
29 const std::string Indent = " ";
30 class PhpGenerator : public BaseGenerator {
32 PhpGenerator(const Parser &parser, const std::string &path,
33 const std::string &file_name)
34 : BaseGenerator(parser, path, file_name, "\\", "\\") {}
36 if (!GenerateEnums()) return false;
37 if (!GenerateStructs()) return false;
42 bool GenerateEnums() {
43 for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
45 auto &enum_def = **it;
47 GenEnum(enum_def, &enumcode);
48 if (!SaveType(enum_def, enumcode, false)) return false;
53 bool GenerateStructs() {
54 for (auto it = parser_.structs_.vec.begin();
55 it != parser_.structs_.vec.end(); ++it) {
56 auto &struct_def = **it;
58 GenStruct(struct_def, &declcode);
59 if (!SaveType(struct_def, declcode, true)) return false;
64 // Begin by declaring namespace and imports.
65 void BeginFile(const std::string &name_space_name, const bool needs_imports,
66 std::string *code_ptr) {
67 auto &code = *code_ptr;
69 code = code + "// " + FlatBuffersGeneratedWarning() + "\n\n";
71 if (!name_space_name.empty()) {
72 code += "namespace " + name_space_name + ";\n\n";
76 code += "use \\Google\\FlatBuffers\\Struct;\n";
77 code += "use \\Google\\FlatBuffers\\Table;\n";
78 code += "use \\Google\\FlatBuffers\\ByteBuffer;\n";
79 code += "use \\Google\\FlatBuffers\\FlatBufferBuilder;\n";
84 // Save out the generated code for a Php Table type.
85 bool SaveType(const Definition &def, const std::string &classcode,
87 if (!classcode.length()) return true;
89 std::string code = "";
90 BeginFile(FullNamespace("\\", *def.defined_namespace), needs_imports,
94 std::string filename =
95 NamespaceDir(*def.defined_namespace) + def.name + ".php";
96 return SaveFile(filename.c_str(), code, false);
99 // Begin a class declaration.
100 static void BeginClass(const StructDef &struct_def, std::string *code_ptr) {
101 std::string &code = *code_ptr;
102 if (struct_def.fixed) {
103 code += "class " + struct_def.name + " extends Struct\n";
105 code += "class " + struct_def.name + " extends Table\n";
110 static void EndClass(std::string *code_ptr) {
111 std::string &code = *code_ptr;
115 // Begin enum code with a class declaration.
116 static void BeginEnum(const std::string &class_name, std::string *code_ptr) {
117 std::string &code = *code_ptr;
118 code += "class " + class_name + "\n{\n";
121 // A single enum member.
122 static void EnumMember(const EnumDef &enum_def, const EnumVal &ev,
123 std::string *code_ptr) {
124 std::string &code = *code_ptr;
125 code += Indent + "const ";
128 code += enum_def.ToString(ev) + ";\n";
132 static void EndEnum(std::string *code_ptr) {
133 std::string &code = *code_ptr;
137 // Initialize a new struct or table from existing data.
138 static void NewRootTypeFromBuffer(const StructDef &struct_def,
139 std::string *code_ptr) {
140 std::string &code = *code_ptr;
142 code += Indent + "/**\n";
143 code += Indent + " * @param ByteBuffer $bb\n";
144 code += Indent + " * @return " + struct_def.name + "\n";
145 code += Indent + " */\n";
146 code += Indent + "public static function getRootAs";
147 code += struct_def.name;
148 code += "(ByteBuffer $bb)\n";
149 code += Indent + "{\n";
151 code += Indent + Indent + "$obj = new " + struct_def.name + "();\n";
152 code += Indent + Indent;
153 code += "return ($obj->init($bb->getInt($bb->getPosition())";
154 code += " + $bb->getPosition(), $bb));\n";
155 code += Indent + "}\n\n";
158 // Initialize an existing object with other data, to avoid an allocation.
159 static void InitializeExisting(const StructDef &struct_def,
160 std::string *code_ptr) {
161 std::string &code = *code_ptr;
163 code += Indent + "/**\n";
164 code += Indent + " * @param int $_i offset\n";
165 code += Indent + " * @param ByteBuffer $_bb\n";
166 code += Indent + " * @return " + struct_def.name + "\n";
167 code += Indent + " **/\n";
168 code += Indent + "public function init($_i, ByteBuffer $_bb)\n";
169 code += Indent + "{\n";
170 code += Indent + Indent + "$this->bb_pos = $_i;\n";
171 code += Indent + Indent + "$this->bb = $_bb;\n";
172 code += Indent + Indent + "return $this;\n";
173 code += Indent + "}\n\n";
176 // Get the length of a vector.
177 static void GetVectorLen(const FieldDef &field, std::string *code_ptr) {
178 std::string &code = *code_ptr;
180 code += Indent + "/**\n";
181 code += Indent + " * @return int\n";
182 code += Indent + " */\n";
183 code += Indent + "public function get";
184 code += MakeCamel(field.name) + "Length()\n";
185 code += Indent + "{\n";
186 code += Indent + Indent + "$o = $this->__offset(";
187 code += NumToString(field.value.offset) + ");\n";
188 code += Indent + Indent;
189 code += "return $o != 0 ? $this->__vector_len($o) : 0;\n";
190 code += Indent + "}\n\n";
193 // Get a [ubyte] vector as a byte array.
194 static void GetUByte(const FieldDef &field, std::string *code_ptr) {
195 std::string &code = *code_ptr;
197 code += Indent + "/**\n";
198 code += Indent + " * @return string\n";
199 code += Indent + " */\n";
200 code += Indent + "public function get";
201 code += MakeCamel(field.name) + "Bytes()\n";
202 code += Indent + "{\n";
203 code += Indent + Indent + "return $this->__vector_as_bytes(";
204 code += NumToString(field.value.offset) + ");\n";
205 code += Indent + "}\n\n";
208 // Get the value of a struct's scalar.
209 static void GetScalarFieldOfStruct(const FieldDef &field,
210 std::string *code_ptr) {
211 std::string &code = *code_ptr;
212 std::string getter = GenGetter(field.value.type);
214 code += Indent + "/**\n";
215 code += Indent + " * @return ";
216 code += GenTypeGet(field.value.type) + "\n";
217 code += Indent + " */\n";
218 code += Indent + "public function " + getter;
219 code += MakeCamel(field.name) + "()\n";
220 code += Indent + "{\n";
221 code += Indent + Indent + "return ";
223 code += "$this->bb->get";
224 code += MakeCamel(GenTypeGet(field.value.type));
225 code += "($this->bb_pos + ";
226 code += NumToString(field.value.offset) + ")";
229 code += Indent + "}\n\n";
232 // Get the value of a table's scalar.
233 void GetScalarFieldOfTable(const FieldDef &field, std::string *code_ptr) {
234 std::string &code = *code_ptr;
236 code += Indent + "/**\n";
237 code += Indent + " * @return " + GenTypeGet(field.value.type) + "\n";
238 code += Indent + " */\n";
239 code += Indent + "public function get";
240 code += MakeCamel(field.name);
242 code += Indent + "{\n";
243 code += Indent + Indent + "$o = $this->__offset(" +
244 NumToString(field.value.offset) + ");\n" + Indent + Indent +
246 code += "$this->bb->get";
247 code += MakeCamel(GenTypeGet(field.value.type)) + "($o + $this->bb_pos)";
248 code += " : " + GenDefaultValue(field.value) + ";\n";
249 code += Indent + "}\n\n";
252 // Get a struct by initializing an existing struct.
253 // Specific to Struct.
254 void GetStructFieldOfStruct(const FieldDef &field, std::string *code_ptr) {
255 std::string &code = *code_ptr;
257 code += Indent + "/**\n";
258 code += Indent + " * @return " + GenTypeGet(field.value.type) + "\n";
259 code += Indent + " */\n";
260 code += Indent + "public function get";
261 code += MakeCamel(field.name) + "()\n";
262 code += Indent + "{\n";
263 code += Indent + Indent + "$obj = new ";
264 code += GenTypeGet(field.value.type) + "();\n";
265 code += Indent + Indent + "$obj->init($this->bb_pos + ";
266 code += NumToString(field.value.offset) + ", $this->bb);";
267 code += "\n" + Indent + Indent + "return $obj;\n";
268 code += Indent + "}\n\n";
271 // Get a struct by initializing an existing struct.
272 // Specific to Table.
273 void GetStructFieldOfTable(const FieldDef &field, std::string *code_ptr) {
274 std::string &code = *code_ptr;
276 code += Indent + "public function get";
277 code += MakeCamel(field.name);
279 code += Indent + "{\n";
280 code += Indent + Indent + "$obj = new ";
281 code += MakeCamel(GenTypeGet(field.value.type)) + "();\n";
282 code += Indent + Indent + "$o = $this->__offset(" +
283 NumToString(field.value.offset) + ");\n";
284 code += Indent + Indent;
285 code += "return $o != 0 ? $obj->init(";
286 if (field.value.type.struct_def->fixed) {
287 code += "$o + $this->bb_pos, $this->bb) : ";
289 code += "$this->__indirect($o + $this->bb_pos), $this->bb) : ";
291 code += GenDefaultValue(field.value) + ";\n";
292 code += Indent + "}\n\n";
295 // Get the value of a string.
296 void GetStringField(const FieldDef &field, std::string *code_ptr) {
297 std::string &code = *code_ptr;
298 code += Indent + "public function get";
299 code += MakeCamel(field.name);
301 code += Indent + "{\n";
302 code += Indent + Indent + "$o = $this->__offset(" +
303 NumToString(field.value.offset) + ");\n";
304 code += Indent + Indent;
305 code += "return $o != 0 ? $this->__string($o + $this->bb_pos) : ";
306 code += GenDefaultValue(field.value) + ";\n";
307 code += Indent + "}\n\n";
310 // Get the value of a union from an object.
311 void GetUnionField(const FieldDef &field, std::string *code_ptr) {
312 std::string &code = *code_ptr;
314 code += Indent + "/**\n";
315 code += Indent + " * @return" + GenTypeBasic(field.value.type) + "\n";
316 code += Indent + " */\n";
317 code += Indent + "public function get";
318 code += MakeCamel(field.name) + "($obj)\n";
319 code += Indent + "{\n";
320 code += Indent + Indent + "$o = $this->__offset(" +
321 NumToString(field.value.offset) + ");\n";
322 code += Indent + Indent;
323 code += "return $o != 0 ? $this->__union($obj, $o) : null;\n";
324 code += Indent + "}\n\n";
327 // Get the value of a vector's struct member.
328 void GetMemberOfVectorOfStruct(const StructDef &struct_def,
329 const FieldDef &field, std::string *code_ptr) {
330 std::string &code = *code_ptr;
331 auto vectortype = field.value.type.VectorType();
333 code += Indent + "/**\n";
334 code += Indent + " * @return" + GenTypeBasic(field.value.type) + "\n";
335 code += Indent + " */\n";
336 code += Indent + "public function get";
337 code += MakeCamel(field.name);
339 code += Indent + "{\n";
340 code += Indent + Indent + "$o = $this->__offset(" +
341 NumToString(field.value.offset) + ");\n";
342 code += Indent + Indent + "$obj = new ";
343 code += MakeCamel(GenTypeGet(field.value.type)) + "();\n";
345 switch (field.value.type.base_type) {
346 case BASE_TYPE_STRUCT:
347 if (struct_def.fixed) {
348 code += Indent + Indent;
349 code += "return $o != 0 ? $obj->init($this->bb_pos +" +
350 NumToString(field.value.offset) + ", $this->bb) : null;\n";
352 code += Indent + Indent + "return $o != 0 ? $obj->init(";
353 code += field.value.type.struct_def->fixed
354 ? "$o + $this->bb_pos"
355 : "$this->__indirect($o + $this->bb_pos)";
356 code += ", $this->bb) : null;\n";
359 case BASE_TYPE_STRING:
360 code += "// base_type_string\n";
361 // TODO(chobie): do we need this?
363 case BASE_TYPE_VECTOR:
364 if (vectortype.base_type == BASE_TYPE_STRUCT) {
365 code += Indent + Indent + "return $o != 0 ? $obj->init(";
366 if (vectortype.struct_def->fixed) {
367 code += "$this->__vector($o) + $j *";
368 code += NumToString(InlineSize(vectortype));
370 code += "$this->__indirect($this->__vector($o) + $j * ";
371 code += NumToString(InlineSize(vectortype)) + ")";
373 code += ", $this->bb) : null;\n";
376 case BASE_TYPE_UNION:
377 code += Indent + Indent + "return $o != 0 ? $this->";
378 code += GenGetter(field.value.type) + "($obj, $o); null;\n";
383 code += Indent + "}\n\n";
386 // Get the value of a vector's non-struct member. Uses a named return
387 // argument to conveniently set the zero value for the result.
388 void GetMemberOfVectorOfNonStruct(const FieldDef &field,
389 std::string *code_ptr) {
390 std::string &code = *code_ptr;
391 auto vectortype = field.value.type.VectorType();
393 code += Indent + "/**\n";
394 code += Indent + " * @param int offset\n";
395 code += Indent + " * @return " + GenTypeGet(field.value.type) + "\n";
396 code += Indent + " */\n";
397 code += Indent + "public function get";
398 code += MakeCamel(field.name);
400 code += Indent + "{\n";
401 code += Indent + Indent + "$o = $this->__offset(" +
402 NumToString(field.value.offset) + ");\n";
404 if (field.value.type.VectorType().base_type == BASE_TYPE_STRING) {
405 code += Indent + Indent;
406 code += "return $o != 0 ? $this->__string($this->__vector($o) + $j * ";
407 code += NumToString(InlineSize(vectortype)) + ") : ";
408 code += GenDefaultValue(field.value) + ";\n";
410 code += Indent + Indent + "return $o != 0 ? $this->bb->get";
411 code += MakeCamel(GenTypeGet(field.value.type));
412 code += "($this->__vector($o) + $j * ";
413 code += NumToString(InlineSize(vectortype)) + ") : ";
414 code += GenDefaultValue(field.value) + ";\n";
416 code += Indent + "}\n\n";
419 // Get the value of a vector's union member. Uses a named return
420 // argument to conveniently set the zero value for the result.
421 void GetMemberOfVectorOfUnion(const FieldDef &field, std::string *code_ptr) {
422 std::string &code = *code_ptr;
423 auto vectortype = field.value.type.VectorType();
425 code += Indent + "/**\n";
426 code += Indent + " * @param int offset\n";
427 code += Indent + " * @return " + GenTypeGet(field.value.type) + "\n";
428 code += Indent + " */\n";
429 code += Indent + "public function get";
430 code += MakeCamel(field.name);
431 code += "($j, $obj)\n";
432 code += Indent + "{\n";
433 code += Indent + Indent + "$o = $this->__offset(" +
434 NumToString(field.value.offset) + ");\n";
435 code += Indent + Indent + "return $o != 0 ? ";
436 code += "$this->__union($obj, $this->__vector($o) + $j * ";
437 code += NumToString(InlineSize(vectortype)) + " - $this->bb_pos) : null;\n";
438 code += Indent + "}\n\n";
441 // Recursively generate arguments for a constructor, to deal with nested
443 static void StructBuilderArgs(const StructDef &struct_def,
444 const char *nameprefix, std::string *code_ptr) {
445 for (auto it = struct_def.fields.vec.begin();
446 it != struct_def.fields.vec.end(); ++it) {
448 if (IsStruct(field.value.type)) {
449 // Generate arguments for a struct inside a struct. To ensure names
450 // don't clash, and to make it obvious
451 // these arguments are constructing
452 // a nested struct, prefix the name with the field name.
453 StructBuilderArgs(*field.value.type.struct_def,
454 (nameprefix + (field.name + "_")).c_str(), code_ptr);
456 std::string &code = *code_ptr;
457 code += std::string(", $") + nameprefix;
458 code += MakeCamel(field.name, false);
463 // Recursively generate struct construction statements and instert manual
465 static void StructBuilderBody(const StructDef &struct_def,
466 const char *nameprefix, std::string *code_ptr) {
467 std::string &code = *code_ptr;
468 code += Indent + Indent + "$builder->prep(";
469 code += NumToString(struct_def.minalign) + ", ";
470 code += NumToString(struct_def.bytesize) + ");\n";
471 for (auto it = struct_def.fields.vec.rbegin();
472 it != struct_def.fields.vec.rend(); ++it) {
475 code += Indent + Indent + "$builder->pad(";
476 code += NumToString(field.padding) + ");\n";
478 if (IsStruct(field.value.type)) {
479 StructBuilderBody(*field.value.type.struct_def,
480 (nameprefix + (field.name + "_")).c_str(), code_ptr);
482 code += Indent + Indent + "$builder->put" + GenMethod(field) + "($";
483 code += nameprefix + MakeCamel(field.name, false) + ");\n";
488 // Get the value of a table's starting offset.
489 static void GetStartOfTable(const StructDef &struct_def,
490 std::string *code_ptr) {
491 std::string &code = *code_ptr;
493 code += Indent + "/**\n";
494 code += Indent + " * @param FlatBufferBuilder $builder\n";
495 code += Indent + " * @return void\n";
496 code += Indent + " */\n";
497 code += Indent + "public static function start" + struct_def.name;
498 code += "(FlatBufferBuilder $builder)\n";
499 code += Indent + "{\n";
500 code += Indent + Indent + "$builder->StartObject(";
501 code += NumToString(struct_def.fields.vec.size());
503 code += Indent + "}\n\n";
505 code += Indent + "/**\n";
506 code += Indent + " * @param FlatBufferBuilder $builder\n";
507 code += Indent + " * @return " + struct_def.name + "\n";
508 code += Indent + " */\n";
509 code += Indent + "public static function create" + struct_def.name;
510 code += "(FlatBufferBuilder $builder, ";
512 for (auto it = struct_def.fields.vec.begin();
513 it != struct_def.fields.vec.end(); ++it) {
516 if (field.deprecated) continue;
517 code += "$" + field.name;
518 if (!(it == (--struct_def.fields.vec.end()))) { code += ", "; }
521 code += Indent + "{\n";
522 code += Indent + Indent + "$builder->startObject(";
523 code += NumToString(struct_def.fields.vec.size());
525 for (auto it = struct_def.fields.vec.begin();
526 it != struct_def.fields.vec.end(); ++it) {
528 if (field.deprecated) continue;
530 code += Indent + Indent + "self::add";
531 code += MakeCamel(field.name) + "($builder, $" + field.name + ");\n";
534 code += Indent + Indent + "$o = $builder->endObject();\n";
536 for (auto it = struct_def.fields.vec.begin();
537 it != struct_def.fields.vec.end(); ++it) {
539 if (!field.deprecated && field.required) {
540 code += Indent + Indent + "$builder->required($o, ";
541 code += NumToString(field.value.offset);
542 code += "); // " + field.name + "\n";
545 code += Indent + Indent + "return $o;\n";
546 code += Indent + "}\n\n";
549 // Set the value of a table's field.
550 static void BuildFieldOfTable(const FieldDef &field, const size_t offset,
551 std::string *code_ptr) {
552 std::string &code = *code_ptr;
554 code += Indent + "/**\n";
555 code += Indent + " * @param FlatBufferBuilder $builder\n";
556 code += Indent + " * @param " + GenTypeBasic(field.value.type) + "\n";
557 code += Indent + " * @return void\n";
558 code += Indent + " */\n";
559 code += Indent + "public static function ";
560 code += "add" + MakeCamel(field.name);
561 code += "(FlatBufferBuilder $builder, ";
562 code += "$" + MakeCamel(field.name, false);
564 code += Indent + "{\n";
565 code += Indent + Indent + "$builder->add";
566 code += GenMethod(field) + "X(";
567 code += NumToString(offset) + ", ";
569 code += "$" + MakeCamel(field.name, false);
572 if (field.value.type.base_type == BASE_TYPE_BOOL) {
575 code += field.value.constant;
578 code += Indent + "}\n\n";
581 // Set the value of one of the members of a table's vector.
582 static void BuildVectorOfTable(const FieldDef &field, std::string *code_ptr) {
583 std::string &code = *code_ptr;
585 auto vector_type = field.value.type.VectorType();
586 auto alignment = InlineAlignment(vector_type);
587 auto elem_size = InlineSize(vector_type);
588 code += Indent + "/**\n";
589 code += Indent + " * @param FlatBufferBuilder $builder\n";
590 code += Indent + " * @param array offset array\n";
591 code += Indent + " * @return int vector offset\n";
592 code += Indent + " */\n";
593 code += Indent + "public static function create";
594 code += MakeCamel(field.name);
595 code += "Vector(FlatBufferBuilder $builder, array $data)\n";
596 code += Indent + "{\n";
597 code += Indent + Indent + "$builder->startVector(";
598 code += NumToString(elem_size);
599 code += ", count($data), " + NumToString(alignment);
601 code += Indent + Indent;
602 code += "for ($i = count($data) - 1; $i >= 0; $i--) {\n";
603 if (IsScalar(field.value.type.VectorType().base_type)) {
604 code += Indent + Indent + Indent;
605 code += "$builder->put";
606 code += MakeCamel(GenTypeBasic(field.value.type.VectorType()));
607 code += "($data[$i]);\n";
609 code += Indent + Indent + Indent;
610 code += "$builder->putOffset($data[$i]);\n";
612 code += Indent + Indent + "}\n";
613 code += Indent + Indent + "return $builder->endVector();\n";
614 code += Indent + "}\n\n";
616 code += Indent + "/**\n";
617 code += Indent + " * @param FlatBufferBuilder $builder\n";
618 code += Indent + " * @param int $numElems\n";
619 code += Indent + " * @return void\n";
620 code += Indent + " */\n";
621 code += Indent + "public static function start";
622 code += MakeCamel(field.name);
623 code += "Vector(FlatBufferBuilder $builder, $numElems)\n";
624 code += Indent + "{\n";
625 code += Indent + Indent + "$builder->startVector(";
626 code += NumToString(elem_size);
627 code += ", $numElems, " + NumToString(alignment);
629 code += Indent + "}\n\n";
632 // Get the offset of the end of a table.
633 void GetEndOffsetOnTable(const StructDef &struct_def, std::string *code_ptr) {
634 std::string &code = *code_ptr;
636 code += Indent + "/**\n";
637 code += Indent + " * @param FlatBufferBuilder $builder\n";
638 code += Indent + " * @return int table offset\n";
639 code += Indent + " */\n";
640 code += Indent + "public static function end" + struct_def.name;
641 code += "(FlatBufferBuilder $builder)\n";
642 code += Indent + "{\n";
643 code += Indent + Indent + "$o = $builder->endObject();\n";
645 for (auto it = struct_def.fields.vec.begin();
646 it != struct_def.fields.vec.end(); ++it) {
648 if (!field.deprecated && field.required) {
649 code += Indent + Indent + "$builder->required($o, ";
650 code += NumToString(field.value.offset);
651 code += "); // " + field.name + "\n";
654 code += Indent + Indent + "return $o;\n";
655 code += Indent + "}\n";
657 if (parser_.root_struct_def_ == &struct_def) {
659 code += Indent + "public static function finish";
660 code += struct_def.name;
661 code += "Buffer(FlatBufferBuilder $builder, $offset)\n";
662 code += Indent + "{\n";
663 code += Indent + Indent + "$builder->finish($offset";
665 if (parser_.file_identifier_.length())
666 code += ", \"" + parser_.file_identifier_ + "\"";
668 code += Indent + "}\n";
672 // Generate a struct field, conditioned on its child type(s).
673 void GenStructAccessor(const StructDef &struct_def, const FieldDef &field,
674 std::string *code_ptr) {
675 GenComment(field.doc_comment, code_ptr, nullptr, Indent.c_str());
677 if (IsScalar(field.value.type.base_type)) {
678 if (struct_def.fixed) {
679 GetScalarFieldOfStruct(field, code_ptr);
681 GetScalarFieldOfTable(field, code_ptr);
684 switch (field.value.type.base_type) {
685 case BASE_TYPE_STRUCT:
686 if (struct_def.fixed) {
687 GetStructFieldOfStruct(field, code_ptr);
689 GetStructFieldOfTable(field, code_ptr);
692 case BASE_TYPE_STRING: GetStringField(field, code_ptr); break;
693 case BASE_TYPE_VECTOR: {
694 auto vectortype = field.value.type.VectorType();
695 if (vectortype.base_type == BASE_TYPE_UNION) {
696 GetMemberOfVectorOfUnion(field, code_ptr);
697 } else if (vectortype.base_type == BASE_TYPE_STRUCT) {
698 GetMemberOfVectorOfStruct(struct_def, field, code_ptr);
700 GetMemberOfVectorOfNonStruct(field, code_ptr);
704 case BASE_TYPE_UNION: GetUnionField(field, code_ptr); break;
705 default: FLATBUFFERS_ASSERT(0);
708 if (field.value.type.base_type == BASE_TYPE_VECTOR) {
709 GetVectorLen(field, code_ptr);
710 if (field.value.type.element == BASE_TYPE_UCHAR) {
711 GetUByte(field, code_ptr);
716 // Generate table constructors, conditioned on its members' types.
717 void GenTableBuilders(const StructDef &struct_def, std::string *code_ptr) {
718 GetStartOfTable(struct_def, code_ptr);
720 for (auto it = struct_def.fields.vec.begin();
721 it != struct_def.fields.vec.end(); ++it) {
723 if (field.deprecated) continue;
725 auto offset = it - struct_def.fields.vec.begin();
726 if (field.value.type.base_type == BASE_TYPE_UNION) {
727 std::string &code = *code_ptr;
728 code += Indent + "public static function add";
729 code += MakeCamel(field.name);
730 code += "(FlatBufferBuilder $builder, $offset)\n";
731 code += Indent + "{\n";
732 code += Indent + Indent + "$builder->addOffsetX(";
733 code += NumToString(offset) + ", $offset, 0);\n";
734 code += Indent + "}\n\n";
736 BuildFieldOfTable(field, offset, code_ptr);
738 if (field.value.type.base_type == BASE_TYPE_VECTOR) {
739 BuildVectorOfTable(field, code_ptr);
743 GetEndOffsetOnTable(struct_def, code_ptr);
746 // Generate struct or table methods.
747 void GenStruct(const StructDef &struct_def, std::string *code_ptr) {
748 if (struct_def.generated) return;
750 GenComment(struct_def.doc_comment, code_ptr, nullptr);
751 BeginClass(struct_def, code_ptr);
753 if (!struct_def.fixed) {
754 // Generate a special accessor for the table that has been declared as
756 NewRootTypeFromBuffer(struct_def, code_ptr);
759 std::string &code = *code_ptr;
760 if (!struct_def.fixed) {
761 if (parser_.file_identifier_.length()) {
762 // Return the identifier
763 code += Indent + "public static function " + struct_def.name;
764 code += "Identifier()\n";
765 code += Indent + "{\n";
766 code += Indent + Indent + "return \"";
767 code += parser_.file_identifier_ + "\";\n";
768 code += Indent + "}\n\n";
770 // Check if a buffer has the identifier.
771 code += Indent + "public static function " + struct_def.name;
772 code += "BufferHasIdentifier(ByteBuffer $buf)\n";
773 code += Indent + "{\n";
774 code += Indent + Indent + "return self::";
775 code += "__has_identifier($buf, self::";
776 code += struct_def.name + "Identifier());\n";
777 code += Indent + "}\n\n";
780 if (parser_.file_extension_.length()) {
781 // Return the extension
782 code += Indent + "public static function " + struct_def.name;
783 code += "Extension()\n";
784 code += Indent + "{\n";
785 code += Indent + Indent + "return \"" + parser_.file_extension_;
787 code += Indent + "}\n\n";
791 // Generate the Init method that sets the field in a pre-existing
792 // accessor object. This is to allow object reuse.
793 InitializeExisting(struct_def, code_ptr);
794 for (auto it = struct_def.fields.vec.begin();
795 it != struct_def.fields.vec.end(); ++it) {
797 if (field.deprecated) continue;
799 GenStructAccessor(struct_def, field, code_ptr);
802 if (struct_def.fixed) {
803 // create a struct constructor function
804 GenStructBuilder(struct_def, code_ptr);
806 // Create a set of functions that allow table construction.
807 GenTableBuilders(struct_def, code_ptr);
812 // Generate enum declarations.
813 static void GenEnum(const EnumDef &enum_def, std::string *code_ptr) {
814 if (enum_def.generated) return;
816 GenComment(enum_def.doc_comment, code_ptr, nullptr);
817 BeginEnum(enum_def.name, code_ptr);
818 for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
820 GenComment(ev.doc_comment, code_ptr, nullptr, Indent.c_str());
821 EnumMember(enum_def, ev, code_ptr);
824 std::string &code = *code_ptr;
826 code += Indent + "private static $names = array(\n";
827 for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
829 code += Indent + Indent + enum_def.name + "::" + ev.name + "=>" + "\"" +
833 code += Indent + ");\n\n";
834 code += Indent + "public static function Name($e)\n";
835 code += Indent + "{\n";
836 code += Indent + Indent + "if (!isset(self::$names[$e])) {\n";
837 code += Indent + Indent + Indent + "throw new \\Exception();\n";
838 code += Indent + Indent + "}\n";
839 code += Indent + Indent + "return self::$names[$e];\n";
840 code += Indent + "}\n";
844 // Returns the function name that is able to read a value of the given type.
845 static std::string GenGetter(const Type &type) {
846 switch (type.base_type) {
847 case BASE_TYPE_STRING: return "__string";
848 case BASE_TYPE_STRUCT: return "__struct";
849 case BASE_TYPE_UNION: return "__union";
850 case BASE_TYPE_VECTOR: return GenGetter(type.VectorType());
851 default: return "Get";
855 // Returns the method name for use with add/put calls.
856 static std::string GenMethod(const FieldDef &field) {
857 return IsScalar(field.value.type.base_type)
858 ? MakeCamel(GenTypeBasic(field.value.type))
859 : (IsStruct(field.value.type) ? "Struct" : "Offset");
862 static std::string GenTypeBasic(const Type &type) {
863 static const char *ctypename[] = {
865 #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
866 CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE, KTYPE) \
868 FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
869 #undef FLATBUFFERS_TD
872 return ctypename[type.base_type];
875 std::string GenDefaultValue(const Value &value) {
876 if (value.type.enum_def) {
877 if (auto val = value.type.enum_def->FindByValue(value.constant)) {
878 return WrapInNameSpace(*value.type.enum_def) + "::" + val->name;
882 switch (value.type.base_type) {
883 case BASE_TYPE_BOOL: return value.constant == "0" ? "false" : "true";
885 case BASE_TYPE_STRING: return "null";
888 case BASE_TYPE_ULONG:
889 if (value.constant != "0") {
890 int64_t constant = StringToInt(value.constant.c_str());
891 return NumToString(constant);
895 default: return value.constant;
899 static std::string GenTypePointer(const Type &type) {
900 switch (type.base_type) {
901 case BASE_TYPE_STRING: return "string";
902 case BASE_TYPE_VECTOR: return GenTypeGet(type.VectorType());
903 case BASE_TYPE_STRUCT: return type.struct_def->name;
904 case BASE_TYPE_UNION:
906 default: return "Table";
910 static std::string GenTypeGet(const Type &type) {
911 return IsScalar(type.base_type) ? GenTypeBasic(type) : GenTypePointer(type);
914 // Create a struct with a builder and the struct's arguments.
915 static void GenStructBuilder(const StructDef &struct_def,
916 std::string *code_ptr) {
917 std::string &code = *code_ptr;
919 code += Indent + "/**\n";
920 code += Indent + " * @return int offset\n";
921 code += Indent + " */\n";
922 code += Indent + "public static function create" + struct_def.name;
923 code += "(FlatBufferBuilder $builder";
924 StructBuilderArgs(struct_def, "", code_ptr);
926 code += Indent + "{\n";
928 StructBuilderBody(struct_def, "", code_ptr);
930 code += Indent + Indent + "return $builder->offset();\n";
931 code += Indent + "}\n";
936 bool GeneratePhp(const Parser &parser, const std::string &path,
937 const std::string &file_name) {
938 php::PhpGenerator generator(parser, path, file_name);
939 return generator.generate();
941 } // namespace flatbuffers