unbox.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package checker
  2. type UnboxResult interface { UnboxResult() }
  3. func (impl Unboxed) UnboxResult() {}
  4. type Unboxed struct {
  5. Type Type
  6. }
  7. func (impl UnboxedButOpaque) UnboxResult() {}
  8. type UnboxedButOpaque struct {}
  9. func (impl UnboxFailed) UnboxResult() {}
  10. type UnboxFailed struct {}
  11. func Unbox(t Type, ctx ExprContext) UnboxResult {
  12. switch T := t.(type) {
  13. case NamedType:
  14. var g = ctx.ModuleInfo.Types[T.Name]
  15. switch gv := g.Value.(type) {
  16. case Boxed:
  17. var ctx_mod = ctx.GetModuleName()
  18. var type_mod = T.Name.ModuleName
  19. if gv.Opaque && ctx_mod != type_mod {
  20. return UnboxedButOpaque {}
  21. } else {
  22. return Unboxed {
  23. Type: FillTypeArgs(gv.InnerType, T.Args),
  24. }
  25. }
  26. }
  27. }
  28. return UnboxFailed {}
  29. }
  30. /**
  31. * Generic Template:
  32. * (${Repr},${ReprBrief}) = (Tuple,T) | (Bundle,B) | (Func,F)
  33. *
  34. type ${Repr}ReprResult interface { ${Repr}ReprResult() }
  35. func (impl ${Repr}) ${Repr}ReprResult() {}
  36. func (impl ${ReprBrief}R_Non${Repr}) ${Repr}ReprResult() {}
  37. type ${ReprBrief}R_Non${Repr} struct {}
  38. func (impl ${ReprBrief}R_${Repr}ButOpaque) ${Repr}ReprResult() {}
  39. type ${ReprBrief}R_${Repr}ButOpaque struct {}
  40. func Unbox${Repr}(t Type, ctx ExprContext) ${Repr}ReprResult {
  41. switch T := t.(type) {
  42. case NamedType:
  43. var g = ctx.ModuleInfo.Types[T.Name]
  44. switch gv := g.Value.(type) {
  45. case Boxed:
  46. var inner = FillTypeArgs(gv.InnerType, T.Args)
  47. switch inner_type := inner.(type) {
  48. case AnonymousType:
  49. switch inner_repr := inner_type.Repr.(type) {
  50. case ${Repr}:
  51. var ctx_mod = ctx.GetModuleName()
  52. var type_mod = T.Name.ModuleName
  53. if gv.Opaque && ctx_mod != type_mod {
  54. return ${ReprBrief}R_${Repr}ButOpaque {}
  55. } else {
  56. return inner_repr
  57. }
  58. }
  59. }
  60. }
  61. return ${ReprBrief}R_Non${Repr} {}
  62. case AnonymousType:
  63. switch r := T.Repr.(type) {
  64. case ${Repr}:
  65. return r
  66. default:
  67. return ${ReprBrief}R_Non${Repr} {}
  68. }
  69. default:
  70. return ${ReprBrief}R_Non${Repr} {}
  71. }
  72. }
  73. */
  74. type TupleReprResult interface { TupleReprResult() }
  75. func (impl Tuple) TupleReprResult() {}
  76. func (impl TR_NonTuple) TupleReprResult() {}
  77. type TR_NonTuple struct {}
  78. func (impl TR_TupleButOpaque) TupleReprResult() {}
  79. type TR_TupleButOpaque struct {}
  80. func UnboxTuple(t Type, ctx ExprContext) TupleReprResult {
  81. switch T := t.(type) {
  82. case NamedType:
  83. var g = ctx.ModuleInfo.Types[T.Name]
  84. switch gv := g.Value.(type) {
  85. case Boxed:
  86. var inner = FillTypeArgs(gv.InnerType, T.Args)
  87. switch inner_type := inner.(type) {
  88. case AnonymousType:
  89. switch inner_repr := inner_type.Repr.(type) {
  90. case Tuple:
  91. var ctx_mod = ctx.GetModuleName()
  92. var type_mod = T.Name.ModuleName
  93. if gv.Opaque && ctx_mod != type_mod {
  94. return TR_TupleButOpaque {}
  95. } else {
  96. return inner_repr
  97. }
  98. }
  99. }
  100. }
  101. return TR_NonTuple {}
  102. case AnonymousType:
  103. switch r := T.Repr.(type) {
  104. case Tuple:
  105. return r
  106. default:
  107. return TR_NonTuple {}
  108. }
  109. default:
  110. return TR_NonTuple {}
  111. }
  112. }
  113. type BundleReprResult interface { BundleReprResult() }
  114. func (impl Bundle) BundleReprResult() {}
  115. func (impl BR_NonBundle) BundleReprResult() {}
  116. type BR_NonBundle struct {}
  117. func (impl BR_BundleButOpaque) BundleReprResult() {}
  118. type BR_BundleButOpaque struct {}
  119. func UnboxBundle(t Type, ctx ExprContext) BundleReprResult {
  120. switch T := t.(type) {
  121. case NamedType:
  122. var g = ctx.ModuleInfo.Types[T.Name]
  123. switch gv := g.Value.(type) {
  124. case Boxed:
  125. var inner = FillTypeArgs(gv.InnerType, T.Args)
  126. switch inner_type := inner.(type) {
  127. case AnonymousType:
  128. switch inner_repr := inner_type.Repr.(type) {
  129. case Bundle:
  130. var ctx_mod = ctx.GetModuleName()
  131. var type_mod = T.Name.ModuleName
  132. if gv.Opaque && ctx_mod != type_mod {
  133. return BR_BundleButOpaque {}
  134. } else {
  135. return inner_repr
  136. }
  137. }
  138. }
  139. }
  140. return BR_NonBundle {}
  141. case AnonymousType:
  142. switch r := T.Repr.(type) {
  143. case Bundle:
  144. return r
  145. default:
  146. return BR_NonBundle {}
  147. }
  148. default:
  149. return BR_NonBundle {}
  150. }
  151. }
  152. type FuncReprResult interface { FuncReprResult() }
  153. func (impl Func) FuncReprResult() {}
  154. func (impl FR_NonFunc) FuncReprResult() {}
  155. type FR_NonFunc struct {}
  156. func (impl FR_FuncButOpaque) FuncReprResult() {}
  157. type FR_FuncButOpaque struct {}
  158. func UnboxFunc(t Type, ctx ExprContext) FuncReprResult {
  159. switch T := t.(type) {
  160. case NamedType:
  161. var g = ctx.ModuleInfo.Types[T.Name]
  162. switch gv := g.Value.(type) {
  163. case Boxed:
  164. var inner = FillTypeArgs(gv.InnerType, T.Args)
  165. switch inner_type := inner.(type) {
  166. case AnonymousType:
  167. switch inner_repr := inner_type.Repr.(type) {
  168. case Func:
  169. var ctx_mod = ctx.GetModuleName()
  170. var type_mod = T.Name.ModuleName
  171. if gv.Opaque && ctx_mod != type_mod {
  172. return FR_FuncButOpaque {}
  173. } else {
  174. return inner_repr
  175. }
  176. }
  177. }
  178. }
  179. return FR_NonFunc {}
  180. case AnonymousType:
  181. switch r := T.Repr.(type) {
  182. case Func:
  183. return r
  184. default:
  185. return FR_NonFunc {}
  186. }
  187. default:
  188. return FR_NonFunc {}
  189. }
  190. }