type.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. package checker
  2. import (
  3. "kumachan/loader"
  4. "kumachan/transformer/node"
  5. "strings"
  6. )
  7. type GenericType struct {
  8. Params [] string
  9. Value TypeVal
  10. Node node.Node
  11. UnionIndex uint
  12. }
  13. type TypeVal interface { TypeVal() }
  14. func (impl Union) TypeVal() {}
  15. type Union struct {
  16. SubTypes [] loader.Symbol
  17. }
  18. func (impl Boxed) TypeVal() {}
  19. type Boxed struct {
  20. InnerType Type
  21. Protected bool
  22. Opaque bool
  23. }
  24. func (impl Native) TypeVal() {}
  25. type Native struct {}
  26. type Type interface { CheckerType() }
  27. func (impl ParameterType) CheckerType() {}
  28. type ParameterType struct {
  29. Index uint
  30. BeingInferred bool
  31. }
  32. func (impl NamedType) CheckerType() {}
  33. type NamedType struct {
  34. Name loader.Symbol
  35. Args [] Type
  36. }
  37. func (impl AnonymousType) CheckerType() {}
  38. type AnonymousType struct {
  39. Repr TypeRepr
  40. }
  41. type TypeRepr interface { TypeRepr() }
  42. func (impl Unit) TypeRepr() {}
  43. type Unit struct {}
  44. func (impl Tuple) TypeRepr() {}
  45. type Tuple struct {
  46. Elements [] Type
  47. }
  48. func (impl Bundle) TypeRepr() {}
  49. type Bundle struct {
  50. Fields map[string] Field
  51. }
  52. type Field struct {
  53. Type Type
  54. Index uint
  55. }
  56. func (impl Func) TypeRepr() {}
  57. type Func struct {
  58. Input Type
  59. Output Type
  60. }
  61. func GetSubtypeIndex(u Union, sym loader.Symbol) (uint, bool) {
  62. for index, subtype := range u.SubTypes {
  63. if subtype == sym {
  64. return uint(index), true
  65. }
  66. }
  67. return BadIndex, false
  68. }
  69. type TypeDescContext struct {
  70. ParamNames [] string
  71. UseInferred bool
  72. InferredNames [] string
  73. InferredTypes map[uint] Type
  74. }
  75. func DescribeTypeWithParams(type_ Type, params []string) string {
  76. return DescribeType(type_, TypeDescContext {
  77. ParamNames: params,
  78. UseInferred: false,
  79. })
  80. }
  81. func DescribeType(type_ Type, ctx TypeDescContext) string {
  82. switch t := type_.(type) {
  83. case ParameterType:
  84. if ctx.UseInferred {
  85. var inferred_t, exists = ctx.InferredTypes[t.Index]
  86. if exists {
  87. return DescribeType(inferred_t, ctx)
  88. } else {
  89. return ctx.InferredNames[t.Index]
  90. }
  91. } else {
  92. return ctx.ParamNames[t.Index]
  93. }
  94. case NamedType:
  95. var buf strings.Builder
  96. if loader.IsPreloadCoreSymbol(t.Name) {
  97. buf.WriteString(t.Name.SymbolName)
  98. } else {
  99. buf.WriteString(t.Name.String())
  100. }
  101. if len(t.Args) > 0 {
  102. buf.WriteRune('[')
  103. for i, arg := range t.Args {
  104. buf.WriteString(DescribeType(arg, ctx))
  105. if i != len(t.Args)-1 {
  106. buf.WriteString(", ")
  107. }
  108. }
  109. buf.WriteRune(']')
  110. }
  111. return buf.String()
  112. case AnonymousType:
  113. switch r := t.Repr.(type) {
  114. case Unit:
  115. return "()"
  116. case Tuple:
  117. var buf strings.Builder
  118. buf.WriteRune('(')
  119. for i, el := range r.Elements {
  120. buf.WriteString(DescribeType(el, ctx))
  121. if i != len(r.Elements)-1 {
  122. buf.WriteString(", ")
  123. }
  124. }
  125. buf.WriteRune(')')
  126. return buf.String()
  127. case Bundle:
  128. var buf strings.Builder
  129. buf.WriteString("{ ")
  130. for name, field := range r.Fields {
  131. buf.WriteString(name)
  132. buf.WriteString(": ")
  133. buf.WriteString(DescribeType(field.Type, ctx))
  134. }
  135. buf.WriteString(" }")
  136. return buf.String()
  137. case Func:
  138. var buf strings.Builder
  139. buf.WriteString("lambda")
  140. buf.WriteString(DescribeType(r.Input, ctx))
  141. buf.WriteString(" ")
  142. buf.WriteString(DescribeType(r.Output, ctx))
  143. return buf.String()
  144. default:
  145. panic("impossible branch")
  146. }
  147. default:
  148. panic("impossible branch")
  149. }
  150. }
  151. func IsLocalType (type_ Type, mod string) bool {
  152. switch t := type_.(type) {
  153. case ParameterType:
  154. return false
  155. case NamedType:
  156. if t.Name.ModuleName == mod {
  157. return true
  158. } else {
  159. for _, arg := range t.Args {
  160. if IsLocalType(arg, mod) {
  161. return true
  162. }
  163. }
  164. return false
  165. }
  166. case AnonymousType:
  167. switch r := t.Repr.(type) {
  168. case Unit:
  169. return false
  170. case Tuple:
  171. for _, el := range r.Elements {
  172. if IsLocalType(el, mod) {
  173. return true
  174. }
  175. }
  176. return false
  177. case Bundle:
  178. for _, f := range r.Fields {
  179. if IsLocalType(f.Type, mod) {
  180. return true
  181. }
  182. }
  183. return false
  184. case Func:
  185. if IsLocalType(r.Input, mod) {
  186. return true
  187. }
  188. if IsLocalType(r.Output, mod) {
  189. return true
  190. }
  191. return false
  192. default:
  193. panic("impossible branch")
  194. }
  195. default:
  196. panic("impossible branch")
  197. }
  198. }
  199. func AreTypesOverloadUnsafe (type1 Type, type2 Type) bool {
  200. // Are type1 and type2 equal in the context of function overloading
  201. switch t1 := type1.(type) {
  202. case ParameterType:
  203. return true // rough comparison
  204. case NamedType:
  205. switch t2 := type2.(type) {
  206. case NamedType:
  207. if t1.Name == t2.Name {
  208. var L1 = len(t1.Args)
  209. var L2 = len(t2.Args)
  210. if L1 != L2 { panic("type registration went wrong") }
  211. var L = L1
  212. for i := 0; i < L; i += 1 {
  213. if !(AreTypesOverloadUnsafe(t1.Args[i], t2.Args[i])) {
  214. return false
  215. }
  216. }
  217. return true
  218. } else {
  219. return false
  220. }
  221. default:
  222. return false
  223. }
  224. case AnonymousType:
  225. switch t2 := type2.(type) {
  226. case AnonymousType:
  227. switch r1 := t1.Repr.(type) {
  228. case Unit:
  229. switch t2.Repr.(type) {
  230. case Unit:
  231. return true
  232. default:
  233. return false
  234. }
  235. case Tuple:
  236. switch r2 := t2.Repr.(type) {
  237. case Tuple:
  238. var L1 = len(r1.Elements)
  239. var L2 = len(r2.Elements)
  240. if L1 == L2 {
  241. var L = L1
  242. for i := 0; i < L; i += 1 {
  243. if !(AreTypesOverloadUnsafe(r1.Elements[i], r2.Elements[i])) {
  244. return false
  245. }
  246. }
  247. return true
  248. } else {
  249. return false
  250. }
  251. default:
  252. return false
  253. }
  254. case Bundle:
  255. switch r2 := t2.Repr.(type) {
  256. case Bundle:
  257. var L1 = len(r1.Fields)
  258. var L2 = len(r2.Fields)
  259. if L1 == L2 {
  260. for name, f1 := range r1.Fields {
  261. var f2, exists = r2.Fields[name]
  262. if !exists || !(AreTypesOverloadUnsafe(f1.Type, f2.Type)) {
  263. return false
  264. }
  265. }
  266. return true
  267. } else {
  268. return false
  269. }
  270. default:
  271. return false
  272. }
  273. case Func:
  274. switch r2 := t2.Repr.(type) {
  275. case Func:
  276. if !(AreTypesOverloadUnsafe(r1.Input, r2.Input)) {
  277. return false
  278. }
  279. if !(AreTypesOverloadUnsafe(r1.Output, r2.Output)) {
  280. return false
  281. }
  282. return true
  283. default:
  284. return true
  285. }
  286. default:
  287. panic("impossible branch")
  288. }
  289. default:
  290. return false
  291. }
  292. default:
  293. panic("impossible branch")
  294. }
  295. }
  296. func AreTypesEqualInSameCtx (type1 Type, type2 Type) bool {
  297. switch t1 := type1.(type) {
  298. case ParameterType:
  299. switch t2 := type2.(type) {
  300. case ParameterType:
  301. return t1.Index == t2.Index
  302. default:
  303. return false
  304. }
  305. case NamedType:
  306. switch t2 := type2.(type) {
  307. case NamedType:
  308. if t1.Name == t2.Name {
  309. var L1 = len(t1.Args)
  310. var L2 = len(t2.Args)
  311. if L1 != L2 { panic("type registration went wrong") }
  312. var L = L1
  313. for i := 0; i < L; i += 1 {
  314. if !(AreTypesEqualInSameCtx(t1.Args[i], t2.Args[i])) {
  315. return false
  316. }
  317. }
  318. return true
  319. } else {
  320. return false
  321. }
  322. default:
  323. return false
  324. }
  325. case AnonymousType:
  326. switch t2 := type2.(type) {
  327. case AnonymousType:
  328. switch r1 := t1.Repr.(type) {
  329. case Unit:
  330. switch t2.Repr.(type) {
  331. case Unit:
  332. return true
  333. default:
  334. return false
  335. }
  336. case Tuple:
  337. switch r2 := t2.Repr.(type) {
  338. case Tuple:
  339. var L1 = len(r1.Elements)
  340. var L2 = len(r2.Elements)
  341. if L1 == L2 {
  342. var L = L1
  343. for i := 0; i < L; i += 1 {
  344. if !(AreTypesEqualInSameCtx(r1.Elements[i], r2.Elements[i])) {
  345. return false
  346. }
  347. }
  348. return true
  349. } else {
  350. return false
  351. }
  352. default:
  353. return false
  354. }
  355. case Bundle:
  356. switch r2 := t2.Repr.(type) {
  357. case Bundle:
  358. var L1 = len(r1.Fields)
  359. var L2 = len(r2.Fields)
  360. if L1 == L2 {
  361. for name, f1 := range r1.Fields {
  362. var f2, exists = r2.Fields[name]
  363. if !exists || !(AreTypesEqualInSameCtx(f1.Type, f2.Type)) {
  364. return false
  365. }
  366. }
  367. return true
  368. } else {
  369. return false
  370. }
  371. default:
  372. return false
  373. }
  374. case Func:
  375. switch r2 := t2.Repr.(type) {
  376. case Func:
  377. if !(AreTypesEqualInSameCtx(r1.Input, r2.Input)) {
  378. return false
  379. }
  380. if !(AreTypesEqualInSameCtx(r1.Output, r2.Output)) {
  381. return false
  382. }
  383. return true
  384. default:
  385. return true
  386. }
  387. default:
  388. panic("impossible branch")
  389. }
  390. default:
  391. return false
  392. }
  393. default:
  394. panic("impossible branch")
  395. }
  396. }