aboutsummaryrefslogtreecommitdiff
path: root/libgo/go/reflect/eqtype_aix_gccgo.go
blob: 7afbf10ad6db58dff6d876847572ffeb45a0cc0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//+build aix,gccgo

// AIX linker isn't able to merge identical type descriptors coming from
// different objects. Thus, two rtypes might have two different pointers
// even if they are the same. Thus, instead of pointer equality, string
// field is checked.

package reflect

import (
	"sync"
)

// rtypeEqual returns true if both rtypes are identical.
func rtypeEqual(t1, t2 *rtype) bool {
	switch {
	case t1 == t2:
		return true
	case t1 == nil || t2 == nil:
		return false
	case t1.kind != t2.kind || t1.hash != t2.hash:
		return false
	default:
		return t1.String() == t2.String()
	}
}

// typeEqual returns true if both Types are identical.
func typeEqual(t1, t2 Type) bool {
	return rtypeEqual(t1.common(), t2.common())
}

// toType converts from a *rtype to a Type that can be returned
// to the client of package reflect. The only concern is that
// a nil *rtype must be replaced by a nil Type.
// On AIX, as type duplications can occur, it also ensure that
// multiple *rtype for the same  type are coalesced into a single
// Type.

var canonicalType = make(map[string]Type)

var canonicalTypeLock sync.RWMutex

func canonicalize(t Type) Type {
	if t == nil {
		return nil
	}
	s := t.rawString()
	canonicalTypeLock.RLock()
	if r, ok := canonicalType[s]; ok {
		canonicalTypeLock.RUnlock()
		return r
	}
	canonicalTypeLock.RUnlock()
	canonicalTypeLock.Lock()
	if r, ok := canonicalType[s]; ok {
		canonicalTypeLock.Unlock()
		return r
	}
	canonicalType[s] = t
	canonicalTypeLock.Unlock()
	return t
}

func toType(p *rtype) Type {
	if p == nil {
		return nil
	}
	return canonicalize(p)
}