gen.go 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967
  1. // Copyright (c) 2012-2015 Ugorji Nwoke. All rights reserved.
  2. // Use of this source code is governed by a MIT license found in the LICENSE file.
  3. package codec
  4. import (
  5. "bytes"
  6. "encoding/base64"
  7. "errors"
  8. "fmt"
  9. "go/format"
  10. "io"
  11. "io/ioutil"
  12. "math/rand"
  13. "os"
  14. "reflect"
  15. "regexp"
  16. "sort"
  17. "strconv"
  18. "strings"
  19. "sync"
  20. "text/template"
  21. "time"
  22. )
  23. // ---------------------------------------------------
  24. // codecgen supports the full cycle of reflection-based codec:
  25. // - RawExt
  26. // - Builtins
  27. // - Extensions
  28. // - (Binary|Text|JSON)(Unm|M)arshal
  29. // - generic by-kind
  30. //
  31. // This means that, for dynamic things, we MUST use reflection to at least get the reflect.Type.
  32. // In those areas, we try to only do reflection or interface-conversion when NECESSARY:
  33. // - Extensions, only if Extensions are configured.
  34. //
  35. // However, codecgen doesn't support the following:
  36. // - Canonical option. (codecgen IGNORES it currently)
  37. // This is just because it has not been implemented.
  38. //
  39. // During encode/decode, Selfer takes precedence.
  40. // A type implementing Selfer will know how to encode/decode itself statically.
  41. //
  42. // The following field types are supported:
  43. // array: [n]T
  44. // slice: []T
  45. // map: map[K]V
  46. // primitive: [u]int[n], float(32|64), bool, string
  47. // struct
  48. //
  49. // ---------------------------------------------------
  50. // Note that a Selfer cannot call (e|d).(En|De)code on itself,
  51. // as this will cause a circular reference, as (En|De)code will call Selfer methods.
  52. // Any type that implements Selfer must implement completely and not fallback to (En|De)code.
  53. //
  54. // In addition, code in this file manages the generation of fast-path implementations of
  55. // encode/decode of slices/maps of primitive keys/values.
  56. //
  57. // Users MUST re-generate their implementations whenever the code shape changes.
  58. // The generated code will panic if it was generated with a version older than the supporting library.
  59. // ---------------------------------------------------
  60. //
  61. // codec framework is very feature rich.
  62. // When encoding or decoding into an interface, it depends on the runtime type of the interface.
  63. // The type of the interface may be a named type, an extension, etc.
  64. // Consequently, we fallback to runtime codec for encoding/decoding interfaces.
  65. // In addition, we fallback for any value which cannot be guaranteed at runtime.
  66. // This allows us support ANY value, including any named types, specifically those which
  67. // do not implement our interfaces (e.g. Selfer).
  68. //
  69. // This explains some slowness compared to other code generation codecs (e.g. msgp).
  70. // This reduction in speed is only seen when your refers to interfaces,
  71. // e.g. type T struct { A interface{}; B []interface{}; C map[string]interface{} }
  72. //
  73. // codecgen will panic if the file was generated with an old version of the library in use.
  74. //
  75. // Note:
  76. // It was a concious decision to have gen.go always explicitly call EncodeNil or TryDecodeAsNil.
  77. // This way, there isn't a function call overhead just to see that we should not enter a block of code.
  78. // GenVersion is the current version of codecgen.
  79. //
  80. // NOTE: Increment this value each time codecgen changes fundamentally.
  81. // Fundamental changes are:
  82. // - helper methods change (signature change, new ones added, some removed, etc)
  83. // - codecgen command line changes
  84. //
  85. // v1: Initial Version
  86. // v2:
  87. // v3: Changes for Kubernetes:
  88. // changes in signature of some unpublished helper methods and codecgen cmdline arguments.
  89. // v4: Removed separator support from (en|de)cDriver, and refactored codec(gen)
  90. // v5: changes to support faster json decoding. Let encoder/decoder maintain state of collections.
  91. const GenVersion = 5
  92. const (
  93. genCodecPkg = "codec1978"
  94. genTempVarPfx = "yy"
  95. genTopLevelVarName = "x"
  96. // ignore canBeNil parameter, and always set to true.
  97. // This is because nil can appear anywhere, so we should always check.
  98. genAnythingCanBeNil = true
  99. // if genUseOneFunctionForDecStructMap, make a single codecDecodeSelferFromMap function;
  100. // else make codecDecodeSelferFromMap{LenPrefix,CheckBreak} so that conditionals
  101. // are not executed a lot.
  102. //
  103. // From testing, it didn't make much difference in runtime, so keep as true (one function only)
  104. genUseOneFunctionForDecStructMap = true
  105. )
  106. type genStructMapStyle uint8
  107. const (
  108. genStructMapStyleConsolidated genStructMapStyle = iota
  109. genStructMapStyleLenPrefix
  110. genStructMapStyleCheckBreak
  111. )
  112. var (
  113. genAllTypesSamePkgErr = errors.New("All types must be in the same package")
  114. genExpectArrayOrMapErr = errors.New("unexpected type. Expecting array/map/slice")
  115. genBase64enc = base64.NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789__")
  116. genQNameRegex = regexp.MustCompile(`[A-Za-z_.]+`)
  117. )
  118. // genRunner holds some state used during a Gen run.
  119. type genRunner struct {
  120. w io.Writer // output
  121. c uint64 // counter used for generating varsfx
  122. t []reflect.Type // list of types to run selfer on
  123. tc reflect.Type // currently running selfer on this type
  124. te map[uintptr]bool // types for which the encoder has been created
  125. td map[uintptr]bool // types for which the decoder has been created
  126. cp string // codec import path
  127. im map[string]reflect.Type // imports to add
  128. imn map[string]string // package names of imports to add
  129. imc uint64 // counter for import numbers
  130. is map[reflect.Type]struct{} // types seen during import search
  131. bp string // base PkgPath, for which we are generating for
  132. cpfx string // codec package prefix
  133. unsafe bool // is unsafe to be used in generated code?
  134. tm map[reflect.Type]struct{} // types for which enc/dec must be generated
  135. ts []reflect.Type // types for which enc/dec must be generated
  136. xs string // top level variable/constant suffix
  137. hn string // fn helper type name
  138. ti *TypeInfos
  139. // rr *rand.Rand // random generator for file-specific types
  140. }
  141. // Gen will write a complete go file containing Selfer implementations for each
  142. // type passed. All the types must be in the same package.
  143. //
  144. // Library users: *DO NOT USE IT DIRECTLY. IT WILL CHANGE CONTINOUSLY WITHOUT NOTICE.*
  145. func Gen(w io.Writer, buildTags, pkgName, uid string, useUnsafe bool, ti *TypeInfos, typ ...reflect.Type) {
  146. if len(typ) == 0 {
  147. return
  148. }
  149. x := genRunner{
  150. unsafe: useUnsafe,
  151. w: w,
  152. t: typ,
  153. te: make(map[uintptr]bool),
  154. td: make(map[uintptr]bool),
  155. im: make(map[string]reflect.Type),
  156. imn: make(map[string]string),
  157. is: make(map[reflect.Type]struct{}),
  158. tm: make(map[reflect.Type]struct{}),
  159. ts: []reflect.Type{},
  160. bp: genImportPath(typ[0]),
  161. xs: uid,
  162. ti: ti,
  163. }
  164. if x.ti == nil {
  165. x.ti = defTypeInfos
  166. }
  167. if x.xs == "" {
  168. rr := rand.New(rand.NewSource(time.Now().UnixNano()))
  169. x.xs = strconv.FormatInt(rr.Int63n(9999), 10)
  170. }
  171. // gather imports first:
  172. x.cp = genImportPath(reflect.TypeOf(x))
  173. x.imn[x.cp] = genCodecPkg
  174. for _, t := range typ {
  175. // fmt.Printf("###########: PkgPath: '%v', Name: '%s'\n", genImportPath(t), t.Name())
  176. if genImportPath(t) != x.bp {
  177. panic(genAllTypesSamePkgErr)
  178. }
  179. x.genRefPkgs(t)
  180. }
  181. if buildTags != "" {
  182. x.line("//+build " + buildTags)
  183. x.line("")
  184. }
  185. x.line(`
  186. // ************************************************************
  187. // DO NOT EDIT.
  188. // THIS FILE IS AUTO-GENERATED BY codecgen.
  189. // ************************************************************
  190. `)
  191. x.line("package " + pkgName)
  192. x.line("")
  193. x.line("import (")
  194. if x.cp != x.bp {
  195. x.cpfx = genCodecPkg + "."
  196. x.linef("%s \"%s\"", genCodecPkg, x.cp)
  197. }
  198. // use a sorted set of im keys, so that we can get consistent output
  199. imKeys := make([]string, 0, len(x.im))
  200. for k, _ := range x.im {
  201. imKeys = append(imKeys, k)
  202. }
  203. sort.Strings(imKeys)
  204. for _, k := range imKeys { // for k, _ := range x.im {
  205. x.linef("%s \"%s\"", x.imn[k], k)
  206. }
  207. // add required packages
  208. for _, k := range [...]string{"reflect", "unsafe", "runtime", "fmt", "errors"} {
  209. if _, ok := x.im[k]; !ok {
  210. if k == "unsafe" && !x.unsafe {
  211. continue
  212. }
  213. x.line("\"" + k + "\"")
  214. }
  215. }
  216. x.line(")")
  217. x.line("")
  218. x.line("const (")
  219. x.linef("// ----- content types ----")
  220. x.linef("codecSelferC_UTF8%s = %v", x.xs, int64(c_UTF8))
  221. x.linef("codecSelferC_RAW%s = %v", x.xs, int64(c_RAW))
  222. x.linef("// ----- value types used ----")
  223. x.linef("codecSelferValueTypeArray%s = %v", x.xs, int64(valueTypeArray))
  224. x.linef("codecSelferValueTypeMap%s = %v", x.xs, int64(valueTypeMap))
  225. x.linef("// ----- containerStateValues ----")
  226. x.linef("codecSelfer_containerMapKey%s = %v", x.xs, int64(containerMapKey))
  227. x.linef("codecSelfer_containerMapValue%s = %v", x.xs, int64(containerMapValue))
  228. x.linef("codecSelfer_containerMapEnd%s = %v", x.xs, int64(containerMapEnd))
  229. x.linef("codecSelfer_containerArrayElem%s = %v", x.xs, int64(containerArrayElem))
  230. x.linef("codecSelfer_containerArrayEnd%s = %v", x.xs, int64(containerArrayEnd))
  231. x.line(")")
  232. x.line("var (")
  233. x.line("codecSelferBitsize" + x.xs + " = uint8(reflect.TypeOf(uint(0)).Bits())")
  234. x.line("codecSelferOnlyMapOrArrayEncodeToStructErr" + x.xs + " = errors.New(`only encoded map or array can be decoded into a struct`)")
  235. x.line(")")
  236. x.line("")
  237. if x.unsafe {
  238. x.line("type codecSelferUnsafeString" + x.xs + " struct { Data uintptr; Len int}")
  239. x.line("")
  240. }
  241. x.hn = "codecSelfer" + x.xs
  242. x.line("type " + x.hn + " struct{}")
  243. x.line("")
  244. x.line("func init() {")
  245. x.linef("if %sGenVersion != %v {", x.cpfx, GenVersion)
  246. x.line("_, file, _, _ := runtime.Caller(0)")
  247. x.line(`err := fmt.Errorf("codecgen version mismatch: current: %v, need %v. Re-generate file: %v", `)
  248. x.linef(`%v, %sGenVersion, file)`, GenVersion, x.cpfx)
  249. x.line("panic(err)")
  250. x.linef("}")
  251. x.line("if false { // reference the types, but skip this branch at build/run time")
  252. var n int
  253. // for k, t := range x.im {
  254. for _, k := range imKeys {
  255. t := x.im[k]
  256. x.linef("var v%v %s.%s", n, x.imn[k], t.Name())
  257. n++
  258. }
  259. if x.unsafe {
  260. x.linef("var v%v unsafe.Pointer", n)
  261. n++
  262. }
  263. if n > 0 {
  264. x.out("_")
  265. for i := 1; i < n; i++ {
  266. x.out(", _")
  267. }
  268. x.out(" = v0")
  269. for i := 1; i < n; i++ {
  270. x.outf(", v%v", i)
  271. }
  272. }
  273. x.line("} ") // close if false
  274. x.line("}") // close init
  275. x.line("")
  276. // generate rest of type info
  277. for _, t := range typ {
  278. x.tc = t
  279. x.selfer(true)
  280. x.selfer(false)
  281. }
  282. for _, t := range x.ts {
  283. rtid := reflect.ValueOf(t).Pointer()
  284. // generate enc functions for all these slice/map types.
  285. x.linef("func (x %s) enc%s(v %s%s, e *%sEncoder) {", x.hn, x.genMethodNameT(t), x.arr2str(t, "*"), x.genTypeName(t), x.cpfx)
  286. x.genRequiredMethodVars(true)
  287. switch t.Kind() {
  288. case reflect.Array, reflect.Slice, reflect.Chan:
  289. x.encListFallback("v", t)
  290. case reflect.Map:
  291. x.encMapFallback("v", t)
  292. default:
  293. panic(genExpectArrayOrMapErr)
  294. }
  295. x.line("}")
  296. x.line("")
  297. // generate dec functions for all these slice/map types.
  298. x.linef("func (x %s) dec%s(v *%s, d *%sDecoder) {", x.hn, x.genMethodNameT(t), x.genTypeName(t), x.cpfx)
  299. x.genRequiredMethodVars(false)
  300. switch t.Kind() {
  301. case reflect.Array, reflect.Slice, reflect.Chan:
  302. x.decListFallback("v", rtid, t)
  303. case reflect.Map:
  304. x.decMapFallback("v", rtid, t)
  305. default:
  306. panic(genExpectArrayOrMapErr)
  307. }
  308. x.line("}")
  309. x.line("")
  310. }
  311. x.line("")
  312. }
  313. func (x *genRunner) checkForSelfer(t reflect.Type, varname string) bool {
  314. // return varname != genTopLevelVarName && t != x.tc
  315. // the only time we checkForSelfer is if we are not at the TOP of the generated code.
  316. return varname != genTopLevelVarName
  317. }
  318. func (x *genRunner) arr2str(t reflect.Type, s string) string {
  319. if t.Kind() == reflect.Array {
  320. return s
  321. }
  322. return ""
  323. }
  324. func (x *genRunner) genRequiredMethodVars(encode bool) {
  325. x.line("var h " + x.hn)
  326. if encode {
  327. x.line("z, r := " + x.cpfx + "GenHelperEncoder(e)")
  328. } else {
  329. x.line("z, r := " + x.cpfx + "GenHelperDecoder(d)")
  330. }
  331. x.line("_, _, _ = h, z, r")
  332. }
  333. func (x *genRunner) genRefPkgs(t reflect.Type) {
  334. if _, ok := x.is[t]; ok {
  335. return
  336. }
  337. // fmt.Printf(">>>>>>: PkgPath: '%v', Name: '%s'\n", genImportPath(t), t.Name())
  338. x.is[t] = struct{}{}
  339. tpkg, tname := genImportPath(t), t.Name()
  340. if tpkg != "" && tpkg != x.bp && tpkg != x.cp && tname != "" && tname[0] >= 'A' && tname[0] <= 'Z' {
  341. if _, ok := x.im[tpkg]; !ok {
  342. x.im[tpkg] = t
  343. if idx := strings.LastIndex(tpkg, "/"); idx < 0 {
  344. x.imn[tpkg] = tpkg
  345. } else {
  346. x.imc++
  347. x.imn[tpkg] = "pkg" + strconv.FormatUint(x.imc, 10) + "_" + tpkg[idx+1:]
  348. }
  349. }
  350. }
  351. switch t.Kind() {
  352. case reflect.Array, reflect.Slice, reflect.Ptr, reflect.Chan:
  353. x.genRefPkgs(t.Elem())
  354. case reflect.Map:
  355. x.genRefPkgs(t.Elem())
  356. x.genRefPkgs(t.Key())
  357. case reflect.Struct:
  358. for i := 0; i < t.NumField(); i++ {
  359. if fname := t.Field(i).Name; fname != "" && fname[0] >= 'A' && fname[0] <= 'Z' {
  360. x.genRefPkgs(t.Field(i).Type)
  361. }
  362. }
  363. }
  364. }
  365. func (x *genRunner) line(s string) {
  366. x.out(s)
  367. if len(s) == 0 || s[len(s)-1] != '\n' {
  368. x.out("\n")
  369. }
  370. }
  371. func (x *genRunner) varsfx() string {
  372. x.c++
  373. return strconv.FormatUint(x.c, 10)
  374. }
  375. func (x *genRunner) out(s string) {
  376. if _, err := io.WriteString(x.w, s); err != nil {
  377. panic(err)
  378. }
  379. }
  380. func (x *genRunner) linef(s string, params ...interface{}) {
  381. x.line(fmt.Sprintf(s, params...))
  382. }
  383. func (x *genRunner) outf(s string, params ...interface{}) {
  384. x.out(fmt.Sprintf(s, params...))
  385. }
  386. func (x *genRunner) genTypeName(t reflect.Type) (n string) {
  387. // defer func() { fmt.Printf(">>>> ####: genTypeName: t: %v, name: '%s'\n", t, n) }()
  388. // if the type has a PkgPath, which doesn't match the current package,
  389. // then include it.
  390. // We cannot depend on t.String() because it includes current package,
  391. // or t.PkgPath because it includes full import path,
  392. //
  393. var ptrPfx string
  394. for t.Kind() == reflect.Ptr {
  395. ptrPfx += "*"
  396. t = t.Elem()
  397. }
  398. if tn := t.Name(); tn != "" {
  399. return ptrPfx + x.genTypeNamePrim(t)
  400. }
  401. switch t.Kind() {
  402. case reflect.Map:
  403. return ptrPfx + "map[" + x.genTypeName(t.Key()) + "]" + x.genTypeName(t.Elem())
  404. case reflect.Slice:
  405. return ptrPfx + "[]" + x.genTypeName(t.Elem())
  406. case reflect.Array:
  407. return ptrPfx + "[" + strconv.FormatInt(int64(t.Len()), 10) + "]" + x.genTypeName(t.Elem())
  408. case reflect.Chan:
  409. return ptrPfx + t.ChanDir().String() + " " + x.genTypeName(t.Elem())
  410. default:
  411. if t == intfTyp {
  412. return ptrPfx + "interface{}"
  413. } else {
  414. return ptrPfx + x.genTypeNamePrim(t)
  415. }
  416. }
  417. }
  418. func (x *genRunner) genTypeNamePrim(t reflect.Type) (n string) {
  419. if t.Name() == "" {
  420. return t.String()
  421. } else if genImportPath(t) == "" || genImportPath(t) == genImportPath(x.tc) {
  422. return t.Name()
  423. } else {
  424. return x.imn[genImportPath(t)] + "." + t.Name()
  425. // return t.String() // best way to get the package name inclusive
  426. }
  427. }
  428. func (x *genRunner) genZeroValueR(t reflect.Type) string {
  429. // if t is a named type, w
  430. switch t.Kind() {
  431. case reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Func,
  432. reflect.Slice, reflect.Map, reflect.Invalid:
  433. return "nil"
  434. case reflect.Bool:
  435. return "false"
  436. case reflect.String:
  437. return `""`
  438. case reflect.Struct, reflect.Array:
  439. return x.genTypeName(t) + "{}"
  440. default: // all numbers
  441. return "0"
  442. }
  443. }
  444. func (x *genRunner) genMethodNameT(t reflect.Type) (s string) {
  445. return genMethodNameT(t, x.tc)
  446. }
  447. func (x *genRunner) selfer(encode bool) {
  448. t := x.tc
  449. t0 := t
  450. // always make decode use a pointer receiver,
  451. // and structs always use a ptr receiver (encode|decode)
  452. isptr := !encode || t.Kind() == reflect.Struct
  453. fnSigPfx := "func (x "
  454. if isptr {
  455. fnSigPfx += "*"
  456. }
  457. fnSigPfx += x.genTypeName(t)
  458. x.out(fnSigPfx)
  459. if isptr {
  460. t = reflect.PtrTo(t)
  461. }
  462. if encode {
  463. x.line(") CodecEncodeSelf(e *" + x.cpfx + "Encoder) {")
  464. x.genRequiredMethodVars(true)
  465. // x.enc(genTopLevelVarName, t)
  466. x.encVar(genTopLevelVarName, t)
  467. } else {
  468. x.line(") CodecDecodeSelf(d *" + x.cpfx + "Decoder) {")
  469. x.genRequiredMethodVars(false)
  470. // do not use decVar, as there is no need to check TryDecodeAsNil
  471. // or way to elegantly handle that, and also setting it to a
  472. // non-nil value doesn't affect the pointer passed.
  473. // x.decVar(genTopLevelVarName, t, false)
  474. x.dec(genTopLevelVarName, t0)
  475. }
  476. x.line("}")
  477. x.line("")
  478. if encode || t0.Kind() != reflect.Struct {
  479. return
  480. }
  481. // write is containerMap
  482. if genUseOneFunctionForDecStructMap {
  483. x.out(fnSigPfx)
  484. x.line(") codecDecodeSelfFromMap(l int, d *" + x.cpfx + "Decoder) {")
  485. x.genRequiredMethodVars(false)
  486. x.decStructMap(genTopLevelVarName, "l", reflect.ValueOf(t0).Pointer(), t0, genStructMapStyleConsolidated)
  487. x.line("}")
  488. x.line("")
  489. } else {
  490. x.out(fnSigPfx)
  491. x.line(") codecDecodeSelfFromMapLenPrefix(l int, d *" + x.cpfx + "Decoder) {")
  492. x.genRequiredMethodVars(false)
  493. x.decStructMap(genTopLevelVarName, "l", reflect.ValueOf(t0).Pointer(), t0, genStructMapStyleLenPrefix)
  494. x.line("}")
  495. x.line("")
  496. x.out(fnSigPfx)
  497. x.line(") codecDecodeSelfFromMapCheckBreak(l int, d *" + x.cpfx + "Decoder) {")
  498. x.genRequiredMethodVars(false)
  499. x.decStructMap(genTopLevelVarName, "l", reflect.ValueOf(t0).Pointer(), t0, genStructMapStyleCheckBreak)
  500. x.line("}")
  501. x.line("")
  502. }
  503. // write containerArray
  504. x.out(fnSigPfx)
  505. x.line(") codecDecodeSelfFromArray(l int, d *" + x.cpfx + "Decoder) {")
  506. x.genRequiredMethodVars(false)
  507. x.decStructArray(genTopLevelVarName, "l", "return", reflect.ValueOf(t0).Pointer(), t0)
  508. x.line("}")
  509. x.line("")
  510. }
  511. // used for chan, array, slice, map
  512. func (x *genRunner) xtraSM(varname string, encode bool, t reflect.Type) {
  513. if encode {
  514. x.linef("h.enc%s((%s%s)(%s), e)", x.genMethodNameT(t), x.arr2str(t, "*"), x.genTypeName(t), varname)
  515. } else {
  516. x.linef("h.dec%s((*%s)(%s), d)", x.genMethodNameT(t), x.genTypeName(t), varname)
  517. }
  518. x.registerXtraT(t)
  519. }
  520. func (x *genRunner) registerXtraT(t reflect.Type) {
  521. // recursively register the types
  522. if _, ok := x.tm[t]; ok {
  523. return
  524. }
  525. var tkey reflect.Type
  526. switch t.Kind() {
  527. case reflect.Chan, reflect.Slice, reflect.Array:
  528. case reflect.Map:
  529. tkey = t.Key()
  530. default:
  531. return
  532. }
  533. x.tm[t] = struct{}{}
  534. x.ts = append(x.ts, t)
  535. // check if this refers to any xtra types eg. a slice of array: add the array
  536. x.registerXtraT(t.Elem())
  537. if tkey != nil {
  538. x.registerXtraT(tkey)
  539. }
  540. }
  541. // encVar will encode a variable.
  542. // The parameter, t, is the reflect.Type of the variable itself
  543. func (x *genRunner) encVar(varname string, t reflect.Type) {
  544. // fmt.Printf(">>>>>> varname: %s, t: %v\n", varname, t)
  545. var checkNil bool
  546. switch t.Kind() {
  547. case reflect.Ptr, reflect.Interface, reflect.Slice, reflect.Map, reflect.Chan:
  548. checkNil = true
  549. }
  550. if checkNil {
  551. x.linef("if %s == nil { r.EncodeNil() } else { ", varname)
  552. }
  553. switch t.Kind() {
  554. case reflect.Ptr:
  555. switch t.Elem().Kind() {
  556. case reflect.Struct, reflect.Array:
  557. x.enc(varname, genNonPtr(t))
  558. default:
  559. i := x.varsfx()
  560. x.line(genTempVarPfx + i + " := *" + varname)
  561. x.enc(genTempVarPfx+i, genNonPtr(t))
  562. }
  563. case reflect.Struct, reflect.Array:
  564. i := x.varsfx()
  565. x.line(genTempVarPfx + i + " := &" + varname)
  566. x.enc(genTempVarPfx+i, t)
  567. default:
  568. x.enc(varname, t)
  569. }
  570. if checkNil {
  571. x.line("}")
  572. }
  573. }
  574. // enc will encode a variable (varname) of type t,
  575. // except t is of kind reflect.Struct or reflect.Array, wherein varname is of type ptrTo(T) (to prevent copying)
  576. func (x *genRunner) enc(varname string, t reflect.Type) {
  577. rtid := reflect.ValueOf(t).Pointer()
  578. // We call CodecEncodeSelf if one of the following are honored:
  579. // - the type already implements Selfer, call that
  580. // - the type has a Selfer implementation just created, use that
  581. // - the type is in the list of the ones we will generate for, but it is not currently being generated
  582. mi := x.varsfx()
  583. tptr := reflect.PtrTo(t)
  584. tk := t.Kind()
  585. if x.checkForSelfer(t, varname) {
  586. if tk == reflect.Array || tk == reflect.Struct { // varname is of type *T
  587. if tptr.Implements(selferTyp) || t.Implements(selferTyp) {
  588. x.line(varname + ".CodecEncodeSelf(e)")
  589. return
  590. }
  591. } else { // varname is of type T
  592. if t.Implements(selferTyp) {
  593. x.line(varname + ".CodecEncodeSelf(e)")
  594. return
  595. } else if tptr.Implements(selferTyp) {
  596. x.linef("%ssf%s := &%s", genTempVarPfx, mi, varname)
  597. x.linef("%ssf%s.CodecEncodeSelf(e)", genTempVarPfx, mi)
  598. return
  599. }
  600. }
  601. if _, ok := x.te[rtid]; ok {
  602. x.line(varname + ".CodecEncodeSelf(e)")
  603. return
  604. }
  605. }
  606. inlist := false
  607. for _, t0 := range x.t {
  608. if t == t0 {
  609. inlist = true
  610. if x.checkForSelfer(t, varname) {
  611. x.line(varname + ".CodecEncodeSelf(e)")
  612. return
  613. }
  614. break
  615. }
  616. }
  617. var rtidAdded bool
  618. if t == x.tc {
  619. x.te[rtid] = true
  620. rtidAdded = true
  621. }
  622. // check if
  623. // - type is RawExt
  624. // - the type implements (Text|JSON|Binary)(Unm|M)arshal
  625. x.linef("%sm%s := z.EncBinary()", genTempVarPfx, mi)
  626. x.linef("_ = %sm%s", genTempVarPfx, mi)
  627. x.line("if false {") //start if block
  628. defer func() { x.line("}") }() //end if block
  629. if t == rawExtTyp {
  630. x.linef("} else { r.EncodeRawExt(%v, e)", varname)
  631. return
  632. }
  633. // HACK: Support for Builtins.
  634. // Currently, only Binc supports builtins, and the only builtin type is time.Time.
  635. // Have a method that returns the rtid for time.Time if Handle is Binc.
  636. if t == timeTyp {
  637. vrtid := genTempVarPfx + "m" + x.varsfx()
  638. x.linef("} else if %s := z.TimeRtidIfBinc(); %s != 0 { ", vrtid, vrtid)
  639. x.linef("r.EncodeBuiltin(%s, %s)", vrtid, varname)
  640. }
  641. // only check for extensions if the type is named, and has a packagePath.
  642. if genImportPath(t) != "" && t.Name() != "" {
  643. // first check if extensions are configued, before doing the interface conversion
  644. x.linef("} else if z.HasExtensions() && z.EncExt(%s) {", varname)
  645. }
  646. if tk == reflect.Array || tk == reflect.Struct { // varname is of type *T
  647. if t.Implements(binaryMarshalerTyp) || tptr.Implements(binaryMarshalerTyp) {
  648. x.linef("} else if %sm%s { z.EncBinaryMarshal(%v) ", genTempVarPfx, mi, varname)
  649. }
  650. if t.Implements(jsonMarshalerTyp) || tptr.Implements(jsonMarshalerTyp) {
  651. x.linef("} else if !%sm%s && z.IsJSONHandle() { z.EncJSONMarshal(%v) ", genTempVarPfx, mi, varname)
  652. } else if t.Implements(textMarshalerTyp) || tptr.Implements(textMarshalerTyp) {
  653. x.linef("} else if !%sm%s { z.EncTextMarshal(%v) ", genTempVarPfx, mi, varname)
  654. }
  655. } else { // varname is of type T
  656. if t.Implements(binaryMarshalerTyp) {
  657. x.linef("} else if %sm%s { z.EncBinaryMarshal(%v) ", genTempVarPfx, mi, varname)
  658. } else if tptr.Implements(binaryMarshalerTyp) {
  659. x.linef("} else if %sm%s { z.EncBinaryMarshal(&%v) ", genTempVarPfx, mi, varname)
  660. }
  661. if t.Implements(jsonMarshalerTyp) {
  662. x.linef("} else if !%sm%s && z.IsJSONHandle() { z.EncJSONMarshal(%v) ", genTempVarPfx, mi, varname)
  663. } else if tptr.Implements(jsonMarshalerTyp) {
  664. x.linef("} else if !%sm%s && z.IsJSONHandle() { z.EncJSONMarshal(&%v) ", genTempVarPfx, mi, varname)
  665. } else if t.Implements(textMarshalerTyp) {
  666. x.linef("} else if !%sm%s { z.EncTextMarshal(%v) ", genTempVarPfx, mi, varname)
  667. } else if tptr.Implements(textMarshalerTyp) {
  668. x.linef("} else if !%sm%s { z.EncTextMarshal(&%v) ", genTempVarPfx, mi, varname)
  669. }
  670. }
  671. x.line("} else {")
  672. switch t.Kind() {
  673. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  674. x.line("r.EncodeInt(int64(" + varname + "))")
  675. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  676. x.line("r.EncodeUint(uint64(" + varname + "))")
  677. case reflect.Float32:
  678. x.line("r.EncodeFloat32(float32(" + varname + "))")
  679. case reflect.Float64:
  680. x.line("r.EncodeFloat64(float64(" + varname + "))")
  681. case reflect.Bool:
  682. x.line("r.EncodeBool(bool(" + varname + "))")
  683. case reflect.String:
  684. x.line("r.EncodeString(codecSelferC_UTF8" + x.xs + ", string(" + varname + "))")
  685. case reflect.Chan:
  686. x.xtraSM(varname, true, t)
  687. // x.encListFallback(varname, rtid, t)
  688. case reflect.Array:
  689. x.xtraSM(varname, true, t)
  690. case reflect.Slice:
  691. // if nil, call dedicated function
  692. // if a []uint8, call dedicated function
  693. // if a known fastpath slice, call dedicated function
  694. // else write encode function in-line.
  695. // - if elements are primitives or Selfers, call dedicated function on each member.
  696. // - else call Encoder.encode(XXX) on it.
  697. if rtid == uint8SliceTypId {
  698. x.line("r.EncodeStringBytes(codecSelferC_RAW" + x.xs + ", []byte(" + varname + "))")
  699. } else if fastpathAV.index(rtid) != -1 {
  700. g := x.newGenV(t)
  701. x.line("z.F." + g.MethodNamePfx("Enc", false) + "V(" + varname + ", false, e)")
  702. } else {
  703. x.xtraSM(varname, true, t)
  704. // x.encListFallback(varname, rtid, t)
  705. }
  706. case reflect.Map:
  707. // if nil, call dedicated function
  708. // if a known fastpath map, call dedicated function
  709. // else write encode function in-line.
  710. // - if elements are primitives or Selfers, call dedicated function on each member.
  711. // - else call Encoder.encode(XXX) on it.
  712. // x.line("if " + varname + " == nil { \nr.EncodeNil()\n } else { ")
  713. if fastpathAV.index(rtid) != -1 {
  714. g := x.newGenV(t)
  715. x.line("z.F." + g.MethodNamePfx("Enc", false) + "V(" + varname + ", false, e)")
  716. } else {
  717. x.xtraSM(varname, true, t)
  718. // x.encMapFallback(varname, rtid, t)
  719. }
  720. case reflect.Struct:
  721. if !inlist {
  722. delete(x.te, rtid)
  723. x.line("z.EncFallback(" + varname + ")")
  724. break
  725. }
  726. x.encStruct(varname, rtid, t)
  727. default:
  728. if rtidAdded {
  729. delete(x.te, rtid)
  730. }
  731. x.line("z.EncFallback(" + varname + ")")
  732. }
  733. }
  734. func (x *genRunner) encZero(t reflect.Type) {
  735. switch t.Kind() {
  736. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  737. x.line("r.EncodeInt(0)")
  738. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  739. x.line("r.EncodeUint(0)")
  740. case reflect.Float32:
  741. x.line("r.EncodeFloat32(0)")
  742. case reflect.Float64:
  743. x.line("r.EncodeFloat64(0)")
  744. case reflect.Bool:
  745. x.line("r.EncodeBool(false)")
  746. case reflect.String:
  747. x.line("r.EncodeString(codecSelferC_UTF8" + x.xs + `, "")`)
  748. default:
  749. x.line("r.EncodeNil()")
  750. }
  751. }
  752. func (x *genRunner) encStruct(varname string, rtid uintptr, t reflect.Type) {
  753. // Use knowledge from structfieldinfo (mbs, encodable fields. Ignore omitempty. )
  754. // replicate code in kStruct i.e. for each field, deref type to non-pointer, and call x.enc on it
  755. // if t === type currently running selfer on, do for all
  756. ti := x.ti.get(rtid, t)
  757. i := x.varsfx()
  758. sepVarname := genTempVarPfx + "sep" + i
  759. numfieldsvar := genTempVarPfx + "q" + i
  760. ti2arrayvar := genTempVarPfx + "r" + i
  761. struct2arrvar := genTempVarPfx + "2arr" + i
  762. x.line(sepVarname + " := !z.EncBinary()")
  763. x.linef("%s := z.EncBasicHandle().StructToArray", struct2arrvar)
  764. tisfi := ti.sfip // always use sequence from file. decStruct expects same thing.
  765. // due to omitEmpty, we need to calculate the
  766. // number of non-empty things we write out first.
  767. // This is required as we need to pre-determine the size of the container,
  768. // to support length-prefixing.
  769. x.linef("var %s [%v]bool", numfieldsvar, len(tisfi))
  770. x.linef("_, _, _ = %s, %s, %s", sepVarname, numfieldsvar, struct2arrvar)
  771. x.linef("const %s bool = %v", ti2arrayvar, ti.toArray)
  772. nn := 0
  773. for j, si := range tisfi {
  774. if !si.omitEmpty {
  775. nn++
  776. continue
  777. }
  778. var t2 reflect.StructField
  779. var omitline string
  780. if si.i != -1 {
  781. t2 = t.Field(int(si.i))
  782. } else {
  783. t2typ := t
  784. varname3 := varname
  785. for _, ix := range si.is {
  786. for t2typ.Kind() == reflect.Ptr {
  787. t2typ = t2typ.Elem()
  788. }
  789. t2 = t2typ.Field(ix)
  790. t2typ = t2.Type
  791. varname3 = varname3 + "." + t2.Name
  792. if t2typ.Kind() == reflect.Ptr {
  793. omitline += varname3 + " != nil && "
  794. }
  795. }
  796. }
  797. // never check omitEmpty on a struct type, as it may contain uncomparable map/slice/etc.
  798. // also, for maps/slices/arrays, check if len ! 0 (not if == zero value)
  799. switch t2.Type.Kind() {
  800. case reflect.Struct:
  801. omitline += " true"
  802. case reflect.Map, reflect.Slice, reflect.Array, reflect.Chan:
  803. omitline += "len(" + varname + "." + t2.Name + ") != 0"
  804. default:
  805. omitline += varname + "." + t2.Name + " != " + x.genZeroValueR(t2.Type)
  806. }
  807. x.linef("%s[%v] = %s", numfieldsvar, j, omitline)
  808. }
  809. x.linef("var %snn%s int", genTempVarPfx, i)
  810. x.linef("if %s || %s {", ti2arrayvar, struct2arrvar) // if ti.toArray {
  811. x.line("r.EncodeArrayStart(" + strconv.FormatInt(int64(len(tisfi)), 10) + ")")
  812. x.linef("} else {") // if not ti.toArray
  813. x.linef("%snn%s = %v", genTempVarPfx, i, nn)
  814. x.linef("for _, b := range %s { if b { %snn%s++ } }", numfieldsvar, genTempVarPfx, i)
  815. x.linef("r.EncodeMapStart(%snn%s)", genTempVarPfx, i)
  816. x.linef("%snn%s = %v", genTempVarPfx, i, 0)
  817. // x.line("r.EncodeMapStart(" + strconv.FormatInt(int64(len(tisfi)), 10) + ")")
  818. x.line("}") // close if not StructToArray
  819. for j, si := range tisfi {
  820. i := x.varsfx()
  821. isNilVarName := genTempVarPfx + "n" + i
  822. var labelUsed bool
  823. var t2 reflect.StructField
  824. if si.i != -1 {
  825. t2 = t.Field(int(si.i))
  826. } else {
  827. t2typ := t
  828. varname3 := varname
  829. for _, ix := range si.is {
  830. // fmt.Printf("%%%% %v, ix: %v\n", t2typ, ix)
  831. for t2typ.Kind() == reflect.Ptr {
  832. t2typ = t2typ.Elem()
  833. }
  834. t2 = t2typ.Field(ix)
  835. t2typ = t2.Type
  836. varname3 = varname3 + "." + t2.Name
  837. if t2typ.Kind() == reflect.Ptr {
  838. if !labelUsed {
  839. x.line("var " + isNilVarName + " bool")
  840. }
  841. x.line("if " + varname3 + " == nil { " + isNilVarName + " = true ")
  842. x.line("goto LABEL" + i)
  843. x.line("}")
  844. labelUsed = true
  845. // "varname3 = new(" + x.genTypeName(t3.Elem()) + ") }")
  846. }
  847. }
  848. // t2 = t.FieldByIndex(si.is)
  849. }
  850. if labelUsed {
  851. x.line("LABEL" + i + ":")
  852. }
  853. // if the type of the field is a Selfer, or one of the ones
  854. x.linef("if %s || %s {", ti2arrayvar, struct2arrvar) // if ti.toArray
  855. if labelUsed {
  856. x.line("if " + isNilVarName + " { r.EncodeNil() } else { ")
  857. }
  858. x.linef("z.EncSendContainerState(codecSelfer_containerArrayElem%s)", x.xs)
  859. if si.omitEmpty {
  860. x.linef("if %s[%v] {", numfieldsvar, j)
  861. }
  862. x.encVar(varname+"."+t2.Name, t2.Type)
  863. if si.omitEmpty {
  864. x.linef("} else {")
  865. x.encZero(t2.Type)
  866. x.linef("}")
  867. }
  868. if labelUsed {
  869. x.line("}")
  870. }
  871. x.linef("} else {") // if not ti.toArray
  872. if si.omitEmpty {
  873. x.linef("if %s[%v] {", numfieldsvar, j)
  874. }
  875. x.linef("z.EncSendContainerState(codecSelfer_containerMapKey%s)", x.xs)
  876. x.line("r.EncodeString(codecSelferC_UTF8" + x.xs + ", string(\"" + si.encName + "\"))")
  877. x.linef("z.EncSendContainerState(codecSelfer_containerMapValue%s)", x.xs)
  878. if labelUsed {
  879. x.line("if " + isNilVarName + " { r.EncodeNil() } else { ")
  880. x.encVar(varname+"."+t2.Name, t2.Type)
  881. x.line("}")
  882. } else {
  883. x.encVar(varname+"."+t2.Name, t2.Type)
  884. }
  885. if si.omitEmpty {
  886. x.line("}")
  887. }
  888. x.linef("} ") // end if/else ti.toArray
  889. }
  890. x.linef("if %s || %s {", ti2arrayvar, struct2arrvar) // if ti.toArray {
  891. x.linef("z.EncSendContainerState(codecSelfer_containerArrayEnd%s)", x.xs)
  892. x.line("} else {")
  893. x.linef("z.EncSendContainerState(codecSelfer_containerMapEnd%s)", x.xs)
  894. x.line("}")
  895. }
  896. func (x *genRunner) encListFallback(varname string, t reflect.Type) {
  897. i := x.varsfx()
  898. g := genTempVarPfx
  899. x.line("r.EncodeArrayStart(len(" + varname + "))")
  900. if t.Kind() == reflect.Chan {
  901. x.linef("for %si%s, %si2%s := 0, len(%s); %si%s < %si2%s; %si%s++ {", g, i, g, i, varname, g, i, g, i, g, i)
  902. x.linef("z.EncSendContainerState(codecSelfer_containerArrayElem%s)", x.xs)
  903. x.linef("%sv%s := <-%s", g, i, varname)
  904. } else {
  905. // x.linef("for %si%s, %sv%s := range %s {", genTempVarPfx, i, genTempVarPfx, i, varname)
  906. x.linef("for _, %sv%s := range %s {", genTempVarPfx, i, varname)
  907. x.linef("z.EncSendContainerState(codecSelfer_containerArrayElem%s)", x.xs)
  908. }
  909. x.encVar(genTempVarPfx+"v"+i, t.Elem())
  910. x.line("}")
  911. x.linef("z.EncSendContainerState(codecSelfer_containerArrayEnd%s)", x.xs)
  912. }
  913. func (x *genRunner) encMapFallback(varname string, t reflect.Type) {
  914. // TODO: expand this to handle canonical.
  915. i := x.varsfx()
  916. x.line("r.EncodeMapStart(len(" + varname + "))")
  917. x.linef("for %sk%s, %sv%s := range %s {", genTempVarPfx, i, genTempVarPfx, i, varname)
  918. // x.line("for " + genTempVarPfx + "k" + i + ", " + genTempVarPfx + "v" + i + " := range " + varname + " {")
  919. x.linef("z.EncSendContainerState(codecSelfer_containerMapKey%s)", x.xs)
  920. x.encVar(genTempVarPfx+"k"+i, t.Key())
  921. x.linef("z.EncSendContainerState(codecSelfer_containerMapValue%s)", x.xs)
  922. x.encVar(genTempVarPfx+"v"+i, t.Elem())
  923. x.line("}")
  924. x.linef("z.EncSendContainerState(codecSelfer_containerMapEnd%s)", x.xs)
  925. }
  926. func (x *genRunner) decVar(varname string, t reflect.Type, canBeNil bool) {
  927. // We only encode as nil if a nillable value.
  928. // This removes some of the wasted checks for TryDecodeAsNil.
  929. // We need to think about this more, to see what happens if omitempty, etc
  930. // cause a nil value to be stored when something is expected.
  931. // This could happen when decoding from a struct encoded as an array.
  932. // For that, decVar should be called with canNil=true, to force true as its value.
  933. i := x.varsfx()
  934. if !canBeNil {
  935. canBeNil = genAnythingCanBeNil || !genIsImmutable(t)
  936. }
  937. if canBeNil {
  938. x.line("if r.TryDecodeAsNil() {")
  939. if t.Kind() == reflect.Ptr {
  940. x.line("if " + varname + " != nil { ")
  941. // if varname is a field of a struct (has a dot in it),
  942. // then just set it to nil
  943. if strings.IndexByte(varname, '.') != -1 {
  944. x.line(varname + " = nil")
  945. } else {
  946. x.line("*" + varname + " = " + x.genZeroValueR(t.Elem()))
  947. }
  948. x.line("}")
  949. } else {
  950. x.line(varname + " = " + x.genZeroValueR(t))
  951. }
  952. x.line("} else {")
  953. } else {
  954. x.line("// cannot be nil")
  955. }
  956. if t.Kind() != reflect.Ptr {
  957. if x.decTryAssignPrimitive(varname, t) {
  958. x.line(genTempVarPfx + "v" + i + " := &" + varname)
  959. x.dec(genTempVarPfx+"v"+i, t)
  960. }
  961. } else {
  962. x.linef("if %s == nil { %s = new(%s) }", varname, varname, x.genTypeName(t.Elem()))
  963. // Ensure we set underlying ptr to a non-nil value (so we can deref to it later).
  964. // There's a chance of a **T in here which is nil.
  965. var ptrPfx string
  966. for t = t.Elem(); t.Kind() == reflect.Ptr; t = t.Elem() {
  967. ptrPfx += "*"
  968. x.linef("if %s%s == nil { %s%s = new(%s)}",
  969. ptrPfx, varname, ptrPfx, varname, x.genTypeName(t))
  970. }
  971. // if varname has [ in it, then create temp variable for this ptr thingie
  972. if strings.Index(varname, "[") >= 0 {
  973. varname2 := genTempVarPfx + "w" + i
  974. x.line(varname2 + " := " + varname)
  975. varname = varname2
  976. }
  977. if ptrPfx == "" {
  978. x.dec(varname, t)
  979. } else {
  980. x.line(genTempVarPfx + "z" + i + " := " + ptrPfx + varname)
  981. x.dec(genTempVarPfx+"z"+i, t)
  982. }
  983. }
  984. if canBeNil {
  985. x.line("} ")
  986. }
  987. }
  988. // dec will decode a variable (varname) of type ptrTo(t).
  989. // t is always a basetype (i.e. not of kind reflect.Ptr).
  990. func (x *genRunner) dec(varname string, t reflect.Type) {
  991. // assumptions:
  992. // - the varname is to a pointer already. No need to take address of it
  993. // - t is always a baseType T (not a *T, etc).
  994. rtid := reflect.ValueOf(t).Pointer()
  995. tptr := reflect.PtrTo(t)
  996. if x.checkForSelfer(t, varname) {
  997. if t.Implements(selferTyp) || tptr.Implements(selferTyp) {
  998. x.line(varname + ".CodecDecodeSelf(d)")
  999. return
  1000. }
  1001. if _, ok := x.td[rtid]; ok {
  1002. x.line(varname + ".CodecDecodeSelf(d)")
  1003. return
  1004. }
  1005. }
  1006. inlist := false
  1007. for _, t0 := range x.t {
  1008. if t == t0 {
  1009. inlist = true
  1010. if x.checkForSelfer(t, varname) {
  1011. x.line(varname + ".CodecDecodeSelf(d)")
  1012. return
  1013. }
  1014. break
  1015. }
  1016. }
  1017. var rtidAdded bool
  1018. if t == x.tc {
  1019. x.td[rtid] = true
  1020. rtidAdded = true
  1021. }
  1022. // check if
  1023. // - type is RawExt
  1024. // - the type implements (Text|JSON|Binary)(Unm|M)arshal
  1025. mi := x.varsfx()
  1026. x.linef("%sm%s := z.DecBinary()", genTempVarPfx, mi)
  1027. x.linef("_ = %sm%s", genTempVarPfx, mi)
  1028. x.line("if false {") //start if block
  1029. defer func() { x.line("}") }() //end if block
  1030. if t == rawExtTyp {
  1031. x.linef("} else { r.DecodeExt(%v, 0, nil)", varname)
  1032. return
  1033. }
  1034. // HACK: Support for Builtins.
  1035. // Currently, only Binc supports builtins, and the only builtin type is time.Time.
  1036. // Have a method that returns the rtid for time.Time if Handle is Binc.
  1037. if t == timeTyp {
  1038. vrtid := genTempVarPfx + "m" + x.varsfx()
  1039. x.linef("} else if %s := z.TimeRtidIfBinc(); %s != 0 { ", vrtid, vrtid)
  1040. x.linef("r.DecodeBuiltin(%s, %s)", vrtid, varname)
  1041. }
  1042. // only check for extensions if the type is named, and has a packagePath.
  1043. if genImportPath(t) != "" && t.Name() != "" {
  1044. // first check if extensions are configued, before doing the interface conversion
  1045. x.linef("} else if z.HasExtensions() && z.DecExt(%s) {", varname)
  1046. }
  1047. if t.Implements(binaryUnmarshalerTyp) || tptr.Implements(binaryUnmarshalerTyp) {
  1048. x.linef("} else if %sm%s { z.DecBinaryUnmarshal(%v) ", genTempVarPfx, mi, varname)
  1049. }
  1050. if t.Implements(jsonUnmarshalerTyp) || tptr.Implements(jsonUnmarshalerTyp) {
  1051. x.linef("} else if !%sm%s && z.IsJSONHandle() { z.DecJSONUnmarshal(%v)", genTempVarPfx, mi, varname)
  1052. } else if t.Implements(textUnmarshalerTyp) || tptr.Implements(textUnmarshalerTyp) {
  1053. x.linef("} else if !%sm%s { z.DecTextUnmarshal(%v)", genTempVarPfx, mi, varname)
  1054. }
  1055. x.line("} else {")
  1056. // Since these are pointers, we cannot share, and have to use them one by one
  1057. switch t.Kind() {
  1058. case reflect.Int:
  1059. x.line("*((*int)(" + varname + ")) = int(r.DecodeInt(codecSelferBitsize" + x.xs + "))")
  1060. // x.line("z.DecInt((*int)(" + varname + "))")
  1061. case reflect.Int8:
  1062. x.line("*((*int8)(" + varname + ")) = int8(r.DecodeInt(8))")
  1063. // x.line("z.DecInt8((*int8)(" + varname + "))")
  1064. case reflect.Int16:
  1065. x.line("*((*int16)(" + varname + ")) = int16(r.DecodeInt(16))")
  1066. // x.line("z.DecInt16((*int16)(" + varname + "))")
  1067. case reflect.Int32:
  1068. x.line("*((*int32)(" + varname + ")) = int32(r.DecodeInt(32))")
  1069. // x.line("z.DecInt32((*int32)(" + varname + "))")
  1070. case reflect.Int64:
  1071. x.line("*((*int64)(" + varname + ")) = int64(r.DecodeInt(64))")
  1072. // x.line("z.DecInt64((*int64)(" + varname + "))")
  1073. case reflect.Uint:
  1074. x.line("*((*uint)(" + varname + ")) = uint(r.DecodeUint(codecSelferBitsize" + x.xs + "))")
  1075. // x.line("z.DecUint((*uint)(" + varname + "))")
  1076. case reflect.Uint8:
  1077. x.line("*((*uint8)(" + varname + ")) = uint8(r.DecodeUint(8))")
  1078. // x.line("z.DecUint8((*uint8)(" + varname + "))")
  1079. case reflect.Uint16:
  1080. x.line("*((*uint16)(" + varname + ")) = uint16(r.DecodeUint(16))")
  1081. //x.line("z.DecUint16((*uint16)(" + varname + "))")
  1082. case reflect.Uint32:
  1083. x.line("*((*uint32)(" + varname + ")) = uint32(r.DecodeUint(32))")
  1084. //x.line("z.DecUint32((*uint32)(" + varname + "))")
  1085. case reflect.Uint64:
  1086. x.line("*((*uint64)(" + varname + ")) = uint64(r.DecodeUint(64))")
  1087. //x.line("z.DecUint64((*uint64)(" + varname + "))")
  1088. case reflect.Uintptr:
  1089. x.line("*((*uintptr)(" + varname + ")) = uintptr(r.DecodeUint(codecSelferBitsize" + x.xs + "))")
  1090. case reflect.Float32:
  1091. x.line("*((*float32)(" + varname + ")) = float32(r.DecodeFloat(true))")
  1092. //x.line("z.DecFloat32((*float32)(" + varname + "))")
  1093. case reflect.Float64:
  1094. x.line("*((*float64)(" + varname + ")) = float64(r.DecodeFloat(false))")
  1095. // x.line("z.DecFloat64((*float64)(" + varname + "))")
  1096. case reflect.Bool:
  1097. x.line("*((*bool)(" + varname + ")) = r.DecodeBool()")
  1098. // x.line("z.DecBool((*bool)(" + varname + "))")
  1099. case reflect.String:
  1100. x.line("*((*string)(" + varname + ")) = r.DecodeString()")
  1101. // x.line("z.DecString((*string)(" + varname + "))")
  1102. case reflect.Array, reflect.Chan:
  1103. x.xtraSM(varname, false, t)
  1104. // x.decListFallback(varname, rtid, true, t)
  1105. case reflect.Slice:
  1106. // if a []uint8, call dedicated function
  1107. // if a known fastpath slice, call dedicated function
  1108. // else write encode function in-line.
  1109. // - if elements are primitives or Selfers, call dedicated function on each member.
  1110. // - else call Encoder.encode(XXX) on it.
  1111. if rtid == uint8SliceTypId {
  1112. x.line("*" + varname + " = r.DecodeBytes(*(*[]byte)(" + varname + "), false, false)")
  1113. } else if fastpathAV.index(rtid) != -1 {
  1114. g := x.newGenV(t)
  1115. x.line("z.F." + g.MethodNamePfx("Dec", false) + "X(" + varname + ", false, d)")
  1116. } else {
  1117. x.xtraSM(varname, false, t)
  1118. // x.decListFallback(varname, rtid, false, t)
  1119. }
  1120. case reflect.Map:
  1121. // if a known fastpath map, call dedicated function
  1122. // else write encode function in-line.
  1123. // - if elements are primitives or Selfers, call dedicated function on each member.
  1124. // - else call Encoder.encode(XXX) on it.
  1125. if fastpathAV.index(rtid) != -1 {
  1126. g := x.newGenV(t)
  1127. x.line("z.F." + g.MethodNamePfx("Dec", false) + "X(" + varname + ", false, d)")
  1128. } else {
  1129. x.xtraSM(varname, false, t)
  1130. // x.decMapFallback(varname, rtid, t)
  1131. }
  1132. case reflect.Struct:
  1133. if inlist {
  1134. x.decStruct(varname, rtid, t)
  1135. } else {
  1136. // delete(x.td, rtid)
  1137. x.line("z.DecFallback(" + varname + ", false)")
  1138. }
  1139. default:
  1140. if rtidAdded {
  1141. delete(x.te, rtid)
  1142. }
  1143. x.line("z.DecFallback(" + varname + ", true)")
  1144. }
  1145. }
  1146. func (x *genRunner) decTryAssignPrimitive(varname string, t reflect.Type) (tryAsPtr bool) {
  1147. // We have to use the actual type name when doing a direct assignment.
  1148. // We don't have the luxury of casting the pointer to the underlying type.
  1149. //
  1150. // Consequently, in the situation of a
  1151. // type Message int32
  1152. // var x Message
  1153. // var i int32 = 32
  1154. // x = i // this will bomb
  1155. // x = Message(i) // this will work
  1156. // *((*int32)(&x)) = i // this will work
  1157. //
  1158. // Consequently, we replace:
  1159. // case reflect.Uint32: x.line(varname + " = uint32(r.DecodeUint(32))")
  1160. // with:
  1161. // case reflect.Uint32: x.line(varname + " = " + genTypeNamePrim(t, x.tc) + "(r.DecodeUint(32))")
  1162. xfn := func(t reflect.Type) string {
  1163. return x.genTypeNamePrim(t)
  1164. }
  1165. switch t.Kind() {
  1166. case reflect.Int:
  1167. x.linef("%s = %s(r.DecodeInt(codecSelferBitsize%s))", varname, xfn(t), x.xs)
  1168. case reflect.Int8:
  1169. x.linef("%s = %s(r.DecodeInt(8))", varname, xfn(t))
  1170. case reflect.Int16:
  1171. x.linef("%s = %s(r.DecodeInt(16))", varname, xfn(t))
  1172. case reflect.Int32:
  1173. x.linef("%s = %s(r.DecodeInt(32))", varname, xfn(t))
  1174. case reflect.Int64:
  1175. x.linef("%s = %s(r.DecodeInt(64))", varname, xfn(t))
  1176. case reflect.Uint:
  1177. x.linef("%s = %s(r.DecodeUint(codecSelferBitsize%s))", varname, xfn(t), x.xs)
  1178. case reflect.Uint8:
  1179. x.linef("%s = %s(r.DecodeUint(8))", varname, xfn(t))
  1180. case reflect.Uint16:
  1181. x.linef("%s = %s(r.DecodeUint(16))", varname, xfn(t))
  1182. case reflect.Uint32:
  1183. x.linef("%s = %s(r.DecodeUint(32))", varname, xfn(t))
  1184. case reflect.Uint64:
  1185. x.linef("%s = %s(r.DecodeUint(64))", varname, xfn(t))
  1186. case reflect.Uintptr:
  1187. x.linef("%s = %s(r.DecodeUint(codecSelferBitsize%s))", varname, xfn(t), x.xs)
  1188. case reflect.Float32:
  1189. x.linef("%s = %s(r.DecodeFloat(true))", varname, xfn(t))
  1190. case reflect.Float64:
  1191. x.linef("%s = %s(r.DecodeFloat(false))", varname, xfn(t))
  1192. case reflect.Bool:
  1193. x.linef("%s = %s(r.DecodeBool())", varname, xfn(t))
  1194. case reflect.String:
  1195. x.linef("%s = %s(r.DecodeString())", varname, xfn(t))
  1196. default:
  1197. tryAsPtr = true
  1198. }
  1199. return
  1200. }
  1201. func (x *genRunner) decListFallback(varname string, rtid uintptr, t reflect.Type) {
  1202. type tstruc struct {
  1203. TempVar string
  1204. Rand string
  1205. Varname string
  1206. CTyp string
  1207. Typ string
  1208. Immutable bool
  1209. Size int
  1210. }
  1211. telem := t.Elem()
  1212. ts := tstruc{genTempVarPfx, x.varsfx(), varname, x.genTypeName(t), x.genTypeName(telem), genIsImmutable(telem), int(telem.Size())}
  1213. funcs := make(template.FuncMap)
  1214. funcs["decLineVar"] = func(varname string) string {
  1215. x.decVar(varname, telem, false)
  1216. return ""
  1217. }
  1218. funcs["decLine"] = func(pfx string) string {
  1219. x.decVar(ts.TempVar+pfx+ts.Rand, reflect.PtrTo(telem), false)
  1220. return ""
  1221. }
  1222. funcs["var"] = func(s string) string {
  1223. return ts.TempVar + s + ts.Rand
  1224. }
  1225. funcs["zero"] = func() string {
  1226. return x.genZeroValueR(telem)
  1227. }
  1228. funcs["isArray"] = func() bool {
  1229. return t.Kind() == reflect.Array
  1230. }
  1231. funcs["isSlice"] = func() bool {
  1232. return t.Kind() == reflect.Slice
  1233. }
  1234. funcs["isChan"] = func() bool {
  1235. return t.Kind() == reflect.Chan
  1236. }
  1237. tm, err := template.New("").Funcs(funcs).Parse(genDecListTmpl)
  1238. if err != nil {
  1239. panic(err)
  1240. }
  1241. if err = tm.Execute(x.w, &ts); err != nil {
  1242. panic(err)
  1243. }
  1244. }
  1245. func (x *genRunner) decMapFallback(varname string, rtid uintptr, t reflect.Type) {
  1246. type tstruc struct {
  1247. TempVar string
  1248. Sfx string
  1249. Rand string
  1250. Varname string
  1251. KTyp string
  1252. Typ string
  1253. Size int
  1254. }
  1255. telem := t.Elem()
  1256. tkey := t.Key()
  1257. ts := tstruc{
  1258. genTempVarPfx, x.xs, x.varsfx(), varname, x.genTypeName(tkey),
  1259. x.genTypeName(telem), int(telem.Size() + tkey.Size()),
  1260. }
  1261. funcs := make(template.FuncMap)
  1262. funcs["decElemZero"] = func() string {
  1263. return x.genZeroValueR(telem)
  1264. }
  1265. funcs["decElemKindImmutable"] = func() bool {
  1266. return genIsImmutable(telem)
  1267. }
  1268. funcs["decElemKindPtr"] = func() bool {
  1269. return telem.Kind() == reflect.Ptr
  1270. }
  1271. funcs["decElemKindIntf"] = func() bool {
  1272. return telem.Kind() == reflect.Interface
  1273. }
  1274. funcs["decLineVarK"] = func(varname string) string {
  1275. x.decVar(varname, tkey, false)
  1276. return ""
  1277. }
  1278. funcs["decLineVar"] = func(varname string) string {
  1279. x.decVar(varname, telem, false)
  1280. return ""
  1281. }
  1282. funcs["decLineK"] = func(pfx string) string {
  1283. x.decVar(ts.TempVar+pfx+ts.Rand, reflect.PtrTo(tkey), false)
  1284. return ""
  1285. }
  1286. funcs["decLine"] = func(pfx string) string {
  1287. x.decVar(ts.TempVar+pfx+ts.Rand, reflect.PtrTo(telem), false)
  1288. return ""
  1289. }
  1290. funcs["var"] = func(s string) string {
  1291. return ts.TempVar + s + ts.Rand
  1292. }
  1293. tm, err := template.New("").Funcs(funcs).Parse(genDecMapTmpl)
  1294. if err != nil {
  1295. panic(err)
  1296. }
  1297. if err = tm.Execute(x.w, &ts); err != nil {
  1298. panic(err)
  1299. }
  1300. }
  1301. func (x *genRunner) decStructMapSwitch(kName string, varname string, rtid uintptr, t reflect.Type) {
  1302. ti := x.ti.get(rtid, t)
  1303. tisfi := ti.sfip // always use sequence from file. decStruct expects same thing.
  1304. x.line("switch (" + kName + ") {")
  1305. for _, si := range tisfi {
  1306. x.line("case \"" + si.encName + "\":")
  1307. var t2 reflect.StructField
  1308. if si.i != -1 {
  1309. t2 = t.Field(int(si.i))
  1310. } else {
  1311. //we must accomodate anonymous fields, where the embedded field is a nil pointer in the value.
  1312. // t2 = t.FieldByIndex(si.is)
  1313. t2typ := t
  1314. varname3 := varname
  1315. for _, ix := range si.is {
  1316. for t2typ.Kind() == reflect.Ptr {
  1317. t2typ = t2typ.Elem()
  1318. }
  1319. t2 = t2typ.Field(ix)
  1320. t2typ = t2.Type
  1321. varname3 = varname3 + "." + t2.Name
  1322. if t2typ.Kind() == reflect.Ptr {
  1323. x.linef("if %s == nil { %s = new(%s) }", varname3, varname3, x.genTypeName(t2typ.Elem()))
  1324. }
  1325. }
  1326. }
  1327. x.decVar(varname+"."+t2.Name, t2.Type, false)
  1328. }
  1329. x.line("default:")
  1330. // pass the slice here, so that the string will not escape, and maybe save allocation
  1331. x.line("z.DecStructFieldNotFound(-1, " + kName + ")")
  1332. x.line("} // end switch " + kName)
  1333. }
  1334. func (x *genRunner) decStructMap(varname, lenvarname string, rtid uintptr, t reflect.Type, style genStructMapStyle) {
  1335. tpfx := genTempVarPfx
  1336. i := x.varsfx()
  1337. kName := tpfx + "s" + i
  1338. // We thought to use ReadStringAsBytes, as go compiler might optimize the copy out.
  1339. // However, using that was more expensive, as it seems that the switch expression
  1340. // is evaluated each time.
  1341. //
  1342. // We could depend on decodeString using a temporary/shared buffer internally.
  1343. // However, this model of creating a byte array, and using explicitly is faster,
  1344. // and allows optional use of unsafe []byte->string conversion without alloc.
  1345. // Also, ensure that the slice array doesn't escape.
  1346. // That will help escape analysis prevent allocation when it gets better.
  1347. // x.line("var " + kName + "Arr = [32]byte{} // default string to decode into")
  1348. // x.line("var " + kName + "Slc = " + kName + "Arr[:] // default slice to decode into")
  1349. // use the scratch buffer to avoid allocation (most field names are < 32).
  1350. x.line("var " + kName + "Slc = z.DecScratchBuffer() // default slice to decode into")
  1351. x.line("_ = " + kName + "Slc")
  1352. switch style {
  1353. case genStructMapStyleLenPrefix:
  1354. x.linef("for %sj%s := 0; %sj%s < %s; %sj%s++ {", tpfx, i, tpfx, i, lenvarname, tpfx, i)
  1355. case genStructMapStyleCheckBreak:
  1356. x.linef("for %sj%s := 0; !r.CheckBreak(); %sj%s++ {", tpfx, i, tpfx, i)
  1357. default: // 0, otherwise.
  1358. x.linef("var %shl%s bool = %s >= 0", tpfx, i, lenvarname) // has length
  1359. x.linef("for %sj%s := 0; ; %sj%s++ {", tpfx, i, tpfx, i)
  1360. x.linef("if %shl%s { if %sj%s >= %s { break }", tpfx, i, tpfx, i, lenvarname)
  1361. x.line("} else { if r.CheckBreak() { break }; }")
  1362. }
  1363. x.linef("z.DecSendContainerState(codecSelfer_containerMapKey%s)", x.xs)
  1364. x.line(kName + "Slc = r.DecodeBytes(" + kName + "Slc, true, true)")
  1365. // let string be scoped to this loop alone, so it doesn't escape.
  1366. if x.unsafe {
  1367. x.line(kName + "SlcHdr := codecSelferUnsafeString" + x.xs + "{uintptr(unsafe.Pointer(&" +
  1368. kName + "Slc[0])), len(" + kName + "Slc)}")
  1369. x.line(kName + " := *(*string)(unsafe.Pointer(&" + kName + "SlcHdr))")
  1370. } else {
  1371. x.line(kName + " := string(" + kName + "Slc)")
  1372. }
  1373. x.linef("z.DecSendContainerState(codecSelfer_containerMapValue%s)", x.xs)
  1374. x.decStructMapSwitch(kName, varname, rtid, t)
  1375. x.line("} // end for " + tpfx + "j" + i)
  1376. x.linef("z.DecSendContainerState(codecSelfer_containerMapEnd%s)", x.xs)
  1377. }
  1378. func (x *genRunner) decStructArray(varname, lenvarname, breakString string, rtid uintptr, t reflect.Type) {
  1379. tpfx := genTempVarPfx
  1380. i := x.varsfx()
  1381. ti := x.ti.get(rtid, t)
  1382. tisfi := ti.sfip // always use sequence from file. decStruct expects same thing.
  1383. x.linef("var %sj%s int", tpfx, i)
  1384. x.linef("var %sb%s bool", tpfx, i) // break
  1385. x.linef("var %shl%s bool = %s >= 0", tpfx, i, lenvarname) // has length
  1386. for _, si := range tisfi {
  1387. var t2 reflect.StructField
  1388. if si.i != -1 {
  1389. t2 = t.Field(int(si.i))
  1390. } else {
  1391. //we must accomodate anonymous fields, where the embedded field is a nil pointer in the value.
  1392. // t2 = t.FieldByIndex(si.is)
  1393. t2typ := t
  1394. varname3 := varname
  1395. for _, ix := range si.is {
  1396. for t2typ.Kind() == reflect.Ptr {
  1397. t2typ = t2typ.Elem()
  1398. }
  1399. t2 = t2typ.Field(ix)
  1400. t2typ = t2.Type
  1401. varname3 = varname3 + "." + t2.Name
  1402. if t2typ.Kind() == reflect.Ptr {
  1403. x.linef("if %s == nil { %s = new(%s) }", varname3, varname3, x.genTypeName(t2typ.Elem()))
  1404. }
  1405. }
  1406. }
  1407. x.linef("%sj%s++; if %shl%s { %sb%s = %sj%s > %s } else { %sb%s = r.CheckBreak() }",
  1408. tpfx, i, tpfx, i, tpfx, i,
  1409. tpfx, i, lenvarname, tpfx, i)
  1410. x.linef("if %sb%s { z.DecSendContainerState(codecSelfer_containerArrayEnd%s); %s }",
  1411. tpfx, i, x.xs, breakString)
  1412. x.linef("z.DecSendContainerState(codecSelfer_containerArrayElem%s)", x.xs)
  1413. x.decVar(varname+"."+t2.Name, t2.Type, true)
  1414. }
  1415. // read remaining values and throw away.
  1416. x.line("for {")
  1417. x.linef("%sj%s++; if %shl%s { %sb%s = %sj%s > %s } else { %sb%s = r.CheckBreak() }",
  1418. tpfx, i, tpfx, i, tpfx, i,
  1419. tpfx, i, lenvarname, tpfx, i)
  1420. x.linef("if %sb%s { break }", tpfx, i)
  1421. x.linef("z.DecSendContainerState(codecSelfer_containerArrayElem%s)", x.xs)
  1422. x.linef(`z.DecStructFieldNotFound(%sj%s - 1, "")`, tpfx, i)
  1423. x.line("}")
  1424. x.linef("z.DecSendContainerState(codecSelfer_containerArrayEnd%s)", x.xs)
  1425. }
  1426. func (x *genRunner) decStruct(varname string, rtid uintptr, t reflect.Type) {
  1427. // if container is map
  1428. i := x.varsfx()
  1429. x.linef("%sct%s := r.ContainerType()", genTempVarPfx, i)
  1430. x.linef("if %sct%s == codecSelferValueTypeMap%s {", genTempVarPfx, i, x.xs)
  1431. x.line(genTempVarPfx + "l" + i + " := r.ReadMapStart()")
  1432. x.linef("if %sl%s == 0 {", genTempVarPfx, i)
  1433. x.linef("z.DecSendContainerState(codecSelfer_containerMapEnd%s)", x.xs)
  1434. if genUseOneFunctionForDecStructMap {
  1435. x.line("} else { ")
  1436. x.linef("x.codecDecodeSelfFromMap(%sl%s, d)", genTempVarPfx, i)
  1437. } else {
  1438. x.line("} else if " + genTempVarPfx + "l" + i + " > 0 { ")
  1439. x.line("x.codecDecodeSelfFromMapLenPrefix(" + genTempVarPfx + "l" + i + ", d)")
  1440. x.line("} else {")
  1441. x.line("x.codecDecodeSelfFromMapCheckBreak(" + genTempVarPfx + "l" + i + ", d)")
  1442. }
  1443. x.line("}")
  1444. // else if container is array
  1445. x.linef("} else if %sct%s == codecSelferValueTypeArray%s {", genTempVarPfx, i, x.xs)
  1446. x.line(genTempVarPfx + "l" + i + " := r.ReadArrayStart()")
  1447. x.linef("if %sl%s == 0 {", genTempVarPfx, i)
  1448. x.linef("z.DecSendContainerState(codecSelfer_containerArrayEnd%s)", x.xs)
  1449. x.line("} else { ")
  1450. x.linef("x.codecDecodeSelfFromArray(%sl%s, d)", genTempVarPfx, i)
  1451. x.line("}")
  1452. // else panic
  1453. x.line("} else { ")
  1454. x.line("panic(codecSelferOnlyMapOrArrayEncodeToStructErr" + x.xs + ")")
  1455. x.line("} ")
  1456. }
  1457. // --------
  1458. type genV struct {
  1459. // genV is either a primitive (Primitive != "") or a map (MapKey != "") or a slice
  1460. MapKey string
  1461. Elem string
  1462. Primitive string
  1463. Size int
  1464. }
  1465. func (x *genRunner) newGenV(t reflect.Type) (v genV) {
  1466. switch t.Kind() {
  1467. case reflect.Slice, reflect.Array:
  1468. te := t.Elem()
  1469. v.Elem = x.genTypeName(te)
  1470. v.Size = int(te.Size())
  1471. case reflect.Map:
  1472. te, tk := t.Elem(), t.Key()
  1473. v.Elem = x.genTypeName(te)
  1474. v.MapKey = x.genTypeName(tk)
  1475. v.Size = int(te.Size() + tk.Size())
  1476. default:
  1477. panic("unexpected type for newGenV. Requires map or slice type")
  1478. }
  1479. return
  1480. }
  1481. func (x *genV) MethodNamePfx(prefix string, prim bool) string {
  1482. var name []byte
  1483. if prefix != "" {
  1484. name = append(name, prefix...)
  1485. }
  1486. if prim {
  1487. name = append(name, genTitleCaseName(x.Primitive)...)
  1488. } else {
  1489. if x.MapKey == "" {
  1490. name = append(name, "Slice"...)
  1491. } else {
  1492. name = append(name, "Map"...)
  1493. name = append(name, genTitleCaseName(x.MapKey)...)
  1494. }
  1495. name = append(name, genTitleCaseName(x.Elem)...)
  1496. }
  1497. return string(name)
  1498. }
  1499. var genCheckVendor = os.Getenv("GO15VENDOREXPERIMENT") == "1"
  1500. // genImportPath returns import path of a non-predeclared named typed, or an empty string otherwise.
  1501. //
  1502. // This handles the misbehaviour that occurs when 1.5-style vendoring is enabled,
  1503. // where PkgPath returns the full path, including the vendoring pre-fix that should have been stripped.
  1504. // We strip it here.
  1505. func genImportPath(t reflect.Type) (s string) {
  1506. s = t.PkgPath()
  1507. if genCheckVendor {
  1508. // HACK: Misbehaviour occurs in go 1.5. May have to re-visit this later.
  1509. // if s contains /vendor/ OR startsWith vendor/, then return everything after it.
  1510. const vendorStart = "vendor/"
  1511. const vendorInline = "/vendor/"
  1512. if i := strings.LastIndex(s, vendorInline); i >= 0 {
  1513. s = s[i+len(vendorInline):]
  1514. } else if strings.HasPrefix(s, vendorStart) {
  1515. s = s[len(vendorStart):]
  1516. }
  1517. }
  1518. return
  1519. }
  1520. func genNonPtr(t reflect.Type) reflect.Type {
  1521. for t.Kind() == reflect.Ptr {
  1522. t = t.Elem()
  1523. }
  1524. return t
  1525. }
  1526. func genTitleCaseName(s string) string {
  1527. switch s {
  1528. case "interface{}":
  1529. return "Intf"
  1530. default:
  1531. return strings.ToUpper(s[0:1]) + s[1:]
  1532. }
  1533. }
  1534. func genMethodNameT(t reflect.Type, tRef reflect.Type) (n string) {
  1535. var ptrPfx string
  1536. for t.Kind() == reflect.Ptr {
  1537. ptrPfx += "Ptrto"
  1538. t = t.Elem()
  1539. }
  1540. tstr := t.String()
  1541. if tn := t.Name(); tn != "" {
  1542. if tRef != nil && genImportPath(t) == genImportPath(tRef) {
  1543. return ptrPfx + tn
  1544. } else {
  1545. if genQNameRegex.MatchString(tstr) {
  1546. return ptrPfx + strings.Replace(tstr, ".", "_", 1000)
  1547. } else {
  1548. return ptrPfx + genCustomTypeName(tstr)
  1549. }
  1550. }
  1551. }
  1552. switch t.Kind() {
  1553. case reflect.Map:
  1554. return ptrPfx + "Map" + genMethodNameT(t.Key(), tRef) + genMethodNameT(t.Elem(), tRef)
  1555. case reflect.Slice:
  1556. return ptrPfx + "Slice" + genMethodNameT(t.Elem(), tRef)
  1557. case reflect.Array:
  1558. return ptrPfx + "Array" + strconv.FormatInt(int64(t.Len()), 10) + genMethodNameT(t.Elem(), tRef)
  1559. case reflect.Chan:
  1560. var cx string
  1561. switch t.ChanDir() {
  1562. case reflect.SendDir:
  1563. cx = "ChanSend"
  1564. case reflect.RecvDir:
  1565. cx = "ChanRecv"
  1566. default:
  1567. cx = "Chan"
  1568. }
  1569. return ptrPfx + cx + genMethodNameT(t.Elem(), tRef)
  1570. default:
  1571. if t == intfTyp {
  1572. return ptrPfx + "Interface"
  1573. } else {
  1574. if tRef != nil && genImportPath(t) == genImportPath(tRef) {
  1575. if t.Name() != "" {
  1576. return ptrPfx + t.Name()
  1577. } else {
  1578. return ptrPfx + genCustomTypeName(tstr)
  1579. }
  1580. } else {
  1581. // best way to get the package name inclusive
  1582. // return ptrPfx + strings.Replace(tstr, ".", "_", 1000)
  1583. // return ptrPfx + genBase64enc.EncodeToString([]byte(tstr))
  1584. if t.Name() != "" && genQNameRegex.MatchString(tstr) {
  1585. return ptrPfx + strings.Replace(tstr, ".", "_", 1000)
  1586. } else {
  1587. return ptrPfx + genCustomTypeName(tstr)
  1588. }
  1589. }
  1590. }
  1591. }
  1592. }
  1593. // genCustomNameForType base64encodes the t.String() value in such a way
  1594. // that it can be used within a function name.
  1595. func genCustomTypeName(tstr string) string {
  1596. len2 := genBase64enc.EncodedLen(len(tstr))
  1597. bufx := make([]byte, len2)
  1598. genBase64enc.Encode(bufx, []byte(tstr))
  1599. for i := len2 - 1; i >= 0; i-- {
  1600. if bufx[i] == '=' {
  1601. len2--
  1602. } else {
  1603. break
  1604. }
  1605. }
  1606. return string(bufx[:len2])
  1607. }
  1608. func genIsImmutable(t reflect.Type) (v bool) {
  1609. return isImmutableKind(t.Kind())
  1610. }
  1611. type genInternal struct {
  1612. Values []genV
  1613. Unsafe bool
  1614. }
  1615. func (x genInternal) FastpathLen() (l int) {
  1616. for _, v := range x.Values {
  1617. if v.Primitive == "" {
  1618. l++
  1619. }
  1620. }
  1621. return
  1622. }
  1623. func genInternalZeroValue(s string) string {
  1624. switch s {
  1625. case "interface{}":
  1626. return "nil"
  1627. case "bool":
  1628. return "false"
  1629. case "string":
  1630. return `""`
  1631. default:
  1632. return "0"
  1633. }
  1634. }
  1635. func genInternalEncCommandAsString(s string, vname string) string {
  1636. switch s {
  1637. case "uint", "uint8", "uint16", "uint32", "uint64":
  1638. return "ee.EncodeUint(uint64(" + vname + "))"
  1639. case "int", "int8", "int16", "int32", "int64":
  1640. return "ee.EncodeInt(int64(" + vname + "))"
  1641. case "string":
  1642. return "ee.EncodeString(c_UTF8, " + vname + ")"
  1643. case "float32":
  1644. return "ee.EncodeFloat32(" + vname + ")"
  1645. case "float64":
  1646. return "ee.EncodeFloat64(" + vname + ")"
  1647. case "bool":
  1648. return "ee.EncodeBool(" + vname + ")"
  1649. case "symbol":
  1650. return "ee.EncodeSymbol(" + vname + ")"
  1651. default:
  1652. return "e.encode(" + vname + ")"
  1653. }
  1654. }
  1655. func genInternalDecCommandAsString(s string) string {
  1656. switch s {
  1657. case "uint":
  1658. return "uint(dd.DecodeUint(uintBitsize))"
  1659. case "uint8":
  1660. return "uint8(dd.DecodeUint(8))"
  1661. case "uint16":
  1662. return "uint16(dd.DecodeUint(16))"
  1663. case "uint32":
  1664. return "uint32(dd.DecodeUint(32))"
  1665. case "uint64":
  1666. return "dd.DecodeUint(64)"
  1667. case "uintptr":
  1668. return "uintptr(dd.DecodeUint(uintBitsize))"
  1669. case "int":
  1670. return "int(dd.DecodeInt(intBitsize))"
  1671. case "int8":
  1672. return "int8(dd.DecodeInt(8))"
  1673. case "int16":
  1674. return "int16(dd.DecodeInt(16))"
  1675. case "int32":
  1676. return "int32(dd.DecodeInt(32))"
  1677. case "int64":
  1678. return "dd.DecodeInt(64)"
  1679. case "string":
  1680. return "dd.DecodeString()"
  1681. case "float32":
  1682. return "float32(dd.DecodeFloat(true))"
  1683. case "float64":
  1684. return "dd.DecodeFloat(false)"
  1685. case "bool":
  1686. return "dd.DecodeBool()"
  1687. default:
  1688. panic(errors.New("gen internal: unknown type for decode: " + s))
  1689. }
  1690. }
  1691. func genInternalSortType(s string, elem bool) string {
  1692. for _, v := range [...]string{"int", "uint", "float", "bool", "string"} {
  1693. if strings.HasPrefix(s, v) {
  1694. if elem {
  1695. if v == "int" || v == "uint" || v == "float" {
  1696. return v + "64"
  1697. } else {
  1698. return v
  1699. }
  1700. }
  1701. return v + "Slice"
  1702. }
  1703. }
  1704. panic("sorttype: unexpected type: " + s)
  1705. }
  1706. // var genInternalMu sync.Mutex
  1707. var genInternalV genInternal
  1708. var genInternalTmplFuncs template.FuncMap
  1709. var genInternalOnce sync.Once
  1710. func genInternalInit() {
  1711. types := [...]string{
  1712. "interface{}",
  1713. "string",
  1714. "float32",
  1715. "float64",
  1716. "uint",
  1717. "uint8",
  1718. "uint16",
  1719. "uint32",
  1720. "uint64",
  1721. "uintptr",
  1722. "int",
  1723. "int8",
  1724. "int16",
  1725. "int32",
  1726. "int64",
  1727. "bool",
  1728. }
  1729. // keep as slice, so it is in specific iteration order.
  1730. // Initial order was uint64, string, interface{}, int, int64
  1731. mapvaltypes := [...]string{
  1732. "interface{}",
  1733. "string",
  1734. "uint",
  1735. "uint8",
  1736. "uint16",
  1737. "uint32",
  1738. "uint64",
  1739. "uintptr",
  1740. "int",
  1741. "int8",
  1742. "int16",
  1743. "int32",
  1744. "int64",
  1745. "float32",
  1746. "float64",
  1747. "bool",
  1748. }
  1749. wordSizeBytes := int(intBitsize) / 8
  1750. mapvaltypes2 := map[string]int{
  1751. "interface{}": 2 * wordSizeBytes,
  1752. "string": 2 * wordSizeBytes,
  1753. "uint": 1 * wordSizeBytes,
  1754. "uint8": 1,
  1755. "uint16": 2,
  1756. "uint32": 4,
  1757. "uint64": 8,
  1758. "uintptr": 1 * wordSizeBytes,
  1759. "int": 1 * wordSizeBytes,
  1760. "int8": 1,
  1761. "int16": 2,
  1762. "int32": 4,
  1763. "int64": 8,
  1764. "float32": 4,
  1765. "float64": 8,
  1766. "bool": 1,
  1767. }
  1768. var gt genInternal
  1769. // For each slice or map type, there must be a (symetrical) Encode and Decode fast-path function
  1770. for _, s := range types {
  1771. gt.Values = append(gt.Values, genV{Primitive: s, Size: mapvaltypes2[s]})
  1772. if s != "uint8" { // do not generate fast path for slice of bytes. Treat specially already.
  1773. gt.Values = append(gt.Values, genV{Elem: s, Size: mapvaltypes2[s]})
  1774. }
  1775. if _, ok := mapvaltypes2[s]; !ok {
  1776. gt.Values = append(gt.Values, genV{MapKey: s, Elem: s, Size: 2 * mapvaltypes2[s]})
  1777. }
  1778. for _, ms := range mapvaltypes {
  1779. gt.Values = append(gt.Values, genV{MapKey: s, Elem: ms, Size: mapvaltypes2[s] + mapvaltypes2[ms]})
  1780. }
  1781. }
  1782. funcs := make(template.FuncMap)
  1783. // funcs["haspfx"] = strings.HasPrefix
  1784. funcs["encmd"] = genInternalEncCommandAsString
  1785. funcs["decmd"] = genInternalDecCommandAsString
  1786. funcs["zerocmd"] = genInternalZeroValue
  1787. funcs["hasprefix"] = strings.HasPrefix
  1788. funcs["sorttype"] = genInternalSortType
  1789. genInternalV = gt
  1790. genInternalTmplFuncs = funcs
  1791. }
  1792. // genInternalGoFile is used to generate source files from templates.
  1793. // It is run by the program author alone.
  1794. // Unfortunately, it has to be exported so that it can be called from a command line tool.
  1795. // *** DO NOT USE ***
  1796. func genInternalGoFile(r io.Reader, w io.Writer, safe bool) (err error) {
  1797. genInternalOnce.Do(genInternalInit)
  1798. gt := genInternalV
  1799. gt.Unsafe = !safe
  1800. t := template.New("").Funcs(genInternalTmplFuncs)
  1801. tmplstr, err := ioutil.ReadAll(r)
  1802. if err != nil {
  1803. return
  1804. }
  1805. if t, err = t.Parse(string(tmplstr)); err != nil {
  1806. return
  1807. }
  1808. var out bytes.Buffer
  1809. err = t.Execute(&out, gt)
  1810. if err != nil {
  1811. return
  1812. }
  1813. bout, err := format.Source(out.Bytes())
  1814. if err != nil {
  1815. w.Write(out.Bytes()) // write out if error, so we can still see.
  1816. // w.Write(bout) // write out if error, as much as possible, so we can still see.
  1817. return
  1818. }
  1819. w.Write(bout)
  1820. return
  1821. }