Marcel van Lohuizen | cea55b2 | 2020-12-18 12:09:05 +0100 | [diff] [blame] | 1 | // Copyright 2020 CUE Authors |
| 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 adt |
| 16 | |
| 17 | // MatchAndInsert finds matching optional parts for a given Arc and adds its |
| 18 | // conjuncts. Bulk fields are only applied if no fields match, and additional |
| 19 | // constraints are only added if neither regular nor bulk fields match. |
| 20 | func (o *StructInfo) MatchAndInsert(c *OpContext, arc *Vertex) { |
| 21 | env := o.Env |
| 22 | |
Marcel van Lohuizen | 826bd7b | 2021-01-16 17:26:00 +0100 | [diff] [blame] | 23 | closeInfo := o.CloseInfo |
| 24 | closeInfo.IsClosed = false |
| 25 | |
Marcel van Lohuizen | cea55b2 | 2020-12-18 12:09:05 +0100 | [diff] [blame] | 26 | // Match normal fields |
| 27 | matched := false |
| 28 | outer: |
| 29 | for _, f := range o.Fields { |
| 30 | if f.Label == arc.Label { |
| 31 | for _, e := range f.Optional { |
Marcel van Lohuizen | 826bd7b | 2021-01-16 17:26:00 +0100 | [diff] [blame] | 32 | arc.AddConjunct(MakeConjunct(env, e, closeInfo)) |
Marcel van Lohuizen | cea55b2 | 2020-12-18 12:09:05 +0100 | [diff] [blame] | 33 | } |
| 34 | matched = true |
| 35 | break outer |
| 36 | } |
| 37 | } |
| 38 | |
Marcel van Lohuizen | 957003c | 2021-04-08 10:26:31 +0200 | [diff] [blame] | 39 | f := arc.Label |
| 40 | if !f.IsRegular() { |
Marcel van Lohuizen | cea55b2 | 2020-12-18 12:09:05 +0100 | [diff] [blame] | 41 | return |
| 42 | } |
Marcel van Lohuizen | 957003c | 2021-04-08 10:26:31 +0200 | [diff] [blame] | 43 | if int64(f.Index()) == MaxIndex { |
| 44 | f = 0 |
| 45 | } |
Marcel van Lohuizen | cea55b2 | 2020-12-18 12:09:05 +0100 | [diff] [blame] | 46 | |
Marcel van Lohuizen | 48d255a | 2021-02-04 16:56:14 +0100 | [diff] [blame] | 47 | var label Value |
Marcel van Lohuizen | 957003c | 2021-04-08 10:26:31 +0200 | [diff] [blame] | 48 | if o.types&HasComplexPattern != 0 && f.IsString() { |
| 49 | label = f.ToValue(c) |
Marcel van Lohuizen | 48d255a | 2021-02-04 16:56:14 +0100 | [diff] [blame] | 50 | } |
| 51 | |
Marcel van Lohuizen | 08e814f | 2021-01-19 13:43:59 +0100 | [diff] [blame] | 52 | if len(o.Bulk) > 0 { |
| 53 | bulkEnv := *env |
Marcel van Lohuizen | 957003c | 2021-04-08 10:26:31 +0200 | [diff] [blame] | 54 | bulkEnv.DynamicLabel = f |
Marcel van Lohuizen | 08e814f | 2021-01-19 13:43:59 +0100 | [diff] [blame] | 55 | bulkEnv.Deref = nil |
| 56 | bulkEnv.Cycles = nil |
Marcel van Lohuizen | cea55b2 | 2020-12-18 12:09:05 +0100 | [diff] [blame] | 57 | |
Marcel van Lohuizen | 08e814f | 2021-01-19 13:43:59 +0100 | [diff] [blame] | 58 | // match bulk optional fields / pattern properties |
| 59 | for _, b := range o.Bulk { |
| 60 | // if matched && f.additional { |
| 61 | // continue |
| 62 | // } |
Marcel van Lohuizen | 957003c | 2021-04-08 10:26:31 +0200 | [diff] [blame] | 63 | if matchBulk(c, env, b, f, label) { |
Marcel van Lohuizen | 08e814f | 2021-01-19 13:43:59 +0100 | [diff] [blame] | 64 | matched = true |
| 65 | info := closeInfo.SpawnSpan(b.Value, ConstraintSpan) |
| 66 | arc.AddConjunct(MakeConjunct(&bulkEnv, b, info)) |
| 67 | } |
Marcel van Lohuizen | cea55b2 | 2020-12-18 12:09:05 +0100 | [diff] [blame] | 68 | } |
| 69 | } |
Marcel van Lohuizen | 08e814f | 2021-01-19 13:43:59 +0100 | [diff] [blame] | 70 | |
| 71 | if matched || len(o.Additional) == 0 { |
Marcel van Lohuizen | cea55b2 | 2020-12-18 12:09:05 +0100 | [diff] [blame] | 72 | return |
| 73 | } |
| 74 | |
| 75 | addEnv := *env |
| 76 | addEnv.Deref = nil |
| 77 | addEnv.Cycles = nil |
| 78 | |
| 79 | // match others |
| 80 | for _, x := range o.Additional { |
Marcel van Lohuizen | 08e814f | 2021-01-19 13:43:59 +0100 | [diff] [blame] | 81 | info := closeInfo |
| 82 | if _, ok := x.(*Top); !ok { |
| 83 | info = info.SpawnSpan(x, ConstraintSpan) |
| 84 | } |
| 85 | arc.AddConjunct(MakeConjunct(&addEnv, x, info)) |
Marcel van Lohuizen | cea55b2 | 2020-12-18 12:09:05 +0100 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
Marcel van Lohuizen | 1f9300d | 2021-03-20 19:18:27 +0100 | [diff] [blame] | 89 | // matchBulk reports whether feature f matches the filter of x. It evaluation of |
| 90 | // the filter is erroneous, it returns false and the error will be set in c. |
Marcel van Lohuizen | 48d255a | 2021-02-04 16:56:14 +0100 | [diff] [blame] | 91 | func matchBulk(c *OpContext, env *Environment, x *BulkOptionalField, f Feature, label Value) bool { |
| 92 | v := env.evalCached(c, x.Filter) |
Marcel van Lohuizen | cea55b2 | 2020-12-18 12:09:05 +0100 | [diff] [blame] | 93 | |
Marcel van Lohuizen | 48d255a | 2021-02-04 16:56:14 +0100 | [diff] [blame] | 94 | // Fast-track certain cases. |
| 95 | switch x := v.(type) { |
Marcel van Lohuizen | 1f9300d | 2021-03-20 19:18:27 +0100 | [diff] [blame] | 96 | case *Bottom: |
| 97 | if c.errs == nil { |
| 98 | c.AddBottom(x) |
| 99 | } |
| 100 | return false |
Marcel van Lohuizen | cea55b2 | 2020-12-18 12:09:05 +0100 | [diff] [blame] | 101 | case *Top: |
Marcel van Lohuizen | 48d255a | 2021-02-04 16:56:14 +0100 | [diff] [blame] | 102 | return true |
Marcel van Lohuizen | cea55b2 | 2020-12-18 12:09:05 +0100 | [diff] [blame] | 103 | |
| 104 | case *BasicType: |
Marcel van Lohuizen | 48d255a | 2021-02-04 16:56:14 +0100 | [diff] [blame] | 105 | return x.K&StringKind != 0 |
Marcel van Lohuizen | cea55b2 | 2020-12-18 12:09:05 +0100 | [diff] [blame] | 106 | |
Marcel van Lohuizen | 48d255a | 2021-02-04 16:56:14 +0100 | [diff] [blame] | 107 | case *BoundValue: |
| 108 | switch x.Kind() { |
| 109 | case StringKind: |
| 110 | if label == nil { |
Marcel van Lohuizen | c505c19 | 2021-04-07 09:40:57 +0200 | [diff] [blame] | 111 | return false |
Marcel van Lohuizen | 48d255a | 2021-02-04 16:56:14 +0100 | [diff] [blame] | 112 | } |
| 113 | str := label.(*String).Str |
| 114 | return x.validateStr(c, str) |
| 115 | |
| 116 | case IntKind: |
| 117 | return x.validateInt(c, int64(f.Index())) |
| 118 | } |
Marcel van Lohuizen | cea55b2 | 2020-12-18 12:09:05 +0100 | [diff] [blame] | 119 | } |
Marcel van Lohuizen | cea55b2 | 2020-12-18 12:09:05 +0100 | [diff] [blame] | 120 | |
Marcel van Lohuizen | c505c19 | 2021-04-07 09:40:57 +0200 | [diff] [blame] | 121 | if label == nil { |
| 122 | return false |
| 123 | } |
| 124 | |
Marcel van Lohuizen | 48d255a | 2021-02-04 16:56:14 +0100 | [diff] [blame] | 125 | n := Vertex{} |
| 126 | m := MakeRootConjunct(env, v) |
| 127 | n.AddConjunct(m) |
Marcel van Lohuizen | 48d255a | 2021-02-04 16:56:14 +0100 | [diff] [blame] | 128 | n.AddConjunct(MakeRootConjunct(m.Env, label)) |
Marcel van Lohuizen | cea55b2 | 2020-12-18 12:09:05 +0100 | [diff] [blame] | 129 | |
Marcel van Lohuizen | 48d255a | 2021-02-04 16:56:14 +0100 | [diff] [blame] | 130 | c.inConstraint++ |
| 131 | n.Finalize(c) |
| 132 | c.inConstraint-- |
Marcel van Lohuizen | cea55b2 | 2020-12-18 12:09:05 +0100 | [diff] [blame] | 133 | |
Marcel van Lohuizen | 48d255a | 2021-02-04 16:56:14 +0100 | [diff] [blame] | 134 | b, _ := n.BaseValue.(*Bottom) |
Marcel van Lohuizen | cea55b2 | 2020-12-18 12:09:05 +0100 | [diff] [blame] | 135 | return b == nil |
| 136 | } |