Skip to content

Commit 6486715

Browse files
committed
sync code from IntelliJ-EmmyLua
1 parent 31b21cd commit 6486715

File tree

4 files changed

+356
-0
lines changed

4 files changed

+356
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (c) 2017. tangzx([email protected])
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.tang.intellij.lua.ty
18+
19+
import com.intellij.psi.stubs.StubInputStream
20+
import com.intellij.psi.stubs.StubOutputStream
21+
import com.tang.intellij.lua.search.SearchContext
22+
23+
interface ITyArray : ITy {
24+
val base: ITy
25+
}
26+
27+
class TyArray(override val base: ITy) : Ty(TyKind.Array), ITyArray {
28+
29+
override fun equals(other: Any?): Boolean {
30+
return other is ITyArray && base == other.base
31+
}
32+
33+
override fun hashCode(): Int {
34+
return displayName.hashCode()
35+
}
36+
37+
override fun subTypeOf(other: ITy, context: SearchContext, strict: Boolean): Boolean {
38+
return super.subTypeOf(other, context, strict) || (other is TyArray && base.subTypeOf(other.base, context, strict)) || other == TABLE
39+
}
40+
41+
override fun substitute(substitutor: ITySubstitutor): ITy {
42+
return TyArray(base.substitute(substitutor))
43+
}
44+
45+
override fun accept(visitor: ITyVisitor) {
46+
visitor.visitArray(this)
47+
}
48+
49+
override fun acceptChildren(visitor: ITyVisitor) {
50+
base.accept(visitor)
51+
}
52+
}
53+
54+
object TyArraySerializer : TySerializer<ITyArray>() {
55+
override fun serializeTy(ty: ITyArray, stream: StubOutputStream) {
56+
Ty.serialize(ty.base, stream)
57+
}
58+
59+
override fun deserializeTy(flags: Int, stream: StubInputStream): ITyArray {
60+
val base = Ty.deserialize(stream)
61+
return TyArray(base)
62+
}
63+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) 2017. tangzx([email protected])
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.tang.intellij.lua.ty
18+
19+
import com.intellij.psi.stubs.StubInputStream
20+
import com.intellij.psi.stubs.StubOutputStream
21+
22+
interface ITySerializer {
23+
fun serialize(ty: ITy, stream: StubOutputStream)
24+
fun deserialize(flags: Int, stream: StubInputStream): ITy
25+
}
26+
27+
abstract class TySerializer<T : ITy> : ITySerializer {
28+
override fun serialize(ty: ITy, stream: StubOutputStream) {
29+
@Suppress("UNCHECKED_CAST")
30+
val t = ty as T
31+
serializeTy(t, stream)
32+
}
33+
34+
override fun deserialize(flags: Int, stream: StubInputStream): ITy {
35+
return deserializeTy(flags, stream)
36+
}
37+
38+
abstract fun deserializeTy(flags: Int, stream: StubInputStream): T
39+
40+
protected abstract fun serializeTy(ty: T, stream: StubOutputStream)
41+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright (c) 2017. tangzx([email protected])
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.tang.intellij.lua.ty
18+
19+
import com.intellij.psi.stubs.StubInputStream
20+
import com.intellij.psi.stubs.StubOutputStream
21+
import com.tang.intellij.lua.search.SearchContext
22+
23+
class TyTuple(val list: List<ITy>) : Ty(TyKind.Tuple) {
24+
25+
val size: Int get() {
26+
return list.size
27+
}
28+
29+
override fun substitute(substitutor: ITySubstitutor): ITy {
30+
val list = list.map { it.substitute(substitutor) }
31+
return TyTuple(list)
32+
}
33+
34+
override fun accept(visitor: ITyVisitor) {
35+
visitor.visitTuple(this)
36+
}
37+
38+
override fun acceptChildren(visitor: ITyVisitor) {
39+
list.forEach { it.accept(visitor) }
40+
}
41+
42+
override fun subTypeOf(other: ITy, context: SearchContext, strict: Boolean): Boolean {
43+
if (other is TyTuple && other.size == size) {
44+
for (i in 0 until size) {
45+
if (!list[i].subTypeOf(other.list[i], context, strict)) {
46+
return false
47+
}
48+
}
49+
return true
50+
}
51+
return false
52+
}
53+
54+
override fun hashCode(): Int {
55+
var hash = 0
56+
for (ty in list) {
57+
hash = hash * 31 + ty.hashCode()
58+
}
59+
return hash
60+
}
61+
62+
override fun equals(other: Any?): Boolean {
63+
if (other is TyTuple && other.size == size) {
64+
for (i in 0 until size) {
65+
if (list[i] != other.list[i]) {
66+
return false
67+
}
68+
}
69+
return true
70+
}
71+
return super.equals(other)
72+
}
73+
}
74+
75+
object TyTupleSerializer : TySerializer<TyTuple>() {
76+
override fun deserializeTy(flags: Int, stream: StubInputStream): TyTuple {
77+
val size = stream.readByte().toInt()
78+
val list = mutableListOf<ITy>()
79+
for (i in 0 until size) list.add(Ty.deserialize(stream))
80+
return TyTuple(list)
81+
}
82+
83+
override fun serializeTy(ty: TyTuple, stream: StubOutputStream) {
84+
stream.writeByte(ty.list.size)
85+
ty.list.forEach { Ty.serialize(it, stream) }
86+
}
87+
}
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/*
2+
* Copyright (c) 2017. tangzx([email protected])
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.tang.intellij.lua.ty
18+
19+
import com.intellij.psi.stubs.StubInputStream
20+
import com.intellij.psi.stubs.StubOutputStream
21+
import com.tang.intellij.lua.search.SearchContext
22+
23+
class TyUnion : Ty(TyKind.Union) {
24+
private val childSet = mutableSetOf<ITy>()
25+
26+
fun getChildTypes() = childSet
27+
28+
val size:Int
29+
get() = childSet.size
30+
31+
fun append(ty: ITy): TyUnion {
32+
if (ty is TyUnion) {
33+
ty.childSet.forEach { addChild(it) }
34+
}
35+
else addChild(ty)
36+
return this
37+
}
38+
39+
private fun addChild(ty: ITy): Boolean {
40+
return childSet.add(ty)
41+
}
42+
43+
override fun subTypeOf(other: ITy, context: SearchContext, strict: Boolean): Boolean {
44+
return super.subTypeOf(other, context, strict) || childSet.any { type -> type.subTypeOf(other, context, strict) }
45+
}
46+
47+
override fun substitute(substitutor: ITySubstitutor): ITy {
48+
val u = TyUnion()
49+
childSet.forEach { u.append(it.substitute(substitutor)) }
50+
return u
51+
}
52+
53+
override fun accept(visitor: ITyVisitor) {
54+
visitor.visitUnion(this)
55+
}
56+
57+
override fun acceptChildren(visitor: ITyVisitor) {
58+
childSet.forEach { it.accept(visitor) }
59+
}
60+
61+
override fun equals(other: Any?): Boolean {
62+
return other is TyUnion && other.hashCode() == hashCode()
63+
}
64+
65+
override fun hashCode(): Int {
66+
var code = 0
67+
childSet.forEach { code = code * 31 + it.hashCode() }
68+
return code
69+
}
70+
71+
companion object {
72+
fun <T : ITy> find(ty: ITy, clazz: Class<T>): T? {
73+
if (clazz.isInstance(ty))
74+
return clazz.cast(ty)
75+
var ret: T? = null
76+
process(ty) {
77+
if (clazz.isInstance(it)) {
78+
ret = clazz.cast(it)
79+
return@process false
80+
}
81+
true
82+
}
83+
return ret
84+
}
85+
86+
fun process(ty: ITy, process: (ITy) -> Boolean) {
87+
if (ty is TyUnion) {
88+
// why nullable ???
89+
val arr: Array<ITy?> = ty.childSet.toTypedArray()
90+
for (child in arr) {
91+
if (child != null && !process(child))
92+
break
93+
}
94+
} else process(ty)
95+
}
96+
97+
fun each(ty: ITy, fn: (ITy) -> Unit) {
98+
process(ty) {
99+
fn(it)
100+
true
101+
}
102+
}
103+
104+
// used by ver.2017
105+
@Suppress("unused")
106+
fun eachPerfect(ty: ITy, process: (ITy) -> Boolean) {
107+
if (ty is TyUnion) {
108+
val list = ty.childSet.sorted()
109+
for (iTy in list) {
110+
if (!process(iTy))
111+
break
112+
}
113+
} else process(ty)
114+
}
115+
116+
fun union(t1: ITy, t2: ITy): ITy {
117+
return when {
118+
isInvalid(t1) -> t2
119+
isInvalid(t2) -> t1
120+
t1 is TyUnion -> t1.append(t2)
121+
t2 is TyUnion -> t2.append(t1)
122+
else -> {
123+
val u = TyUnion()
124+
u.addChild(t1)
125+
u.addChild(t2)
126+
//if t1 == t2
127+
if (u.childSet.size == 1) t1 else u
128+
}
129+
}
130+
}
131+
132+
fun getPerfectClass(ty: ITy): ITyClass? {
133+
var clazz: ITyClass? = null
134+
var anonymous: ITyClass? = null
135+
var global: ITyClass? = null
136+
process(ty) {
137+
if (it is ITyClass) {
138+
when {
139+
it.isAnonymous -> anonymous = it
140+
it.isGlobal -> global = it
141+
else -> clazz = it
142+
}
143+
}
144+
clazz == null
145+
}
146+
return clazz ?: global ?: anonymous
147+
}
148+
}
149+
}
150+
151+
object TyUnionSerializer : TySerializer<TyUnion>() {
152+
override fun serializeTy(ty: TyUnion, stream: StubOutputStream) {
153+
stream.writeInt(ty.size)
154+
TyUnion.each(ty) { Ty.serialize(it, stream) }
155+
}
156+
157+
override fun deserializeTy(flags: Int, stream: StubInputStream): TyUnion {
158+
val union = TyUnion()
159+
val size = stream.readInt()
160+
for (i in 0 until size) {
161+
union.append(Ty.deserialize(stream))
162+
}
163+
return union
164+
}
165+
}

0 commit comments

Comments
 (0)