FileDescriptor.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // https://developers.google.com/protocol-buffers/
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. #endregion
  32. using Google.Protobuf.WellKnownTypes;
  33. using System;
  34. using System.Collections.Generic;
  35. using System.Collections.ObjectModel;
  36. namespace Google.Protobuf.Reflection
  37. {
  38. /// <summary>
  39. /// Describes a .proto file, including everything defined within.
  40. /// IDescriptor is implemented such that the File property returns this descriptor,
  41. /// and the FullName is the same as the Name.
  42. /// </summary>
  43. public sealed class FileDescriptor : IDescriptor
  44. {
  45. // Prevent linker failures when using IL2CPP with the well-known types.
  46. static FileDescriptor()
  47. {
  48. ForceReflectionInitialization<Syntax>();
  49. ForceReflectionInitialization<NullValue>();
  50. ForceReflectionInitialization<Field.Types.Cardinality>();
  51. ForceReflectionInitialization<Field.Types.Kind>();
  52. ForceReflectionInitialization<Value.KindOneofCase>();
  53. }
  54. private FileDescriptor(ByteString descriptorData, FileDescriptorProto proto, FileDescriptor[] dependencies, DescriptorPool pool, bool allowUnknownDependencies, GeneratedClrTypeInfo generatedCodeInfo)
  55. {
  56. SerializedData = descriptorData;
  57. DescriptorPool = pool;
  58. Proto = proto;
  59. Dependencies = new ReadOnlyCollection<FileDescriptor>((FileDescriptor[]) dependencies.Clone());
  60. PublicDependencies = DeterminePublicDependencies(this, proto, dependencies, allowUnknownDependencies);
  61. pool.AddPackage(Package, this);
  62. MessageTypes = DescriptorUtil.ConvertAndMakeReadOnly(proto.MessageType,
  63. (message, index) =>
  64. new MessageDescriptor(message, this, null, index, generatedCodeInfo.NestedTypes[index]));
  65. EnumTypes = DescriptorUtil.ConvertAndMakeReadOnly(proto.EnumType,
  66. (enumType, index) =>
  67. new EnumDescriptor(enumType, this, null, index, generatedCodeInfo.NestedEnums[index]));
  68. Services = DescriptorUtil.ConvertAndMakeReadOnly(proto.Service,
  69. (service, index) =>
  70. new ServiceDescriptor(service, this, index));
  71. }
  72. /// <summary>
  73. /// Computes the full name of a descriptor within this file, with an optional parent message.
  74. /// </summary>
  75. internal string ComputeFullName(MessageDescriptor parent, string name)
  76. {
  77. if (parent != null)
  78. {
  79. return parent.FullName + "." + name;
  80. }
  81. if (Package.Length > 0)
  82. {
  83. return Package + "." + name;
  84. }
  85. return name;
  86. }
  87. /// <summary>
  88. /// Extracts public dependencies from direct dependencies. This is a static method despite its
  89. /// first parameter, as the value we're in the middle of constructing is only used for exceptions.
  90. /// </summary>
  91. private static IList<FileDescriptor> DeterminePublicDependencies(FileDescriptor @this, FileDescriptorProto proto, FileDescriptor[] dependencies, bool allowUnknownDependencies)
  92. {
  93. var nameToFileMap = new Dictionary<string, FileDescriptor>();
  94. foreach (var file in dependencies)
  95. {
  96. nameToFileMap[file.Name] = file;
  97. }
  98. var publicDependencies = new List<FileDescriptor>();
  99. for (int i = 0; i < proto.PublicDependency.Count; i++)
  100. {
  101. int index = proto.PublicDependency[i];
  102. if (index < 0 || index >= proto.Dependency.Count)
  103. {
  104. throw new DescriptorValidationException(@this, "Invalid public dependency index.");
  105. }
  106. string name = proto.Dependency[index];
  107. FileDescriptor file = nameToFileMap[name];
  108. if (file == null)
  109. {
  110. if (!allowUnknownDependencies)
  111. {
  112. throw new DescriptorValidationException(@this, "Invalid public dependency: " + name);
  113. }
  114. // Ignore unknown dependencies.
  115. }
  116. else
  117. {
  118. publicDependencies.Add(file);
  119. }
  120. }
  121. return new ReadOnlyCollection<FileDescriptor>(publicDependencies);
  122. }
  123. /// <value>
  124. /// The descriptor in its protocol message representation.
  125. /// </value>
  126. internal FileDescriptorProto Proto { get; }
  127. /// <value>
  128. /// The file name.
  129. /// </value>
  130. public string Name => Proto.Name;
  131. /// <summary>
  132. /// The package as declared in the .proto file. This may or may not
  133. /// be equivalent to the .NET namespace of the generated classes.
  134. /// </summary>
  135. public string Package => Proto.Package;
  136. /// <value>
  137. /// Unmodifiable list of top-level message types declared in this file.
  138. /// </value>
  139. public IList<MessageDescriptor> MessageTypes { get; }
  140. /// <value>
  141. /// Unmodifiable list of top-level enum types declared in this file.
  142. /// </value>
  143. public IList<EnumDescriptor> EnumTypes { get; }
  144. /// <value>
  145. /// Unmodifiable list of top-level services declared in this file.
  146. /// </value>
  147. public IList<ServiceDescriptor> Services { get; }
  148. /// <value>
  149. /// Unmodifiable list of this file's dependencies (imports).
  150. /// </value>
  151. public IList<FileDescriptor> Dependencies { get; }
  152. /// <value>
  153. /// Unmodifiable list of this file's public dependencies (public imports).
  154. /// </value>
  155. public IList<FileDescriptor> PublicDependencies { get; }
  156. /// <value>
  157. /// The original serialized binary form of this descriptor.
  158. /// </value>
  159. public ByteString SerializedData { get; }
  160. /// <value>
  161. /// Implementation of IDescriptor.FullName - just returns the same as Name.
  162. /// </value>
  163. string IDescriptor.FullName => Name;
  164. /// <value>
  165. /// Implementation of IDescriptor.File - just returns this descriptor.
  166. /// </value>
  167. FileDescriptor IDescriptor.File => this;
  168. /// <value>
  169. /// Pool containing symbol descriptors.
  170. /// </value>
  171. internal DescriptorPool DescriptorPool { get; }
  172. /// <summary>
  173. /// Finds a type (message, enum, service or extension) in the file by name. Does not find nested types.
  174. /// </summary>
  175. /// <param name="name">The unqualified type name to look for.</param>
  176. /// <typeparam name="T">The type of descriptor to look for</typeparam>
  177. /// <returns>The type's descriptor, or null if not found.</returns>
  178. public T FindTypeByName<T>(String name)
  179. where T : class, IDescriptor
  180. {
  181. // Don't allow looking up nested types. This will make optimization
  182. // easier later.
  183. if (name.IndexOf('.') != -1)
  184. {
  185. return null;
  186. }
  187. if (Package.Length > 0)
  188. {
  189. name = Package + "." + name;
  190. }
  191. T result = DescriptorPool.FindSymbol<T>(name);
  192. if (result != null && result.File == this)
  193. {
  194. return result;
  195. }
  196. return null;
  197. }
  198. /// <summary>
  199. /// Builds a FileDescriptor from its protocol buffer representation.
  200. /// </summary>
  201. /// <param name="descriptorData">The original serialized descriptor data.
  202. /// We have only limited proto2 support, so serializing FileDescriptorProto
  203. /// would not necessarily give us this.</param>
  204. /// <param name="proto">The protocol message form of the FileDescriptor.</param>
  205. /// <param name="dependencies">FileDescriptors corresponding to all of the
  206. /// file's dependencies, in the exact order listed in the .proto file. May be null,
  207. /// in which case it is treated as an empty array.</param>
  208. /// <param name="allowUnknownDependencies">Whether unknown dependencies are ignored (true) or cause an exception to be thrown (false).</param>
  209. /// <param name="generatedCodeInfo">Details about generated code, for the purposes of reflection.</param>
  210. /// <exception cref="DescriptorValidationException">If <paramref name="proto"/> is not
  211. /// a valid descriptor. This can occur for a number of reasons, such as a field
  212. /// having an undefined type or because two messages were defined with the same name.</exception>
  213. private static FileDescriptor BuildFrom(ByteString descriptorData, FileDescriptorProto proto, FileDescriptor[] dependencies, bool allowUnknownDependencies, GeneratedClrTypeInfo generatedCodeInfo)
  214. {
  215. // Building descriptors involves two steps: translating and linking.
  216. // In the translation step (implemented by FileDescriptor's
  217. // constructor), we build an object tree mirroring the
  218. // FileDescriptorProto's tree and put all of the descriptors into the
  219. // DescriptorPool's lookup tables. In the linking step, we look up all
  220. // type references in the DescriptorPool, so that, for example, a
  221. // FieldDescriptor for an embedded message contains a pointer directly
  222. // to the Descriptor for that message's type. We also detect undefined
  223. // types in the linking step.
  224. if (dependencies == null)
  225. {
  226. dependencies = new FileDescriptor[0];
  227. }
  228. DescriptorPool pool = new DescriptorPool(dependencies);
  229. FileDescriptor result = new FileDescriptor(descriptorData, proto, dependencies, pool, allowUnknownDependencies, generatedCodeInfo);
  230. // Validate that the dependencies we've been passed (as FileDescriptors) are actually the ones we
  231. // need.
  232. if (dependencies.Length != proto.Dependency.Count)
  233. {
  234. throw new DescriptorValidationException(
  235. result,
  236. "Dependencies passed to FileDescriptor.BuildFrom() don't match " +
  237. "those listed in the FileDescriptorProto.");
  238. }
  239. result.CrossLink();
  240. return result;
  241. }
  242. private void CrossLink()
  243. {
  244. foreach (MessageDescriptor message in MessageTypes)
  245. {
  246. message.CrossLink();
  247. }
  248. foreach (ServiceDescriptor service in Services)
  249. {
  250. service.CrossLink();
  251. }
  252. }
  253. /// <summary>
  254. /// Creates a descriptor for generated code.
  255. /// </summary>
  256. /// <remarks>
  257. /// This method is only designed to be used by the results of generating code with protoc,
  258. /// which creates the appropriate dependencies etc. It has to be public because the generated
  259. /// code is "external", but should not be called directly by end users.
  260. /// </remarks>
  261. public static FileDescriptor FromGeneratedCode(
  262. byte[] descriptorData,
  263. FileDescriptor[] dependencies,
  264. GeneratedClrTypeInfo generatedCodeInfo)
  265. {
  266. FileDescriptorProto proto;
  267. try
  268. {
  269. proto = FileDescriptorProto.Parser.ParseFrom(descriptorData);
  270. }
  271. catch (InvalidProtocolBufferException e)
  272. {
  273. throw new ArgumentException("Failed to parse protocol buffer descriptor for generated code.", e);
  274. }
  275. try
  276. {
  277. // When building descriptors for generated code, we allow unknown
  278. // dependencies by default.
  279. return BuildFrom(ByteString.CopyFrom(descriptorData), proto, dependencies, true, generatedCodeInfo);
  280. }
  281. catch (DescriptorValidationException e)
  282. {
  283. throw new ArgumentException($"Invalid embedded descriptor for \"{proto.Name}\".", e);
  284. }
  285. }
  286. /// <summary>
  287. /// Returns a <see cref="System.String" /> that represents this instance.
  288. /// </summary>
  289. /// <returns>
  290. /// A <see cref="System.String" /> that represents this instance.
  291. /// </returns>
  292. public override string ToString()
  293. {
  294. return $"FileDescriptor for {Name}";
  295. }
  296. /// <summary>
  297. /// Returns the file descriptor for descriptor.proto.
  298. /// </summary>
  299. /// <remarks>
  300. /// This is used for protos which take a direct dependency on <c>descriptor.proto</c>, typically for
  301. /// annotations. While <c>descriptor.proto</c> is a proto2 file, it is built into the Google.Protobuf
  302. /// runtime for reflection purposes. The messages are internal to the runtime as they would require
  303. /// proto2 semantics for full support, but the file descriptor is available via this property. The
  304. /// C# codegen in protoc automatically uses this property when it detects a dependency on <c>descriptor.proto</c>.
  305. /// </remarks>
  306. /// <value>
  307. /// The file descriptor for <c>descriptor.proto</c>.
  308. /// </value>
  309. public static FileDescriptor DescriptorProtoFileDescriptor { get { return DescriptorReflection.Descriptor; } }
  310. /// <summary>
  311. /// The (possibly empty) set of custom options for this file.
  312. /// </summary>
  313. public CustomOptions CustomOptions => Proto.Options?.CustomOptions ?? CustomOptions.Empty;
  314. /// <summary>
  315. /// Performs initialization for the given generic type argument.
  316. /// </summary>
  317. /// <remarks>
  318. /// This method is present for the sake of AOT compilers. It allows code (whether handwritten or generated)
  319. /// to make calls into the reflection machinery of this library to express an intention to use that type
  320. /// reflectively (e.g. for JSON parsing and formatting). The call itself does almost nothing, but AOT compilers
  321. /// attempting to determine which generic type arguments need to be handled will spot the code path and act
  322. /// accordingly.
  323. /// </remarks>
  324. /// <typeparam name="T">The type to force initialization for.</typeparam>
  325. public static void ForceReflectionInitialization<T>() => ReflectionUtil.ForceInitialize<T>();
  326. }
  327. }