| 1 | // Copyright 2018 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package checker |
| 16 | |
| 17 | import ( |
| 18 | "github.com/google/cel-go/common/types" |
| 19 | ) |
| 20 | |
| 21 | // isDyn returns true if the input t is either type DYN or a well-known ANY message. |
| 22 | func isDyn(t *types.Type) bool { |
| 23 | // Note: object type values that are well-known and map to a DYN value in practice |
| 24 | // are sanitized prior to being added to the environment. |
| 25 | switch t.Kind() { |
| 26 | case types.DynKind, types.AnyKind: |
| 27 | return true |
| 28 | default: |
| 29 | return false |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | // isDynOrError returns true if the input is either an Error, DYN, or well-known ANY message. |
| 34 | func isDynOrError(t *types.Type) bool { |
| 35 | return isError(t) || isDyn(t) |
| 36 | } |
| 37 | |
| 38 | func isError(t *types.Type) bool { |
| 39 | return t.Kind() == types.ErrorKind |
| 40 | } |
| 41 | |
| 42 | func isOptional(t *types.Type) bool { |
| 43 | if t.Kind() == types.OpaqueKind { |
| 44 | return t.TypeName() == "optional_type" |
| 45 | } |
| 46 | return false |
| 47 | } |
| 48 | |
| 49 | func maybeUnwrapOptional(t *types.Type) (*types.Type, bool) { |
| 50 | if isOptional(t) { |
| 51 | return t.Parameters()[0], true |
| 52 | } |
| 53 | return t, false |
| 54 | } |
| 55 | |
| 56 | // isEqualOrLessSpecific checks whether one type is equal or less specific than the other one. |
| 57 | // A type is less specific if it matches the other type using the DYN type. |
| 58 | func isEqualOrLessSpecific(t1, t2 *types.Type) bool { |
| 59 | kind1, kind2 := t1.Kind(), t2.Kind() |
| 60 | // The first type is less specific. |
| 61 | if isDyn(t1) || kind1 == types.TypeParamKind { |
| 62 | return true |
| 63 | } |
| 64 | // The first type is not less specific. |
| 65 | if isDyn(t2) || kind2 == types.TypeParamKind { |
| 66 | return false |
| 67 | } |
| 68 | // Types must be of the same kind to be equal. |
| 69 | if kind1 != kind2 { |
| 70 | return false |
| 71 | } |
| 72 | |
| 73 | // With limited exceptions for ANY and JSON values, the types must agree and be equivalent in |
| 74 | // order to return true. |
| 75 | switch kind1 { |
| 76 | case types.OpaqueKind: |
| 77 | if t1.TypeName() != t2.TypeName() || |
| 78 | len(t1.Parameters()) != len(t2.Parameters()) { |
| 79 | return false |
| 80 | } |
| 81 | for i, p1 := range t1.Parameters() { |
| 82 | if !isEqualOrLessSpecific(p1, t2.Parameters()[i]) { |
| 83 | return false |
| 84 | } |
| 85 | } |
| 86 | return true |
| 87 | case types.ListKind: |
| 88 | return isEqualOrLessSpecific(t1.Parameters()[0], t2.Parameters()[0]) |
| 89 | case types.MapKind: |
| 90 | return isEqualOrLessSpecific(t1.Parameters()[0], t2.Parameters()[0]) && |
| 91 | isEqualOrLessSpecific(t1.Parameters()[1], t2.Parameters()[1]) |
| 92 | case types.TypeKind: |
| 93 | return true |
| 94 | default: |
| 95 | return t1.IsExactType(t2) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // / internalIsAssignable returns true if t1 is assignable to t2. |
| 100 | func internalIsAssignable(m *mapping, t1, t2 *types.Type) bool { |
| 101 | // Process type parameters. |
| 102 | kind1, kind2 := t1.Kind(), t2.Kind() |
| 103 | if kind2 == types.TypeParamKind { |
| 104 | // If t2 is a valid type substitution for t1, return true. |
| 105 | valid, t2HasSub := isValidTypeSubstitution(m, t1, t2) |
| 106 | if valid { |
| 107 | return true |
| 108 | } |
| 109 | // If t2 is not a valid type sub for t1, and already has a known substitution return false |
| 110 | // since it is not possible for t1 to be a substitution for t2. |
| 111 | if !valid && t2HasSub { |
| 112 | return false |
| 113 | } |
| 114 | // Otherwise, fall through to check whether t1 is a possible substitution for t2. |
| 115 | } |
| 116 | if kind1 == types.TypeParamKind { |
| 117 | // Return whether t1 is a valid substitution for t2. If not, do no additional checks as the |
| 118 | // possible type substitutions have been searched in both directions. |
| 119 | valid, _ := isValidTypeSubstitution(m, t2, t1) |
| 120 | return valid |
| 121 | } |
| 122 | |
| 123 | // Next check for wildcard types. |
| 124 | if isDynOrError(t1) || isDynOrError(t2) { |
| 125 | return true |
| 126 | } |
| 127 | // Preserve the nullness checks of the legacy type-checker. |
| 128 | if kind1 == types.NullTypeKind { |
| 129 | return internalIsAssignableNull(t2) |
| 130 | } |
| 131 | if kind2 == types.NullTypeKind { |
| 132 | return internalIsAssignableNull(t1) |
| 133 | } |
| 134 | |
| 135 | // Test for when the types do not need to agree, but are more specific than dyn. |
| 136 | switch kind1 { |
| 137 | case types.BoolKind, types.BytesKind, types.DoubleKind, types.IntKind, types.StringKind, types.UintKind, |
| 138 | types.AnyKind, types.DurationKind, types.TimestampKind, |
| 139 | types.StructKind: |
| 140 | // Test whether t2 is assignable from t1. The order of this check won't usually matter; |
| 141 | // however, there may be cases where type capabilities are expanded beyond what is supported |
| 142 | // in the current common/types package. For example, an interface designation for a group of |
| 143 | // Struct types. |
| 144 | return t2.IsAssignableType(t1) |
| 145 | case types.TypeKind: |
| 146 | return kind2 == types.TypeKind |
| 147 | case types.OpaqueKind, types.ListKind, types.MapKind: |
| 148 | return t1.Kind() == t2.Kind() && t1.TypeName() == t2.TypeName() && |
| 149 | internalIsAssignableList(m, t1.Parameters(), t2.Parameters()) |
| 150 | default: |
| 151 | return false |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | // isValidTypeSubstitution returns whether t2 (or its type substitution) is a valid type |
| 156 | // substitution for t1, and whether t2 has a type substitution in mapping m. |
| 157 | // |
| 158 | // The type t2 is a valid substitution for t1 if any of the following statements is true |
| 159 | // - t2 has a type substitution (t2sub) equal to t1 |
| 160 | // - t2 has a type substitution (t2sub) assignable to t1 |
| 161 | // - t2 does not occur within t1. |
| 162 | func isValidTypeSubstitution(m *mapping, t1, t2 *types.Type) (valid, hasSub bool) { |
| 163 | // Early return if the t1 and t2 are the same instance. |
| 164 | kind1, kind2 := t1.Kind(), t2.Kind() |
| 165 | if kind1 == kind2 && t1.IsExactType(t2) { |
| 166 | return true, true |
| 167 | } |
| 168 | if t2Sub, found := m.find(t2); found { |
| 169 | // Early return if t1 and t2Sub are the same instance as otherwise the mapping |
| 170 | // might mark a type as being a subtitution for itself. |
| 171 | if kind1 == t2Sub.Kind() && t1.IsExactType(t2Sub) { |
| 172 | return true, true |
| 173 | } |
| 174 | // If the types are compatible, pick the more general type and return true |
| 175 | if internalIsAssignable(m, t1, t2Sub) { |
| 176 | t2New := mostGeneral(t1, t2Sub) |
| 177 | // only update the type reference map if the target type does not occur within it. |
| 178 | if notReferencedIn(m, t2, t2New) { |
| 179 | m.add(t2, t2New) |
| 180 | } |
| 181 | // acknowledge the type agreement, and that the substitution is already tracked. |
| 182 | return true, true |
| 183 | } |
| 184 | return false, true |
| 185 | } |
| 186 | if notReferencedIn(m, t2, t1) { |
| 187 | m.add(t2, t1) |
| 188 | return true, false |
| 189 | } |
| 190 | return false, false |
| 191 | } |
| 192 | |
| 193 | // internalIsAssignableList returns true if the element types at each index in the list are |
| 194 | // assignable from l1[i] to l2[i]. The list lengths must also agree for the lists to be |
| 195 | // assignable. |
| 196 | func internalIsAssignableList(m *mapping, l1, l2 []*types.Type) bool { |
| 197 | if len(l1) != len(l2) { |
| 198 | return false |
| 199 | } |
| 200 | for i, t1 := range l1 { |
| 201 | if !internalIsAssignable(m, t1, l2[i]) { |
| 202 | return false |
| 203 | } |
| 204 | } |
| 205 | return true |
| 206 | } |
| 207 | |
| 208 | // internalIsAssignableNull returns true if the type is nullable. |
| 209 | func internalIsAssignableNull(t *types.Type) bool { |
| 210 | return isLegacyNullable(t) || t.IsAssignableType(types.NullType) |
| 211 | } |
| 212 | |
| 213 | // isLegacyNullable preserves the null-ness compatibility of the original type-checker implementation. |
| 214 | func isLegacyNullable(t *types.Type) bool { |
| 215 | switch t.Kind() { |
| 216 | case types.OpaqueKind, types.StructKind, types.AnyKind, types.DurationKind, types.TimestampKind: |
| 217 | return true |
| 218 | } |
| 219 | return false |
| 220 | } |
| 221 | |
| 222 | // isAssignable returns an updated type substitution mapping if t1 is assignable to t2. |
| 223 | func isAssignable(m *mapping, t1, t2 *types.Type) *mapping { |
| 224 | mCopy := m.copy() |
| 225 | if internalIsAssignable(mCopy, t1, t2) { |
| 226 | return mCopy |
| 227 | } |
| 228 | return nil |
| 229 | } |
| 230 | |
| 231 | // isAssignableList returns an updated type substitution mapping if l1 is assignable to l2. |
| 232 | func isAssignableList(m *mapping, l1, l2 []*types.Type) *mapping { |
| 233 | mCopy := m.copy() |
| 234 | if internalIsAssignableList(mCopy, l1, l2) { |
| 235 | return mCopy |
| 236 | } |
| 237 | return nil |
| 238 | } |
| 239 | |
| 240 | // mostGeneral returns the more general of two types which are known to unify. |
| 241 | func mostGeneral(t1, t2 *types.Type) *types.Type { |
| 242 | if isEqualOrLessSpecific(t1, t2) { |
| 243 | return t1 |
| 244 | } |
| 245 | return t2 |
| 246 | } |
| 247 | |
| 248 | // notReferencedIn checks whether the type doesn't appear directly or transitively within the other |
| 249 | // type. This is a standard requirement for type unification, commonly referred to as the "occurs |
| 250 | // check". |
| 251 | func notReferencedIn(m *mapping, t, withinType *types.Type) bool { |
| 252 | if t.IsExactType(withinType) { |
| 253 | return false |
| 254 | } |
| 255 | withinKind := withinType.Kind() |
| 256 | switch withinKind { |
| 257 | case types.TypeParamKind: |
| 258 | wtSub, found := m.find(withinType) |
| 259 | if !found { |
| 260 | return true |
| 261 | } |
| 262 | return notReferencedIn(m, t, wtSub) |
| 263 | case types.OpaqueKind, types.ListKind, types.MapKind, types.TypeKind: |
| 264 | for _, pt := range withinType.Parameters() { |
| 265 | if !notReferencedIn(m, t, pt) { |
| 266 | return false |
| 267 | } |
| 268 | } |
| 269 | return true |
| 270 | default: |
| 271 | return true |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | // substitute replaces all direct and indirect occurrences of bound type parameters. Unbound type |
| 276 | // parameters are replaced by DYN if typeParamToDyn is true. |
| 277 | func substitute(m *mapping, t *types.Type, typeParamToDyn bool) *types.Type { |
| 278 | if tSub, found := m.find(t); found { |
| 279 | return substitute(m, tSub, typeParamToDyn) |
| 280 | } |
| 281 | kind := t.Kind() |
| 282 | if typeParamToDyn && kind == types.TypeParamKind { |
| 283 | return types.DynType |
| 284 | } |
| 285 | switch kind { |
| 286 | case types.OpaqueKind: |
| 287 | return types.NewOpaqueType(t.TypeName(), substituteParams(m, t.Parameters(), typeParamToDyn)...) |
| 288 | case types.ListKind: |
| 289 | return types.NewListType(substitute(m, t.Parameters()[0], typeParamToDyn)) |
| 290 | case types.MapKind: |
| 291 | return types.NewMapType(substitute(m, t.Parameters()[0], typeParamToDyn), |
| 292 | substitute(m, t.Parameters()[1], typeParamToDyn)) |
| 293 | case types.TypeKind: |
| 294 | if len(t.Parameters()) > 0 { |
| 295 | tParam := t.Parameters()[0] |
| 296 | return types.NewTypeTypeWithParam(substitute(m, tParam, typeParamToDyn)) |
| 297 | } |
| 298 | return t |
| 299 | default: |
| 300 | return t |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | func substituteParams(m *mapping, typeParams []*types.Type, typeParamToDyn bool) []*types.Type { |
| 305 | subParams := make([]*types.Type, len(typeParams)) |
| 306 | for i, tp := range typeParams { |
| 307 | subParams[i] = substitute(m, tp, typeParamToDyn) |
| 308 | } |
| 309 | return subParams |
| 310 | } |
| 311 | |
| 312 | func newFunctionType(resultType *types.Type, argTypes ...*types.Type) *types.Type { |
| 313 | return types.NewOpaqueType("function", append([]*types.Type{resultType}, argTypes...)...) |
| 314 | } |