| 1 |
// Copyright 2020 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
//+build aix,gccgo |
| 6 |
|
| 7 |
// AIX linker isn't able to merge identical type descriptors coming from |
| 8 |
// different objects. Thus, two rtypes might have two different pointers |
| 9 |
// even if they are the same. Thus, instead of pointer equality, string |
| 10 |
// field is checked. |
| 11 |
|
| 12 |
package reflect |
| 13 |
|
| 14 |
import ( |
| 15 |
"sync" |
| 16 |
) |
| 17 |
|
| 18 |
// rtypeEqual returns true if both rtypes are identical. |
| 19 |
func rtypeEqual(t1, t2 *rtype) bool { |
| 20 |
switch { |
| 21 |
case t1 == t2: |
| 22 |
return true |
| 23 |
case t1 == nil || t2 == nil: |
| 24 |
return false |
| 25 |
case t1.kind != t2.kind || t1.hash != t2.hash: |
| 26 |
return false |
| 27 |
default: |
| 28 |
return t1.String() == t2.String() |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
// typeEqual returns true if both Types are identical. |
| 33 |
func typeEqual(t1, t2 Type) bool { |
| 34 |
return rtypeEqual(t1.common(), t2.common()) |
| 35 |
} |
| 36 |
|
| 37 |
// toType converts from a *rtype to a Type that can be returned |
| 38 |
// to the client of package reflect. The only concern is that |
| 39 |
// a nil *rtype must be replaced by a nil Type. |
| 40 |
// On AIX, as type duplications can occur, it also ensure that |
| 41 |
// multiple *rtype for the same type are coalesced into a single |
| 42 |
// Type. |
| 43 |
|
| 44 |
var canonicalType = make(map[string]Type) |
| 45 |
|
| 46 |
var canonicalTypeLock sync.RWMutex |
| 47 |
|
| 48 |
func canonicalize(t Type) Type { |
| 49 |
if t == nil { |
| 50 |
return nil |
| 51 |
} |
| 52 |
s := t.rawString() |
| 53 |
canonicalTypeLock.RLock() |
| 54 |
if r, ok := canonicalType[s]; ok { |
| 55 |
canonicalTypeLock.RUnlock() |
| 56 |
return r |
| 57 |
} |
| 58 |
canonicalTypeLock.RUnlock() |
| 59 |
canonicalTypeLock.Lock() |
| 60 |
if r, ok := canonicalType[s]; ok { |
| 61 |
canonicalTypeLock.Unlock() |
| 62 |
return r |
| 63 |
} |
| 64 |
canonicalType[s] = t |
| 65 |
canonicalTypeLock.Unlock() |
| 66 |
return t |
| 67 |
} |
| 68 |
|
| 69 |
func toType(p *rtype) Type { |
| 70 |
if p == nil { |
| 71 |
return nil |
| 72 |
} |
| 73 |
return canonicalize(p) |
| 74 |
} |