gen.go 64 KB

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