gen.go 61 KB

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