|
| 1 | +// Copyright 2025 the libevm authors. |
| 2 | +// |
| 3 | +// The libevm additions to go-ethereum are free software: you can redistribute |
| 4 | +// them and/or modify them under the terms of the GNU Lesser General Public License |
| 5 | +// as published by the Free Software Foundation, either version 3 of the License, |
| 6 | +// or (at your option) any later version. |
| 7 | +// |
| 8 | +// The libevm additions are distributed in the hope that they will be useful, |
| 9 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser |
| 11 | +// General Public License for more details. |
| 12 | +// |
| 13 | +// You should have received a copy of the GNU Lesser General Public License |
| 14 | +// along with the go-ethereum library. If not, see |
| 15 | +// <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "go/types" |
| 22 | +) |
| 23 | + |
| 24 | +// makeNamedBasicOp is a convenience wrapper for basicOp. |
| 25 | +// It returns a basicOp with the named type as the main type instead of the underlying basic type. |
| 26 | +func (bctx *buildContext) makeNamedBasicOp(named *types.Named) (op, error) { |
| 27 | + underlying := named.Underlying() |
| 28 | + basic, ok := underlying.(*types.Basic) |
| 29 | + if !ok { |
| 30 | + return nil, fmt.Errorf("expected basic type, got %T", underlying) |
| 31 | + } |
| 32 | + |
| 33 | + // We use basic op because it actually supports necessary conversions (through writeNeedsConversion and decodeNeedsConversion) |
| 34 | + // for named types. |
| 35 | + // The only problem with that is it does not support the named type as the main type. |
| 36 | + // So we use the named type as the main type instead of the underlying basic type. |
| 37 | + baseOp, err := bctx.makeBasicOp(basic) |
| 38 | + if err != nil { |
| 39 | + return nil, err |
| 40 | + } |
| 41 | + |
| 42 | + op, ok := baseOp.(basicOp) |
| 43 | + if !ok { |
| 44 | + return nil, fmt.Errorf("expected basicOp, got %T", baseOp) |
| 45 | + } |
| 46 | + op.typ = named |
| 47 | + |
| 48 | + return op, nil |
| 49 | +} |
| 50 | + |
| 51 | +// hasBasicUnderlying checks whether `named` has an underlying basic type. |
| 52 | +func hasBasicUnderlying(named *types.Named) bool { |
| 53 | + _, ok := named.Underlying().(*types.Basic) |
| 54 | + return ok |
| 55 | +} |
0 commit comments