generics.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. package checker
  2. func GenericFunctionCall (
  3. f *GenericFunction,
  4. name string,
  5. index uint,
  6. type_args [] Type,
  7. arg SemiExpr,
  8. f_info ExprInfo,
  9. call_info ExprInfo,
  10. ctx ExprContext,
  11. unbox_count *uint, // mutate it instead of additional return value
  12. ) (Expr, *ExprError) {
  13. var type_arity = len(f.TypeParams)
  14. ctx = ctx.WithUnboxCounted(unbox_count)
  15. if len(type_args) == type_arity {
  16. var f_raw_type = AnonymousType { f.DeclaredType }
  17. var f_type = FillTypeArgs(f_raw_type, type_args)
  18. var f_type_repr = f_type.(AnonymousType).Repr.(Func)
  19. var input_type = f_type_repr.Input
  20. var output_type = f_type_repr.Output
  21. var arg_typed, err = AssignTo(input_type, arg, ctx)
  22. if err != nil { return Expr{}, err }
  23. return Expr {
  24. Type: output_type,
  25. Value: Call {
  26. Function: Expr {
  27. Type: f_type,
  28. Value: RefFunction {
  29. Name: name,
  30. Index: index,
  31. },
  32. Info: f_info,
  33. },
  34. Argument: arg_typed,
  35. },
  36. Info: call_info,
  37. }, nil
  38. } else if len(type_args) == 0 {
  39. var inf_ctx = ctx.WithTypeArgsInferringEnabled(f.TypeParams)
  40. var raw_input_type = f.DeclaredType.Input
  41. var raw_output_type = f.DeclaredType.Output
  42. var marked_input_type = MarkParamsAsBeingInferred(raw_input_type)
  43. var arg_typed, err = AssignTo(marked_input_type, arg, inf_ctx)
  44. if err != nil { return Expr{}, err }
  45. if len(inf_ctx.Inferred) != type_arity {
  46. return Expr{}, &ExprError {
  47. Point: f_info.ErrorPoint,
  48. Concrete: E_ExplicitTypeParamsRequired {},
  49. }
  50. }
  51. var inferred_args = make([]Type, type_arity)
  52. for i, t := range inf_ctx.Inferred {
  53. inferred_args[i] = t
  54. }
  55. var input_type = FillTypeArgs(raw_input_type, inferred_args)
  56. if !(AreTypesEqualInSameCtx(input_type, arg_typed.Type)) {
  57. panic("something went wrong")
  58. }
  59. var output_type = FillTypeArgs(raw_output_type, inferred_args)
  60. var f_type = AnonymousType { Func {
  61. Input: input_type,
  62. Output: output_type,
  63. } }
  64. return Expr {
  65. Type: output_type,
  66. Value: Call {
  67. Function: Expr {
  68. Type: f_type,
  69. Value: RefFunction {
  70. Name: name,
  71. Index: index,
  72. },
  73. Info: f_info,
  74. },
  75. Argument: arg_typed,
  76. },
  77. Info: call_info,
  78. }, nil
  79. } else {
  80. return Expr{}, &ExprError {
  81. Point: f_info.ErrorPoint,
  82. Concrete: E_FunctionWrongTypeParamsQuantity {
  83. FuncName: name,
  84. Given: uint(len(type_args)),
  85. Required: uint(type_arity),
  86. },
  87. }
  88. }
  89. }
  90. func GenericFunctionAssignTo (
  91. expected Type,
  92. name string,
  93. index uint,
  94. f *GenericFunction,
  95. type_args []Type,
  96. info ExprInfo,
  97. ctx ExprContext,
  98. ) (Expr, *ExprError) {
  99. var type_arity = len(f.TypeParams)
  100. if len(type_args) == type_arity {
  101. var f_raw_type = AnonymousType { f.DeclaredType }
  102. var f_type = FillTypeArgs(f_raw_type, type_args)
  103. var f_expr = Expr {
  104. Type: f_type,
  105. Value: RefFunction {
  106. Name: name,
  107. Index: index,
  108. },
  109. Info: info,
  110. }
  111. return AssignTypedTo(expected, f_expr, ctx, true)
  112. } else if len(type_args) == 0 {
  113. if expected == nil {
  114. return Expr{}, &ExprError {
  115. Point: info.ErrorPoint,
  116. Concrete: E_ExplicitTypeRequired {},
  117. }
  118. }
  119. var f_raw_type = AnonymousType { f.DeclaredType }
  120. // Note: Unbox/Union related inferring is not required
  121. // since function types are anonymous types and invariant.
  122. // Just apply NaivelyInferTypeArgs() here.
  123. var inferred = make(map[uint]Type)
  124. NaivelyInferTypeArgs(f_raw_type, expected, inferred)
  125. if len(inferred) == type_arity {
  126. var args = make([]Type, type_arity)
  127. for i, t := range inferred {
  128. args[i] = t
  129. }
  130. var f_type = FillTypeArgs(f_raw_type, args)
  131. if !(AreTypesEqualInSameCtx(f_type, expected)) {
  132. panic("something went wrong")
  133. }
  134. return Expr {
  135. Type: f_type,
  136. Value: RefFunction {
  137. Name: name,
  138. Index: index,
  139. },
  140. Info: info,
  141. }, nil
  142. } else {
  143. return Expr{}, &ExprError {
  144. Point: info.ErrorPoint,
  145. Concrete: E_ExplicitTypeParamsRequired {},
  146. }
  147. }
  148. } else {
  149. return Expr{}, &ExprError {
  150. Point: info.ErrorPoint,
  151. Concrete: E_FunctionWrongTypeParamsQuantity {
  152. FuncName: name,
  153. Given: uint(len(type_args)),
  154. Required: uint(type_arity),
  155. },
  156. }
  157. }
  158. }
  159. func FillTypeArgs(t Type, given []Type) Type {
  160. switch T := t.(type) {
  161. case ParameterType:
  162. return given[T.Index]
  163. case NamedType:
  164. var filled = make([]Type, len(T.Args))
  165. for i, arg := range T.Args {
  166. filled[i] = FillTypeArgs(arg, given)
  167. }
  168. return NamedType {
  169. Name: T.Name,
  170. Args: filled,
  171. }
  172. case AnonymousType:
  173. switch r := T.Repr.(type) {
  174. case Unit:
  175. return AnonymousType { Unit {} }
  176. case Tuple:
  177. var filled = make([]Type, len(r.Elements))
  178. for i, element := range r.Elements {
  179. filled[i] = FillTypeArgs(element, given)
  180. }
  181. return AnonymousType {
  182. Repr: Tuple {
  183. Elements: filled,
  184. },
  185. }
  186. case Bundle:
  187. var filled = make(map[string]Field, len(r.Fields))
  188. for name, field := range r.Fields {
  189. filled[name] = Field {
  190. Type: FillTypeArgs(field.Type, given),
  191. Index: field.Index,
  192. }
  193. }
  194. return AnonymousType {
  195. Repr: Bundle {
  196. Fields: filled,
  197. },
  198. }
  199. case Func:
  200. return AnonymousType {
  201. Repr:Func {
  202. Input: FillTypeArgs(r.Input, given),
  203. Output: FillTypeArgs(r.Output, given),
  204. },
  205. }
  206. default:
  207. panic("impossible branch")
  208. }
  209. default:
  210. panic("impossible branch")
  211. }
  212. }
  213. func NaivelyInferTypeArgs(template Type, given Type, inferred map[uint]Type) {
  214. switch T := template.(type) {
  215. case ParameterType:
  216. var existing, exists = inferred[T.Index]
  217. if !exists || AreTypesEqualInSameCtx(existing, given) {
  218. inferred[T.Index] = given
  219. }
  220. case NamedType:
  221. switch G := given.(type) {
  222. case NamedType:
  223. var L1 = len(T.Args)
  224. var L2 = len(G.Args)
  225. if L1 != L2 { panic("type registration went wrong") }
  226. var L = L1
  227. for i := 0; i < L; i += 1 {
  228. NaivelyInferTypeArgs(T.Args[i], G.Args[i], inferred)
  229. }
  230. }
  231. case AnonymousType:
  232. switch G := given.(type) {
  233. case AnonymousType:
  234. switch Tr := T.Repr.(type) {
  235. case Tuple:
  236. switch Gr := G.Repr.(type) {
  237. case Tuple:
  238. var L1 = len(Tr.Elements)
  239. var L2 = len(Gr.Elements)
  240. if L1 == L2 {
  241. var L = L1
  242. for i := 0; i < L; i += 1 {
  243. NaivelyInferTypeArgs(Tr.Elements[i], Gr.Elements[i], inferred)
  244. }
  245. }
  246. }
  247. case Bundle:
  248. switch Gr := G.Repr.(type) {
  249. case Bundle:
  250. for name, Tf := range Tr.Fields {
  251. var Gf, exists = Gr.Fields[name]
  252. if exists {
  253. NaivelyInferTypeArgs(Tf.Type, Gf.Type, inferred)
  254. }
  255. }
  256. }
  257. case Func:
  258. switch Gr := G.Repr.(type) {
  259. case Func:
  260. NaivelyInferTypeArgs(Tr.Input, Gr.Input, inferred)
  261. NaivelyInferTypeArgs(Tr.Output, Gr.Output, inferred)
  262. }
  263. default:
  264. panic("impossible branch")
  265. }
  266. }
  267. default:
  268. panic("impossible branch")
  269. }
  270. }
  271. func MarkParamsAsBeingInferred(type_ Type) Type {
  272. switch t := type_.(type) {
  273. case ParameterType:
  274. return ParameterType {
  275. Index: t.Index,
  276. BeingInferred: true,
  277. }
  278. case NamedType:
  279. var marked_args = make([]Type, len(t.Args))
  280. for i, arg := range t.Args {
  281. marked_args[i] = MarkParamsAsBeingInferred(arg)
  282. }
  283. return NamedType {
  284. Name: t.Name,
  285. Args: marked_args,
  286. }
  287. case AnonymousType:
  288. switch r := t.Repr.(type) {
  289. case Unit:
  290. return AnonymousType { Unit{} }
  291. case Tuple:
  292. var marked_elements = make([]Type, len(r.Elements))
  293. for i, el := range r.Elements {
  294. marked_elements[i] = MarkParamsAsBeingInferred(el)
  295. }
  296. return AnonymousType { Tuple { marked_elements } }
  297. case Bundle:
  298. var marked_fields = make(map[string]Field)
  299. for name, f := range r.Fields {
  300. marked_fields[name] = Field {
  301. Type: MarkParamsAsBeingInferred(f.Type),
  302. Index: f.Index,
  303. }
  304. }
  305. return AnonymousType { Bundle { marked_fields } }
  306. case Func:
  307. var marked_input = MarkParamsAsBeingInferred(r.Input)
  308. var marked_output = MarkParamsAsBeingInferred(r.Output)
  309. return AnonymousType { Func {
  310. Input: marked_input,
  311. Output: marked_output,
  312. } }
  313. default:
  314. panic("impossible branch")
  315. }
  316. default:
  317. panic("impossible branch")
  318. }
  319. }
  320. func FillMarkedParams(type_ Type, ctx ExprContext) Type {
  321. if !(ctx.InferTypeArgs) { panic("something went wrong") }
  322. switch T := type_.(type) {
  323. case ParameterType:
  324. if T.BeingInferred {
  325. var inferred, exists = ctx.Inferred[T.Index]
  326. if !exists { panic("something went wrong") }
  327. return inferred
  328. } else {
  329. return T
  330. }
  331. case NamedType:
  332. var filled = make([]Type, len(T.Args))
  333. for i, arg := range T.Args {
  334. filled[i] = FillMarkedParams(arg, ctx)
  335. }
  336. return NamedType {
  337. Name: T.Name,
  338. Args: filled,
  339. }
  340. case AnonymousType:
  341. switch r := T.Repr.(type) {
  342. case Unit:
  343. return AnonymousType { Unit {} }
  344. case Tuple:
  345. var filled = make([]Type, len(r.Elements))
  346. for i, element := range r.Elements {
  347. filled[i] = FillMarkedParams(element, ctx)
  348. }
  349. return AnonymousType {
  350. Repr: Tuple {
  351. Elements: filled,
  352. },
  353. }
  354. case Bundle:
  355. var filled = make(map[string]Field, len(r.Fields))
  356. for name, field := range r.Fields {
  357. filled[name] = Field {
  358. Type: FillMarkedParams(field.Type, ctx),
  359. Index: field.Index,
  360. }
  361. }
  362. return AnonymousType {
  363. Repr: Bundle {
  364. Fields: filled,
  365. },
  366. }
  367. case Func:
  368. return AnonymousType {
  369. Repr:Func {
  370. Input: FillMarkedParams(r.Input, ctx),
  371. Output: FillMarkedParams(r.Output, ctx),
  372. },
  373. }
  374. default:
  375. panic("impossible branch")
  376. }
  377. default:
  378. panic("impossible branch")
  379. }
  380. }