-
Notifications
You must be signed in to change notification settings - Fork 23
/
contract_definition.ts
296 lines (257 loc) · 8.54 KB
/
contract_definition.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import { ASTNode, ASTNodeWithChildren } from "../../ast_node";
import { ContractKind } from "../../constants";
import {
getDanglingDocumentation,
getDocumentation,
setDanglingDocumentation,
setDocumentation,
WithDanglingDocs,
WithPrecedingDocs
} from "../../documentation";
import { InheritanceSpecifier } from "../meta/inheritance_specifier";
import { SourceUnit } from "../meta/source_unit";
import { StructuredDocumentation } from "../meta/structured_documentation";
import { UsingForDirective } from "../meta/using_for_directive";
import { EnumDefinition } from "./enum_definition";
import { ErrorDefinition } from "./error_definition";
import { EventDefinition } from "./event_definition";
import { FunctionDefinition } from "./function_definition";
import { ModifierDefinition } from "./modifier_definition";
import { StructDefinition } from "./struct_definition";
import { UserDefinedValueTypeDefinition } from "./user_defined_value_type_definition";
import { VariableDeclaration } from "./variable_declaration";
export class ContractDefinition
extends ASTNodeWithChildren<ASTNode>
implements WithPrecedingDocs, WithDanglingDocs
{
docString?: string;
danglingDocString?: string;
/**
* The contract name
*/
name: string;
/**
* The source range for name string
*/
nameLocation?: string;
/**
* Id of its scoped source unit
*/
scope: number;
/**
* Type of contract declaration, e.g. `contract`, `library` or `interface`.
*/
kind: ContractKind;
/**
* Is `true` if contract is declared as an abstract
* (using `abstract` keyword since Solidity 0.6).
*
* Is `false` otherwise.
*/
abstract: boolean;
/**
* Is `false` if one of the functions is not implemented.
*
* Is `true` otherwise.
*/
fullyImplemented: boolean;
/**
* C3-linearized base contract ids including the current contract's id
*/
linearizedBaseContracts: number[];
/**
* Used error definition ids (including external definition ids)
*/
usedErrors: number[];
/**
* Used error definition ids (including external definition ids)
*/
usedEvents: number[];
constructor(
id: number,
src: string,
name: string,
scope: number,
kind: ContractKind,
abstract: boolean,
fullyImplemented: boolean,
linearizedBaseContracts: number[],
usedErrors: number[],
usedEvents: number[],
documentation?: string | StructuredDocumentation,
children?: Iterable<ASTNode>,
nameLocation?: string,
raw?: any
) {
super(id, src, raw);
this.name = name;
this.scope = scope;
this.kind = kind;
this.abstract = abstract;
this.fullyImplemented = fullyImplemented;
this.linearizedBaseContracts = linearizedBaseContracts;
this.usedErrors = usedErrors;
this.usedEvents = usedEvents;
if (children) {
for (const node of children) {
this.appendChild(node);
}
}
this.documentation = documentation;
this.nameLocation = nameLocation;
}
/**
* Optional documentation appearing above the contract definition:
* - Is `undefined` when not specified.
* - Is type of `string` when specified and compiler version is older than `0.6.3`.
* - Is instance of `StructuredDocumentation` when specified and compiler version is `0.6.3` or newer.
*/
get documentation(): string | StructuredDocumentation | undefined {
return getDocumentation(this);
}
set documentation(value: string | StructuredDocumentation | undefined) {
setDocumentation(this, value);
}
/**
* Optional documentation that is dangling in the source fragment,
* that is after end of last child and before the end of the current node.
*
* It is:
* - Is `undefined` when not detected.
* - Is type of `string` for compatibility reasons.
*/
get danglingDocumentation(): string | StructuredDocumentation | undefined {
return getDanglingDocumentation(this);
}
set danglingDocumentation(value: string | StructuredDocumentation | undefined) {
setDanglingDocumentation(this, value);
}
/**
* Reference to its scoped source unit
*/
get vScope(): SourceUnit {
return this.requiredContext.locate(this.scope) as SourceUnit;
}
set vScope(value: SourceUnit) {
if (!this.requiredContext.contains(value)) {
throw new Error(`Node ${value.type}#${value.id} not belongs to a current context`);
}
this.scope = value.id;
}
/**
* C3-linearized base contract references including the current contract
*/
get vLinearizedBaseContracts(): readonly ContractDefinition[] {
const context = this.requiredContext;
return this.linearizedBaseContracts.map((id) => context.locate(id)) as ContractDefinition[];
}
/**
* Used error definitions (including external definitions)
*/
get vUsedErrors(): readonly ErrorDefinition[] {
const context = this.requiredContext;
return this.usedErrors.map((id) => context.locate(id)) as ErrorDefinition[];
}
/**
* Used event definitions (including external definitions)
*/
get vUsedEvents(): readonly EventDefinition[] {
const context = this.requiredContext;
return this.usedEvents.map((id) => context.locate(id)) as EventDefinition[];
}
/**
* Inheritance specifiers
*/
get vInheritanceSpecifiers(): readonly InheritanceSpecifier[] {
return this.ownChildren.filter(
(node) => node instanceof InheritanceSpecifier
) as InheritanceSpecifier[];
}
/**
* State variables are `VariableDeclaration`s
* that have the attribute `stateVariable` set to `true`
* and that are direct children of a contract
*/
get vStateVariables(): readonly VariableDeclaration[] {
return this.ownChildren.filter(
(node) => node instanceof VariableDeclaration
) as VariableDeclaration[];
}
/**
* Modifiers of the contract
*/
get vModifiers(): readonly ModifierDefinition[] {
return this.ownChildren.filter(
(node) => node instanceof ModifierDefinition
) as ModifierDefinition[];
}
/**
* Events of the contract
*/
get vEvents(): readonly EventDefinition[] {
return this.ownChildren.filter(
(node) => node instanceof EventDefinition
) as EventDefinition[];
}
/**
* Errors of the contract
*/
get vErrors(): readonly ErrorDefinition[] {
return this.ownChildren.filter(
(node) => node instanceof ErrorDefinition
) as ErrorDefinition[];
}
/**
* Functions of the contract
*/
get vFunctions(): readonly FunctionDefinition[] {
return this.ownChildren.filter(
(node) => node instanceof FunctionDefinition
) as FunctionDefinition[];
}
/**
* Type-bound libraries directives of the contract
*/
get vUsingForDirectives(): readonly UsingForDirective[] {
return this.ownChildren.filter(
(node) => node instanceof UsingForDirective
) as UsingForDirective[];
}
/**
* Structs of the contract
*/
get vStructs(): readonly StructDefinition[] {
return this.ownChildren.filter(
(node) => node instanceof StructDefinition
) as StructDefinition[];
}
/**
* Enums of the contract
*/
get vEnums(): readonly EnumDefinition[] {
return this.ownChildren.filter(
(node) => node instanceof EnumDefinition
) as EnumDefinition[];
}
/**
* User-defined value type definitions of contract
*/
get vUserDefinedValueTypes(): readonly UserDefinedValueTypeDefinition[] {
return this.ownChildren.filter(
(node) => node instanceof UserDefinedValueTypeDefinition
) as UserDefinedValueTypeDefinition[];
}
/**
* Constructor reference (if definition is present for this contract)
*/
get vConstructor(): FunctionDefinition | undefined {
return this.vFunctions.find((fn) => fn.isConstructor);
}
/**
* Returns `true` if `other` contract is present in the inheritance chain
* of the current contract. Returns `false` otherwise.
*/
isSubclassOf(other: ContractDefinition): boolean {
return this.vLinearizedBaseContracts.includes(other);
}
}