gen.go 62 KB

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