Merge commit '7c1ae250acb4120322b01a666993ed63c795ef21' into master

Update flatbuffers to latest master.

This adds an upstream flatbuffer_ts_library rule and changes the
typescript codegen, so ends up touching every typescript file we have
that uses flatbuffers. But it does clean up some of the hacks we had to
make to get that to work.

Had to hack the flatbuffer_ts_library a bit because I forgot to make
imports work nicely for cross-repo things.

And of course, the original motivation for this is that it gives us
proper handling of transitive dependencies for
flatbuffer_cc_library, if we start using the deps attribute!

Had to modify the codegen for the new declaration_file attribute in
flatbuffer schemas to make it just be the filename rather than including
the path. Modifying that to make it use a workspace-relative path was
looking obnoxious. It's definitely feasible, but since we don't actually
need that attribute, just make it be a filename for now.

Change-Id: I523a758cafa512fa2a686c9705d23337a26798ca
Signed-off-by: James Kuszmaul <james.kuszmaul@bluerivertech.com>
Signed-off-by: James Kuszmaul <jabukuszmaul+collab@gmail.com>
diff --git a/third_party/flatbuffers/reflection/BUILD.bazel b/third_party/flatbuffers/reflection/BUILD.bazel
new file mode 100644
index 0000000..7948e12
--- /dev/null
+++ b/third_party/flatbuffers/reflection/BUILD.bazel
@@ -0,0 +1,14 @@
+load("//:build_defs.bzl", "flatbuffer_ts_library")
+
+filegroup(
+    name = "reflection_fbs_schema",
+    srcs = ["reflection.fbs"],
+    visibility = ["//visibility:public"],
+)
+
+flatbuffer_ts_library(
+    name = "reflection_ts_fbs",
+    srcs = ["reflection.fbs"],
+    include_reflection = False,
+    visibility = ["//visibility:public"],
+)
diff --git a/third_party/flatbuffers/reflection/generate_code.bat b/third_party/flatbuffers/reflection/generate_code.bat
deleted file mode 100644
index 7b0f4d8..0000000
--- a/third_party/flatbuffers/reflection/generate_code.bat
+++ /dev/null
@@ -1,18 +0,0 @@
-:: Copyright 2015 Google Inc. All rights reserved.
-::
-:: Licensed under the Apache License, Version 2.0 (the "License");
-:: you may not use this file except in compliance with the License.
-:: You may obtain a copy of the License at
-::
-::     http://www.apache.org/licenses/LICENSE-2.0
-::
-:: Unless required by applicable law or agreed to in writing, software
-:: distributed under the License is distributed on an "AS IS" BASIS,
-:: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-:: See the License for the specific language governing permissions and
-:: limitations under the License.
-
-set buildtype=Release
-if "%1"=="-b" set buildtype=%2
-
-..\%buildtype%\flatc.exe --cpp --cpp-std c++0x --no-prefix -o ../include/flatbuffers reflection.fbs || exit /b 1
diff --git a/third_party/flatbuffers/reflection/generate_code.sh b/third_party/flatbuffers/reflection/generate_code.sh
deleted file mode 100755
index b4cde5d..0000000
--- a/third_party/flatbuffers/reflection/generate_code.sh
+++ /dev/null
@@ -1,32 +0,0 @@
-#!/bin/bash
-#
-# Copyright 2016 Google Inc. All rights reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#     http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-set -e
-
-tempDir="../include/flatbuffers/.tmp"
-originalFile="../include/flatbuffers/reflection_generated.h"
-newFile="$tempDir/reflection_generated.h"
-
-../../../bazel-bin/external/com_github_google_flatbuffers/flatc -c --gen-object-api --reflect-names  --cpp-std c++0x --no-prefix -o $tempDir reflection.fbs
-
-if [ -f "$newFile" ]; then
-  if ! cmp -s "$originalFile" "$newFile"; then
-    mv $newFile $originalFile
-  else
-    rm $newFile
-  fi
-  rmdir $tempDir
-fi
-
diff --git a/third_party/flatbuffers/reflection/reflection.fbs b/third_party/flatbuffers/reflection/reflection.fbs
index 61d2f0c..cc06959 100644
--- a/third_party/flatbuffers/reflection/reflection.fbs
+++ b/third_party/flatbuffers/reflection/reflection.fbs
@@ -38,6 +38,10 @@
                              // If base_type == Union, UnionType, or integral derived
                              // from an enum, index into "enums" below.
     fixed_length:uint16 = 0 (id: 3);  // Only if base_type == Array.
+    /// The size (octets) of the `base_type` field.
+    base_size:uint = 4 (id: 4); // 4 Is a common size due to offsets being that size.
+    /// The size (octets) of the `element` field, if present.
+    element_size:uint = 0 (id: 5);
 }
 
 table KeyValue {
@@ -46,25 +50,27 @@
 }
 
 table EnumVal {
-    name:string (required, id: 0);
-    value:long (key, id: 1);
-    object:Object (id: 2);  // Will be deprecated in favor of union_type in the future.
+    name:string (id: 0, required);
+    value:long (id: 1, key);
+    object:Object (id: 2, deprecated);
     union_type:Type (id: 3);
     documentation:[string] (id: 4);
 }
 
 table Enum {
-    name:string (required, key, id: 0);
-    values:[EnumVal] (required, id: 1);  // In order of their values.
+    name:string (id: 0, required, key);
+    values:[EnumVal] (id: 1, required);  // In order of their values.
     is_union:bool = false (id: 2);
-    underlying_type:Type (required, id: 3);
+    underlying_type:Type (id: 3, required);
     attributes:[KeyValue] (id: 4);
     documentation:[string] (id: 5);
+    /// File that this Enum is declared in.
+    declaration_file: string (id: 6);
 }
 
 table Field {
-    name:string (required, key, id: 0);
-    type:Type (required, id: 1);
+    name:string (id: 0, required, key);
+    type:Type (id: 1, required);
     id:ushort (id: 2);
     offset:ushort (id: 3);  // Offset into the vtable for tables, or into the struct.
     default_integer:long = 0 (id: 4);
@@ -75,16 +81,20 @@
     attributes:[KeyValue] (id: 9);
     documentation:[string] (id: 10);
     optional:bool = false (id: 11);
+    /// Number of padding octets to always add after this field. Structs only.
+    padding:uint16 = 0 (id: 12); 
 }
 
 table Object {  // Used for both tables and structs.
-    name:string (required, key, id: 0);
-    fields:[Field] (required, id: 1);  // Sorted.
+    name:string (id: 0, required, key);
+    fields:[Field] (id: 1, required);  // Sorted.
     is_struct:bool = false (id: 2);
     minalign:int (id: 3);
     bytesize:int (id: 4);  // For structs.
     attributes:[KeyValue] (id: 5);
     documentation:[string] (id: 6);
+    /// File that this Object is declared in.
+    declaration_file: string (id: 7);
 }
 
 table RPCCall {
@@ -96,19 +106,43 @@
 }
 
 table Service {
-    name:string (required, key, id: 0);
+    name:string (id: 0, required, key);
     calls:[RPCCall] (id: 1);
     attributes:[KeyValue] (id: 2);
     documentation:[string] (id: 3);
+    /// File that this Service is declared in.
+    declaration_file: string (id: 4);
+}
+
+/// New schema language features that are not supported by old code generators.
+enum AdvancedFeatures : ulong (bit_flags) {
+    AdvancedArrayFeatures,
+    AdvancedUnionFeatures,
+    OptionalScalars,
+    DefaultVectorsAndStrings,
+}
+
+/// File specific information.
+/// Symbols declared within a file may be recovered by iterating over all
+/// symbols and examining the `declaration_file` field.
+table SchemaFile {
+  /// Filename, relative to project root.
+  filename:string (id: 0, required, key);
+  /// Names of included files, relative to project root.
+  included_filenames:[string] (id: 1);
 }
 
 table Schema {
-    objects:[Object] (required, id: 0);    // Sorted.
-    enums:[Enum] (required, id: 1);        // Sorted.
+    objects:[Object] (id: 0, required);    // Sorted.
+    enums:[Enum] (id: 1, required);        // Sorted.
     file_ident:string (id: 2);
     file_ext:string (id: 3);
     root_table:Object (id: 4);
     services:[Service] (id: 5);             // Sorted.
+    advanced_features:AdvancedFeatures (id: 6);
+    /// All the files used in this compilation. Files are relative to where
+    /// flatc was invoked.
+    fbs_files:[SchemaFile] (id: 7);         // Sorted.
 }
 
 root_type Schema;