-
Notifications
You must be signed in to change notification settings - Fork 131
/
programindices.go
132 lines (108 loc) Β· 4.4 KB
/
programindices.go
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* Cadence - The resource-oriented smart contract programming language
*
* Copyright Dapper Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ast
import (
"sync"
)
// programIndices is a container for all indices of a program's declarations
type programIndices struct {
once sync.Once
// Use `pragmaDeclarations` instead
_pragmaDeclarations []*PragmaDeclaration
// Use `importDeclarations` instead
_importDeclarations []*ImportDeclaration
// Use `interfaceDeclarations` instead
_interfaceDeclarations []*InterfaceDeclaration
// Use `compositeDeclarations` instead
_compositeDeclarations []*CompositeDeclaration
// Use `attachmentDeclarations` instead
_attachmentDeclarations []*AttachmentDeclaration
// Use `functionDeclarations()` instead
_functionDeclarations []*FunctionDeclaration
// Use `transactionDeclarations()` instead
_transactionDeclarations []*TransactionDeclaration
// Use `variableDeclarations()` instead
_variableDeclarations []*VariableDeclaration
}
func (i *programIndices) pragmaDeclarations(declarations []Declaration) []*PragmaDeclaration {
i.once.Do(i.initializer(declarations))
return i._pragmaDeclarations
}
func (i *programIndices) importDeclarations(declarations []Declaration) []*ImportDeclaration {
i.once.Do(i.initializer(declarations))
return i._importDeclarations
}
func (i *programIndices) interfaceDeclarations(declarations []Declaration) []*InterfaceDeclaration {
i.once.Do(i.initializer(declarations))
return i._interfaceDeclarations
}
func (i *programIndices) compositeDeclarations(declarations []Declaration) []*CompositeDeclaration {
i.once.Do(i.initializer(declarations))
return i._compositeDeclarations
}
func (i *programIndices) attachmentDeclarations(declarations []Declaration) []*AttachmentDeclaration {
i.once.Do(i.initializer(declarations))
return i._attachmentDeclarations
}
func (i *programIndices) functionDeclarations(declarations []Declaration) []*FunctionDeclaration {
i.once.Do(i.initializer(declarations))
return i._functionDeclarations
}
func (i *programIndices) transactionDeclarations(declarations []Declaration) []*TransactionDeclaration {
i.once.Do(i.initializer(declarations))
return i._transactionDeclarations
}
func (i *programIndices) variableDeclarations(declarations []Declaration) []*VariableDeclaration {
i.once.Do(i.initializer(declarations))
return i._variableDeclarations
}
func (i *programIndices) initializer(declarations []Declaration) func() {
return func() {
i.init(declarations)
}
}
func (i *programIndices) init(declarations []Declaration) {
// Important: allocate instead of nil
i._pragmaDeclarations = make([]*PragmaDeclaration, 0)
i._importDeclarations = make([]*ImportDeclaration, 0)
i._compositeDeclarations = make([]*CompositeDeclaration, 0)
i._attachmentDeclarations = make([]*AttachmentDeclaration, 0)
i._interfaceDeclarations = make([]*InterfaceDeclaration, 0)
i._functionDeclarations = make([]*FunctionDeclaration, 0)
i._transactionDeclarations = make([]*TransactionDeclaration, 0)
for _, declaration := range declarations {
switch declaration := declaration.(type) {
case *PragmaDeclaration:
i._pragmaDeclarations = append(i._pragmaDeclarations, declaration)
case *ImportDeclaration:
i._importDeclarations = append(i._importDeclarations, declaration)
case *CompositeDeclaration:
i._compositeDeclarations = append(i._compositeDeclarations, declaration)
case *AttachmentDeclaration:
i._attachmentDeclarations = append(i._attachmentDeclarations, declaration)
case *InterfaceDeclaration:
i._interfaceDeclarations = append(i._interfaceDeclarations, declaration)
case *FunctionDeclaration:
i._functionDeclarations = append(i._functionDeclarations, declaration)
case *TransactionDeclaration:
i._transactionDeclarations = append(i._transactionDeclarations, declaration)
case *VariableDeclaration:
i._variableDeclarations = append(i._variableDeclarations, declaration)
}
}
}