-
Notifications
You must be signed in to change notification settings - Fork 23
/
source_unit.ts
224 lines (195 loc) · 6.59 KB
/
source_unit.ts
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import { ABIEncoderVersion, ABIEncoderVersions } from "../../../types";
import { ASTNode, ASTNodeWithChildren } from "../../ast_node";
import { EventDefinition } from "../declaration";
import { ContractDefinition } from "../declaration/contract_definition";
import { EnumDefinition } from "../declaration/enum_definition";
import { ErrorDefinition } from "../declaration/error_definition";
import { FunctionDefinition } from "../declaration/function_definition";
import { StructDefinition } from "../declaration/struct_definition";
import { UserDefinedValueTypeDefinition } from "../declaration/user_defined_value_type_definition";
import { VariableDeclaration } from "../declaration/variable_declaration";
import { ImportDirective } from "./import_directive";
import { PragmaDirective } from "./pragma_directive";
import { UsingForDirective } from "./using_for_directive";
export type ExportedSymbol =
| ContractDefinition
| StructDefinition
| EnumDefinition
| ErrorDefinition
| FunctionDefinition
| EventDefinition
| UserDefinedValueTypeDefinition
| VariableDeclaration
| ImportDirective;
export class SourceUnit extends ASTNodeWithChildren<ASTNode> {
/**
* Original "sources" key, that corresponds to current AST entry.
*/
sourceEntryKey: string;
/**
* Index of the source unit in the "sourceList".
*/
sourceListIndex: number;
/**
* File path to the source file
*/
absolutePath: string;
/**
* Exported symbols dictionary, e.g. `{ "A": 74, "B": 34, ... }`
*/
exportedSymbols: Map<string, number>;
/**
* SPDX license identifier (if provided)
*/
license?: string;
constructor(
id: number,
src: string,
sourceEntryKey: string,
sourceListIndex: number,
absolutePath: string,
exportedSymbols: Map<string, number>,
children?: Iterable<ASTNode>,
license?: string,
raw?: any
) {
super(id, src, raw);
this.sourceEntryKey = sourceEntryKey;
this.sourceListIndex = sourceListIndex;
this.absolutePath = absolutePath;
this.exportedSymbols = exportedSymbols;
this.license = license;
if (children) {
for (const node of children) {
this.appendChild(node);
}
}
}
/**
* References to pragma directives
*/
get vPragmaDirectives(): readonly PragmaDirective[] {
return this.ownChildren.filter(
(node): node is PragmaDirective => node instanceof PragmaDirective
);
}
/**
* References to import directives
*/
get vImportDirectives(): readonly ImportDirective[] {
return this.ownChildren.filter(
(node): node is ImportDirective => node instanceof ImportDirective
);
}
/**
* References to contract definitions
*/
get vContracts(): readonly ContractDefinition[] {
return this.ownChildren.filter(
(node): node is ContractDefinition => node instanceof ContractDefinition
);
}
/**
* References to file-level enum definitions
*/
get vEnums(): readonly EnumDefinition[] {
return this.ownChildren.filter(
(node): node is EnumDefinition => node instanceof EnumDefinition
);
}
/**
* References to file-level error definitions
*/
get vErrors(): readonly ErrorDefinition[] {
return this.ownChildren.filter(
(node): node is ErrorDefinition => node instanceof ErrorDefinition
);
}
/**
* References to file-level struct definitions
*/
get vStructs(): readonly StructDefinition[] {
return this.ownChildren.filter(
(node): node is StructDefinition => node instanceof StructDefinition
);
}
/**
* References to file-level function definitions (free functions)
*/
get vFunctions(): readonly FunctionDefinition[] {
return this.ownChildren.filter(
(node): node is FunctionDefinition => node instanceof FunctionDefinition
);
}
/**
* References to file-level event definitions
*/
get vEvents(): readonly EventDefinition[] {
return this.ownChildren.filter(
(node): node is EventDefinition => node instanceof EventDefinition
);
}
/**
* References to file-level constant variable definitions
*/
get vVariables(): readonly VariableDeclaration[] {
return this.ownChildren.filter(
(node): node is VariableDeclaration => node instanceof VariableDeclaration
);
}
/**
* References to file-level user-defined value type definitions
*/
get vUserDefinedValueTypes(): readonly UserDefinedValueTypeDefinition[] {
return this.ownChildren.filter(
(node): node is UserDefinedValueTypeDefinition =>
node instanceof UserDefinedValueTypeDefinition
);
}
/**
* References to file-level using-for directives
*/
get vUsingForDirectives(): readonly UsingForDirective[] {
return this.ownChildren.filter(
(node): node is UsingForDirective => node instanceof UsingForDirective
);
}
/**
* Referenced exported symbols
*/
get vExportedSymbols(): ReadonlyMap<string, ExportedSymbol> {
const result = new Map<string, ExportedSymbol>();
const context = this.requiredContext;
for (const [name, id] of this.exportedSymbols.entries()) {
result.set(name, context.locate(id) as ExportedSymbol);
}
return result;
}
/**
* Returns user-defined ABI encoder version for the source unit.
* If there is no encoder version defined in the pragma directives,
* then returns `undefined`.
*/
get abiEncoderVersion(): ABIEncoderVersion | undefined {
for (const directive of this.vPragmaDirectives) {
if (directive.vIdentifier === "abicoder") {
const raw = directive.literals[1];
if (raw === "v1") {
return ABIEncoderVersion.V1;
}
if (raw === "v2") {
return ABIEncoderVersion.V2;
}
throw new Error(`Unknown abicoder pragma version ${raw}`);
}
if (
directive.vIdentifier === "experimental" &&
directive.literals.length === 2 &&
ABIEncoderVersions.has(directive.literals[1])
) {
return directive.literals[1] as ABIEncoderVersion;
}
}
return undefined;
}
}