desc_init.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. // Copyright 2019 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package filedesc
  5. import (
  6. "fmt"
  7. "sync"
  8. "google.golang.org/protobuf/encoding/protowire"
  9. "google.golang.org/protobuf/internal/genid"
  10. "google.golang.org/protobuf/internal/strs"
  11. "google.golang.org/protobuf/reflect/protoreflect"
  12. )
  13. // fileRaw is a data struct used when initializing a file descriptor from
  14. // a raw FileDescriptorProto.
  15. type fileRaw struct {
  16. builder Builder
  17. allEnums []Enum
  18. allMessages []Message
  19. allExtensions []Extension
  20. allServices []Service
  21. }
  22. func newRawFile(db Builder) *File {
  23. fd := &File{fileRaw: fileRaw{builder: db}}
  24. fd.initDecls(db.NumEnums, db.NumMessages, db.NumExtensions, db.NumServices)
  25. fd.unmarshalSeed(db.RawDescriptor)
  26. // Extended message targets are eagerly resolved since registration
  27. // needs this information at program init time.
  28. for i := range fd.allExtensions {
  29. xd := &fd.allExtensions[i]
  30. xd.L1.Extendee = fd.resolveMessageDependency(xd.L1.Extendee, listExtTargets, int32(i))
  31. }
  32. fd.checkDecls()
  33. return fd
  34. }
  35. // initDecls pre-allocates slices for the exact number of enums, messages
  36. // (including map entries), extensions, and services declared in the proto file.
  37. // This is done to avoid regrowing the slice, which would change the address
  38. // for any previously seen declaration.
  39. //
  40. // The alloc methods "allocates" slices by pulling from the capacity.
  41. func (fd *File) initDecls(numEnums, numMessages, numExtensions, numServices int32) {
  42. fd.allEnums = make([]Enum, 0, numEnums)
  43. fd.allMessages = make([]Message, 0, numMessages)
  44. fd.allExtensions = make([]Extension, 0, numExtensions)
  45. fd.allServices = make([]Service, 0, numServices)
  46. }
  47. func (fd *File) allocEnums(n int) []Enum {
  48. total := len(fd.allEnums)
  49. es := fd.allEnums[total : total+n]
  50. fd.allEnums = fd.allEnums[:total+n]
  51. return es
  52. }
  53. func (fd *File) allocMessages(n int) []Message {
  54. total := len(fd.allMessages)
  55. ms := fd.allMessages[total : total+n]
  56. fd.allMessages = fd.allMessages[:total+n]
  57. return ms
  58. }
  59. func (fd *File) allocExtensions(n int) []Extension {
  60. total := len(fd.allExtensions)
  61. xs := fd.allExtensions[total : total+n]
  62. fd.allExtensions = fd.allExtensions[:total+n]
  63. return xs
  64. }
  65. func (fd *File) allocServices(n int) []Service {
  66. total := len(fd.allServices)
  67. xs := fd.allServices[total : total+n]
  68. fd.allServices = fd.allServices[:total+n]
  69. return xs
  70. }
  71. // checkDecls performs a sanity check that the expected number of expected
  72. // declarations matches the number that were found in the descriptor proto.
  73. func (fd *File) checkDecls() {
  74. switch {
  75. case len(fd.allEnums) != cap(fd.allEnums):
  76. case len(fd.allMessages) != cap(fd.allMessages):
  77. case len(fd.allExtensions) != cap(fd.allExtensions):
  78. case len(fd.allServices) != cap(fd.allServices):
  79. default:
  80. return
  81. }
  82. panic("mismatching cardinality")
  83. }
  84. func (fd *File) unmarshalSeed(b []byte) {
  85. sb := getBuilder()
  86. defer putBuilder(sb)
  87. var prevField protoreflect.FieldNumber
  88. var numEnums, numMessages, numExtensions, numServices int
  89. var posEnums, posMessages, posExtensions, posServices int
  90. var options []byte
  91. b0 := b
  92. for len(b) > 0 {
  93. num, typ, n := protowire.ConsumeTag(b)
  94. b = b[n:]
  95. switch typ {
  96. case protowire.BytesType:
  97. v, m := protowire.ConsumeBytes(b)
  98. b = b[m:]
  99. switch num {
  100. case genid.FileDescriptorProto_Syntax_field_number:
  101. switch string(v) {
  102. case "proto2":
  103. fd.L1.Syntax = protoreflect.Proto2
  104. case "proto3":
  105. fd.L1.Syntax = protoreflect.Proto3
  106. case "editions":
  107. fd.L1.Syntax = protoreflect.Editions
  108. default:
  109. panic("invalid syntax")
  110. }
  111. case genid.FileDescriptorProto_Name_field_number:
  112. fd.L1.Path = sb.MakeString(v)
  113. case genid.FileDescriptorProto_Package_field_number:
  114. fd.L1.Package = protoreflect.FullName(sb.MakeString(v))
  115. case genid.FileDescriptorProto_Options_field_number:
  116. options = v
  117. case genid.FileDescriptorProto_EnumType_field_number:
  118. if prevField != genid.FileDescriptorProto_EnumType_field_number {
  119. if numEnums > 0 {
  120. panic("non-contiguous repeated field")
  121. }
  122. posEnums = len(b0) - len(b) - n - m
  123. }
  124. numEnums++
  125. case genid.FileDescriptorProto_MessageType_field_number:
  126. if prevField != genid.FileDescriptorProto_MessageType_field_number {
  127. if numMessages > 0 {
  128. panic("non-contiguous repeated field")
  129. }
  130. posMessages = len(b0) - len(b) - n - m
  131. }
  132. numMessages++
  133. case genid.FileDescriptorProto_Extension_field_number:
  134. if prevField != genid.FileDescriptorProto_Extension_field_number {
  135. if numExtensions > 0 {
  136. panic("non-contiguous repeated field")
  137. }
  138. posExtensions = len(b0) - len(b) - n - m
  139. }
  140. numExtensions++
  141. case genid.FileDescriptorProto_Service_field_number:
  142. if prevField != genid.FileDescriptorProto_Service_field_number {
  143. if numServices > 0 {
  144. panic("non-contiguous repeated field")
  145. }
  146. posServices = len(b0) - len(b) - n - m
  147. }
  148. numServices++
  149. }
  150. prevField = num
  151. case protowire.VarintType:
  152. v, m := protowire.ConsumeVarint(b)
  153. b = b[m:]
  154. switch num {
  155. case genid.FileDescriptorProto_Edition_field_number:
  156. fd.L1.Edition = Edition(v)
  157. }
  158. default:
  159. m := protowire.ConsumeFieldValue(num, typ, b)
  160. b = b[m:]
  161. prevField = -1 // ignore known field numbers of unknown wire type
  162. }
  163. }
  164. // If syntax is missing, it is assumed to be proto2.
  165. if fd.L1.Syntax == 0 {
  166. fd.L1.Syntax = protoreflect.Proto2
  167. }
  168. if fd.L1.Syntax == protoreflect.Editions {
  169. fd.L1.EditionFeatures = getFeaturesFor(fd.L1.Edition)
  170. }
  171. // Parse editions features from options if any
  172. if options != nil {
  173. fd.unmarshalSeedOptions(options)
  174. }
  175. // Must allocate all declarations before parsing each descriptor type
  176. // to ensure we handled all descriptors in "flattened ordering".
  177. if numEnums > 0 {
  178. fd.L1.Enums.List = fd.allocEnums(numEnums)
  179. }
  180. if numMessages > 0 {
  181. fd.L1.Messages.List = fd.allocMessages(numMessages)
  182. }
  183. if numExtensions > 0 {
  184. fd.L1.Extensions.List = fd.allocExtensions(numExtensions)
  185. }
  186. if numServices > 0 {
  187. fd.L1.Services.List = fd.allocServices(numServices)
  188. }
  189. if numEnums > 0 {
  190. b := b0[posEnums:]
  191. for i := range fd.L1.Enums.List {
  192. _, n := protowire.ConsumeVarint(b)
  193. v, m := protowire.ConsumeBytes(b[n:])
  194. fd.L1.Enums.List[i].unmarshalSeed(v, sb, fd, fd, i)
  195. b = b[n+m:]
  196. }
  197. }
  198. if numMessages > 0 {
  199. b := b0[posMessages:]
  200. for i := range fd.L1.Messages.List {
  201. _, n := protowire.ConsumeVarint(b)
  202. v, m := protowire.ConsumeBytes(b[n:])
  203. fd.L1.Messages.List[i].unmarshalSeed(v, sb, fd, fd, i)
  204. b = b[n+m:]
  205. }
  206. }
  207. if numExtensions > 0 {
  208. b := b0[posExtensions:]
  209. for i := range fd.L1.Extensions.List {
  210. _, n := protowire.ConsumeVarint(b)
  211. v, m := protowire.ConsumeBytes(b[n:])
  212. fd.L1.Extensions.List[i].unmarshalSeed(v, sb, fd, fd, i)
  213. b = b[n+m:]
  214. }
  215. }
  216. if numServices > 0 {
  217. b := b0[posServices:]
  218. for i := range fd.L1.Services.List {
  219. _, n := protowire.ConsumeVarint(b)
  220. v, m := protowire.ConsumeBytes(b[n:])
  221. fd.L1.Services.List[i].unmarshalSeed(v, sb, fd, fd, i)
  222. b = b[n+m:]
  223. }
  224. }
  225. }
  226. func (fd *File) unmarshalSeedOptions(b []byte) {
  227. for b := b; len(b) > 0; {
  228. num, typ, n := protowire.ConsumeTag(b)
  229. b = b[n:]
  230. switch typ {
  231. case protowire.BytesType:
  232. v, m := protowire.ConsumeBytes(b)
  233. b = b[m:]
  234. switch num {
  235. case genid.FileOptions_Features_field_number:
  236. if fd.Syntax() != protoreflect.Editions {
  237. panic(fmt.Sprintf("invalid descriptor: using edition features in a proto with syntax %s", fd.Syntax()))
  238. }
  239. fd.L1.EditionFeatures = unmarshalFeatureSet(v, fd.L1.EditionFeatures)
  240. }
  241. default:
  242. m := protowire.ConsumeFieldValue(num, typ, b)
  243. b = b[m:]
  244. }
  245. }
  246. }
  247. func (ed *Enum) unmarshalSeed(b []byte, sb *strs.Builder, pf *File, pd protoreflect.Descriptor, i int) {
  248. ed.L0.ParentFile = pf
  249. ed.L0.Parent = pd
  250. ed.L0.Index = i
  251. var numValues int
  252. for b := b; len(b) > 0; {
  253. num, typ, n := protowire.ConsumeTag(b)
  254. b = b[n:]
  255. switch typ {
  256. case protowire.BytesType:
  257. v, m := protowire.ConsumeBytes(b)
  258. b = b[m:]
  259. switch num {
  260. case genid.EnumDescriptorProto_Name_field_number:
  261. ed.L0.FullName = appendFullName(sb, pd.FullName(), v)
  262. case genid.EnumDescriptorProto_Value_field_number:
  263. numValues++
  264. }
  265. default:
  266. m := protowire.ConsumeFieldValue(num, typ, b)
  267. b = b[m:]
  268. }
  269. }
  270. // Only construct enum value descriptors for top-level enums since
  271. // they are needed for registration.
  272. if pd != pf {
  273. return
  274. }
  275. ed.L1.eagerValues = true
  276. ed.L2 = new(EnumL2)
  277. ed.L2.Values.List = make([]EnumValue, numValues)
  278. for i := 0; len(b) > 0; {
  279. num, typ, n := protowire.ConsumeTag(b)
  280. b = b[n:]
  281. switch typ {
  282. case protowire.BytesType:
  283. v, m := protowire.ConsumeBytes(b)
  284. b = b[m:]
  285. switch num {
  286. case genid.EnumDescriptorProto_Value_field_number:
  287. ed.L2.Values.List[i].unmarshalFull(v, sb, pf, ed, i)
  288. i++
  289. }
  290. default:
  291. m := protowire.ConsumeFieldValue(num, typ, b)
  292. b = b[m:]
  293. }
  294. }
  295. }
  296. func (md *Message) unmarshalSeed(b []byte, sb *strs.Builder, pf *File, pd protoreflect.Descriptor, i int) {
  297. md.L0.ParentFile = pf
  298. md.L0.Parent = pd
  299. md.L0.Index = i
  300. md.L1.EditionFeatures = featuresFromParentDesc(md.Parent())
  301. var prevField protoreflect.FieldNumber
  302. var numEnums, numMessages, numExtensions int
  303. var posEnums, posMessages, posExtensions int
  304. b0 := b
  305. for len(b) > 0 {
  306. num, typ, n := protowire.ConsumeTag(b)
  307. b = b[n:]
  308. switch typ {
  309. case protowire.BytesType:
  310. v, m := protowire.ConsumeBytes(b)
  311. b = b[m:]
  312. switch num {
  313. case genid.DescriptorProto_Name_field_number:
  314. md.L0.FullName = appendFullName(sb, pd.FullName(), v)
  315. case genid.DescriptorProto_EnumType_field_number:
  316. if prevField != genid.DescriptorProto_EnumType_field_number {
  317. if numEnums > 0 {
  318. panic("non-contiguous repeated field")
  319. }
  320. posEnums = len(b0) - len(b) - n - m
  321. }
  322. numEnums++
  323. case genid.DescriptorProto_NestedType_field_number:
  324. if prevField != genid.DescriptorProto_NestedType_field_number {
  325. if numMessages > 0 {
  326. panic("non-contiguous repeated field")
  327. }
  328. posMessages = len(b0) - len(b) - n - m
  329. }
  330. numMessages++
  331. case genid.DescriptorProto_Extension_field_number:
  332. if prevField != genid.DescriptorProto_Extension_field_number {
  333. if numExtensions > 0 {
  334. panic("non-contiguous repeated field")
  335. }
  336. posExtensions = len(b0) - len(b) - n - m
  337. }
  338. numExtensions++
  339. case genid.DescriptorProto_Options_field_number:
  340. md.unmarshalSeedOptions(v)
  341. }
  342. prevField = num
  343. default:
  344. m := protowire.ConsumeFieldValue(num, typ, b)
  345. b = b[m:]
  346. prevField = -1 // ignore known field numbers of unknown wire type
  347. }
  348. }
  349. // Must allocate all declarations before parsing each descriptor type
  350. // to ensure we handled all descriptors in "flattened ordering".
  351. if numEnums > 0 {
  352. md.L1.Enums.List = pf.allocEnums(numEnums)
  353. }
  354. if numMessages > 0 {
  355. md.L1.Messages.List = pf.allocMessages(numMessages)
  356. }
  357. if numExtensions > 0 {
  358. md.L1.Extensions.List = pf.allocExtensions(numExtensions)
  359. }
  360. if numEnums > 0 {
  361. b := b0[posEnums:]
  362. for i := range md.L1.Enums.List {
  363. _, n := protowire.ConsumeVarint(b)
  364. v, m := protowire.ConsumeBytes(b[n:])
  365. md.L1.Enums.List[i].unmarshalSeed(v, sb, pf, md, i)
  366. b = b[n+m:]
  367. }
  368. }
  369. if numMessages > 0 {
  370. b := b0[posMessages:]
  371. for i := range md.L1.Messages.List {
  372. _, n := protowire.ConsumeVarint(b)
  373. v, m := protowire.ConsumeBytes(b[n:])
  374. md.L1.Messages.List[i].unmarshalSeed(v, sb, pf, md, i)
  375. b = b[n+m:]
  376. }
  377. }
  378. if numExtensions > 0 {
  379. b := b0[posExtensions:]
  380. for i := range md.L1.Extensions.List {
  381. _, n := protowire.ConsumeVarint(b)
  382. v, m := protowire.ConsumeBytes(b[n:])
  383. md.L1.Extensions.List[i].unmarshalSeed(v, sb, pf, md, i)
  384. b = b[n+m:]
  385. }
  386. }
  387. }
  388. func (md *Message) unmarshalSeedOptions(b []byte) {
  389. for len(b) > 0 {
  390. num, typ, n := protowire.ConsumeTag(b)
  391. b = b[n:]
  392. switch typ {
  393. case protowire.VarintType:
  394. v, m := protowire.ConsumeVarint(b)
  395. b = b[m:]
  396. switch num {
  397. case genid.MessageOptions_MapEntry_field_number:
  398. md.L1.IsMapEntry = protowire.DecodeBool(v)
  399. case genid.MessageOptions_MessageSetWireFormat_field_number:
  400. md.L1.IsMessageSet = protowire.DecodeBool(v)
  401. }
  402. case protowire.BytesType:
  403. v, m := protowire.ConsumeBytes(b)
  404. b = b[m:]
  405. switch num {
  406. case genid.MessageOptions_Features_field_number:
  407. md.L1.EditionFeatures = unmarshalFeatureSet(v, md.L1.EditionFeatures)
  408. }
  409. default:
  410. m := protowire.ConsumeFieldValue(num, typ, b)
  411. b = b[m:]
  412. }
  413. }
  414. }
  415. func (xd *Extension) unmarshalSeed(b []byte, sb *strs.Builder, pf *File, pd protoreflect.Descriptor, i int) {
  416. xd.L0.ParentFile = pf
  417. xd.L0.Parent = pd
  418. xd.L0.Index = i
  419. for len(b) > 0 {
  420. num, typ, n := protowire.ConsumeTag(b)
  421. b = b[n:]
  422. switch typ {
  423. case protowire.VarintType:
  424. v, m := protowire.ConsumeVarint(b)
  425. b = b[m:]
  426. switch num {
  427. case genid.FieldDescriptorProto_Number_field_number:
  428. xd.L1.Number = protoreflect.FieldNumber(v)
  429. case genid.FieldDescriptorProto_Label_field_number:
  430. xd.L1.Cardinality = protoreflect.Cardinality(v)
  431. case genid.FieldDescriptorProto_Type_field_number:
  432. xd.L1.Kind = protoreflect.Kind(v)
  433. }
  434. case protowire.BytesType:
  435. v, m := protowire.ConsumeBytes(b)
  436. b = b[m:]
  437. switch num {
  438. case genid.FieldDescriptorProto_Name_field_number:
  439. xd.L0.FullName = appendFullName(sb, pd.FullName(), v)
  440. case genid.FieldDescriptorProto_Extendee_field_number:
  441. xd.L1.Extendee = PlaceholderMessage(makeFullName(sb, v))
  442. }
  443. default:
  444. m := protowire.ConsumeFieldValue(num, typ, b)
  445. b = b[m:]
  446. }
  447. }
  448. }
  449. func (sd *Service) unmarshalSeed(b []byte, sb *strs.Builder, pf *File, pd protoreflect.Descriptor, i int) {
  450. sd.L0.ParentFile = pf
  451. sd.L0.Parent = pd
  452. sd.L0.Index = i
  453. for len(b) > 0 {
  454. num, typ, n := protowire.ConsumeTag(b)
  455. b = b[n:]
  456. switch typ {
  457. case protowire.BytesType:
  458. v, m := protowire.ConsumeBytes(b)
  459. b = b[m:]
  460. switch num {
  461. case genid.ServiceDescriptorProto_Name_field_number:
  462. sd.L0.FullName = appendFullName(sb, pd.FullName(), v)
  463. }
  464. default:
  465. m := protowire.ConsumeFieldValue(num, typ, b)
  466. b = b[m:]
  467. }
  468. }
  469. }
  470. var nameBuilderPool = sync.Pool{
  471. New: func() interface{} { return new(strs.Builder) },
  472. }
  473. func getBuilder() *strs.Builder {
  474. return nameBuilderPool.Get().(*strs.Builder)
  475. }
  476. func putBuilder(b *strs.Builder) {
  477. nameBuilderPool.Put(b)
  478. }
  479. // makeFullName converts b to a protoreflect.FullName,
  480. // where b must start with a leading dot.
  481. func makeFullName(sb *strs.Builder, b []byte) protoreflect.FullName {
  482. if len(b) == 0 || b[0] != '.' {
  483. panic("name reference must be fully qualified")
  484. }
  485. return protoreflect.FullName(sb.MakeString(b[1:]))
  486. }
  487. func appendFullName(sb *strs.Builder, prefix protoreflect.FullName, suffix []byte) protoreflect.FullName {
  488. return sb.AppendFullName(prefix, protoreflect.Name(strs.UnsafeString(suffix)))
  489. }