forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.rs
More file actions
609 lines (548 loc) · 19.3 KB
/
Copy pathcontext.rs
File metadata and controls
609 lines (548 loc) · 19.3 KB
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
use crate::builtins::GlobalMethod;
use crate::errors::CannotMove;
use crate::namespace::events::EventDef;
use crate::namespace::scopes::{ContractFunctionDef, ContractScope, ModuleScope, Shared};
use crate::namespace::types::{Array, Contract, FixedSize, Struct, Tuple, Type};
pub use fe_common::diagnostics::Label;
use fe_common::diagnostics::{Diagnostic, Severity};
use fe_common::files::SourceFileId;
use fe_common::Span;
use fe_parser::ast as fe;
use fe_parser::node::{Node, NodeId};
use std::cell::RefCell;
use std::collections::{BTreeMap, BTreeSet};
use std::fmt::{Debug, Display};
use std::hash::Hash;
use std::rc::Rc;
/// Indicates where an expression is stored.
#[derive(Copy, Clone, Debug, PartialEq, Hash)]
pub enum Location {
/// A storage value may not have a nonce known at compile time, so it is
/// optional.
Storage {
nonce: Option<usize>,
},
Memory,
Value,
}
impl Location {
/// The expected location of a value with the given type when being
/// assigned, returned, or passed.
pub fn assign_location(typ: &FixedSize) -> Self {
match typ {
FixedSize::Base(_) => Location::Value,
FixedSize::Contract(_) => Location::Value,
FixedSize::Array(_) => Location::Memory,
FixedSize::Tuple(_) => Location::Memory,
FixedSize::String(_) => Location::Memory,
FixedSize::Struct(_) => Location::Memory,
}
}
}
/// Contains contextual information relating to a contract AST node.
#[derive(Clone, Debug, PartialEq, Hash)]
pub struct ContractAttributes {
/// Public functions that have been defined by the user.
pub public_functions: Vec<FunctionAttributes>,
/// An init function that has been defined by the user.
pub init_function: Option<FunctionAttributes>,
/// Events that have been defined by the user.
pub events: Vec<EventDef>,
/// List expressions that the contract uses
pub list_expressions: BTreeSet<Array>,
/// Static strings that the contract defines
pub string_literals: BTreeSet<String>,
/// Structs that have been defined by the user
pub structs: Vec<Struct>,
/// External contracts that may be called from within this contract.
pub external_contracts: Vec<Contract>,
/// Names of contracts that have been created inside of this contract.
pub created_contracts: BTreeSet<String>,
}
impl From<Shared<ContractScope>> for ContractAttributes {
fn from(scope: Shared<ContractScope>) -> Self {
let mut public_functions = vec![];
let mut init_function = None;
for (name, def) in scope.borrow().function_defs.iter() {
if !def.is_public {
continue;
}
if name != "__init__" {
public_functions.push(FunctionAttributes {
is_public: def.is_public,
name: name.clone(),
params: def.params.to_owned(),
return_type: def.return_type.to_owned(),
});
} else {
init_function = Some(FunctionAttributes {
is_public: def.is_public,
name: name.clone(),
params: def.params.to_owned(),
return_type: FixedSize::unit(),
})
}
}
let external_contracts = scope.borrow().get_module_type_defs(|typ| {
if let Type::Contract(contract) = typ {
// Skip our own contract
if contract.name == scope.borrow().name {
None
} else {
Some(contract.to_owned())
}
} else {
None
}
});
let structs = scope.borrow().get_module_type_defs(|typ| {
if let Type::Struct(val) = typ {
Some(val.to_owned())
} else {
None
}
});
ContractAttributes {
public_functions,
init_function,
events: scope
.borrow()
.event_defs
.values()
.map(|event| event.to_owned())
.collect::<Vec<EventDef>>(),
list_expressions: scope.borrow().list_expressions.clone(),
string_literals: scope.borrow().string_defs.clone(),
structs,
external_contracts,
created_contracts: scope.borrow().created_contracts.to_owned(),
}
}
}
/// Contains contextual information relating to an expression AST node.
#[derive(Clone, Debug, PartialEq, Hash)]
pub struct ExpressionAttributes {
pub typ: Type,
pub location: Location,
pub move_location: Option<Location>,
}
impl ExpressionAttributes {
pub fn new(typ: Type, location: Location) -> Self {
Self {
typ,
location,
move_location: None,
}
}
/// Adds a move to memory.
pub fn into_cloned(mut self) -> Self {
self.move_location = Some(Location::Memory);
self
}
/// Adds a move to value, if it is in storage or memory.
pub fn into_loaded(mut self) -> Result<Self, CannotMove> {
match self.typ {
Type::Base(_) => {}
Type::Contract(_) => {}
_ => return Err(CannotMove),
}
if self.location != Location::Value {
self.move_location = Some(Location::Value);
}
Ok(self)
}
/// The final location of an expression after a possible move.
pub fn final_location(&self) -> Location {
if let Some(location) = self.move_location {
return location;
}
self.location
}
}
/// The type of a function call.
#[derive(Clone, Debug, PartialEq, Hash)]
pub enum CallType {
BuiltinFunction { func: GlobalMethod },
TypeConstructor { typ: Type },
SelfAttribute { func_name: String },
ValueAttribute,
TypeAttribute { typ: Type, func_name: String },
}
/// Contains contextual information relating to a function definition AST node.
#[derive(Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub struct FunctionAttributes {
pub is_public: bool,
pub name: String,
pub params: Vec<(String, FixedSize)>,
pub return_type: FixedSize,
}
impl FunctionAttributes {
pub fn param_types(&self) -> Vec<FixedSize> {
self.params.iter().map(|(_, typ)| typ.to_owned()).collect()
}
pub fn param_names(&self) -> Vec<String> {
self.params
.iter()
.map(|(name, _)| name.to_owned())
.collect()
}
}
impl From<ContractFunctionDef> for FunctionAttributes {
fn from(def: ContractFunctionDef) -> Self {
Self {
is_public: def.is_public,
name: def.name,
params: def.params,
return_type: def.return_type,
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct ModuleAttributes {
/// Type definitions in a module.
pub type_defs: BTreeMap<String, Type>,
/// Tuples that were used inside of a module.
///
/// BTreeSet is used for ordering, this way items are retrieved in the same order every time.
pub tuples_used: BTreeSet<Tuple>,
}
impl From<Shared<ModuleScope>> for ModuleAttributes {
fn from(scope: Shared<ModuleScope>) -> Self {
Self {
type_defs: scope.borrow().type_defs.clone(),
tuples_used: scope.borrow().tuples_used.clone(),
}
}
}
/// Contains contextual information about a Fe module and can be queried using
/// `Spanned` AST nodes.
#[derive(Clone, Debug)]
pub struct Context {
/// Node ids in the order they were visited.
pub node_ids: Vec<NodeId>,
/// The span of a given node id.
pub spans: BTreeMap<NodeId, Span>,
pub expressions: BTreeMap<NodeId, ExpressionAttributes>,
pub emits: BTreeMap<NodeId, EventDef>,
pub functions: BTreeMap<NodeId, FunctionAttributes>,
pub declarations: BTreeMap<NodeId, FixedSize>,
pub contracts: BTreeMap<NodeId, ContractAttributes>,
pub calls: BTreeMap<NodeId, CallType>,
pub events: BTreeMap<NodeId, EventDef>,
pub type_descs: BTreeMap<NodeId, Type>,
pub module: Option<ModuleAttributes>,
pub file_id: SourceFileId,
pub diagnostics: Vec<Diagnostic>,
/// Holds fresh id for [`Context::make_unique_name`]
fresh_id: u64,
}
impl Context {
pub fn new_shared(file_id: SourceFileId) -> Shared<Self> {
Rc::new(RefCell::new(Self::new(file_id)))
}
pub fn new(file_id: SourceFileId) -> Self {
Context {
node_ids: Vec::new(),
spans: BTreeMap::new(),
expressions: BTreeMap::new(),
emits: BTreeMap::new(),
functions: BTreeMap::new(),
declarations: BTreeMap::new(),
contracts: BTreeMap::new(),
calls: BTreeMap::new(),
events: BTreeMap::new(),
type_descs: BTreeMap::new(),
module: None,
file_id,
diagnostics: Vec::new(),
fresh_id: 0,
}
}
fn add_node<T>(&mut self, node: &Node<T>) {
self.node_ids.push(node.id);
self.spans.insert(node.id, node.span);
}
/// Attribute contextual information to an expression node.
///
/// # Panics
///
/// Panics if an entry already exists for the node id.
pub fn add_expression(&mut self, node: &Node<fe::Expr>, attributes: ExpressionAttributes) {
self.add_node(node);
expect_none(
self.expressions.insert(node.id, attributes),
"expression attributes already exist",
);
}
/// Update the expression attributes.
///
/// # Panics
///
/// Panics if an entry does not already exist for the node id.
pub fn update_expression(&mut self, node: &Node<fe::Expr>, attributes: ExpressionAttributes) {
self.expressions
.insert(node.id, attributes)
.expect("expression attributes do not exist");
}
/// Get information that has been attributed to an expression node.
pub fn get_expression<T: Into<NodeId>>(&self, node_id: T) -> Option<&ExpressionAttributes> {
self.expressions.get(&node_id.into())
}
/// Attribute contextual information to an emit statement node.
///
/// # Panics
///
/// Panics if an entry already exists for the node id.
pub fn add_emit(&mut self, node: &Node<fe::FuncStmt>, event: EventDef) {
self.add_node(node);
expect_none(
self.emits.insert(node.id, event),
"emit statement attributes already exist",
);
}
/// Get information that has been attributed to an emit statement node.
pub fn get_emit<T: Into<NodeId>>(&self, node_id: T) -> Option<&EventDef> {
self.emits.get(&node_id.into())
}
/// Attribute contextual information to a function definition node.
///
/// # Panics
///
/// Panics if an entry already exists for the node id.
pub fn add_function(&mut self, node: &Node<fe::ContractStmt>, attributes: FunctionAttributes) {
self.add_node(node);
expect_none(
self.functions.insert(node.id, attributes),
"function attributes already exist",
);
}
/// Get information that has been attributed to a function definition node.
pub fn get_function<T: Into<NodeId>>(&self, node_id: T) -> Option<&FunctionAttributes> {
self.functions.get(&node_id.into())
}
/// Attribute contextual information to a declaration node.
///
/// # Panics
///
/// Panics if an entry already exists for the node id.
pub fn add_declaration(&mut self, node: &Node<fe::FuncStmt>, typ: FixedSize) {
self.add_node(node);
expect_none(
self.declarations.insert(node.id, typ),
"declaration attributes already exist",
);
}
/// Get information that has been attributed to a declaration node.
pub fn get_declaration<T: Into<NodeId>>(&self, node_id: T) -> Option<&FixedSize> {
self.declarations.get(&node_id.into())
}
/// Attribute contextual information to a contract definition node.
///
/// # Panics
///
/// Panics if an entry already exists for the node id.
pub fn add_contract(&mut self, node: &Node<fe::ModuleStmt>, attributes: ContractAttributes) {
self.add_node(node);
expect_none(
self.contracts.insert(node.id, attributes),
"contract attributes already exist",
);
}
/// Get information that has been attributed to a contract definition node.
pub fn get_contract<T: Into<NodeId>>(&self, node_id: T) -> Option<&ContractAttributes> {
self.contracts.get(&node_id.into())
}
/// Attribute contextual information to a call expression node.
///
/// # Panics
///
/// Panics if an entry already exists for the node id.
pub fn add_call(&mut self, node: &Node<fe::Expr>, call_type: CallType) {
self.add_node(node);
expect_none(
self.calls.insert(node.id, call_type),
"call attributes already exist",
);
}
/// Get information that has been attributed to a call expression node.
pub fn get_call<T: Into<NodeId>>(&self, node_id: T) -> Option<&CallType> {
self.calls.get(&node_id.into())
}
/// Attribute contextual information to an event definition node.
///
/// # Panics
///
/// Panics if an entry already exists for the node id.
pub fn add_event(&mut self, node: &Node<fe::ContractStmt>, event: EventDef) {
self.add_node(node);
expect_none(
self.events.insert(node.id, event),
"event attributes already exist",
);
}
/// Get information that has been attributed to an event definition node.
pub fn get_event<T: Into<NodeId>>(&self, node_id: T) -> Option<&EventDef> {
self.events.get(&node_id.into())
}
/// Attribute contextual information to a type description node.
///
/// # Panics
///
/// Panics if an entry already exists for the node id.
pub fn add_type_desc(&mut self, node: &Node<fe::TypeDesc>, typ: Type) {
self.add_node(node);
expect_none(
self.type_descs.insert(node.id, typ),
"type desc attributes already exist",
);
}
/// Get information that has been attributed to a type description node.
pub fn get_type_desc<T: Into<NodeId>>(&self, node_id: T) -> Option<&Type> {
self.type_descs.get(&node_id.into())
}
/// Attribute contextual information to the module.
pub fn set_module(&mut self, attributes: ModuleAttributes) {
self.module = Some(attributes);
}
/// Get information that has been attributed to the module.
pub fn get_module(&self) -> Option<&ModuleAttributes> {
self.module.as_ref()
}
/// Get the span and attributes of all expressions.
pub fn get_spanned_expressions(&self) -> Vec<(Span, ExpressionAttributes)> {
self.node_ids
.iter()
.filter_map(|node_id| {
self.get_expression(*node_id)
.map(|attributes| (self.spans[node_id], attributes.to_owned()))
})
.collect::<Vec<_>>()
}
/// Get the span and attributes of all emits.
pub fn get_spanned_emits(&self) -> Vec<(Span, EventDef)> {
self.node_ids
.iter()
.filter_map(|node_id| {
self.get_emit(*node_id)
.map(|attributes| (self.spans[node_id], attributes.to_owned()))
})
.collect::<Vec<_>>()
}
/// Get the span and attributes of all functions.
pub fn get_spanned_functions(&self) -> Vec<(Span, FunctionAttributes)> {
self.node_ids
.iter()
.filter_map(|node_id| {
self.get_function(*node_id)
.map(|attributes| (self.spans[node_id], attributes.to_owned()))
})
.collect::<Vec<_>>()
}
/// Get the span and attributes of all declarations.
pub fn get_spanned_declarations(&self) -> Vec<(Span, FixedSize)> {
self.node_ids
.iter()
.filter_map(|node_id| {
self.get_declaration(*node_id)
.map(|attributes| (self.spans[node_id], attributes.to_owned()))
})
.collect::<Vec<_>>()
}
/// Get the span and attributes of all contracts.
pub fn get_spanned_contracts(&self) -> Vec<(Span, ContractAttributes)> {
self.node_ids
.iter()
.filter_map(|node_id| {
self.get_contract(*node_id)
.map(|attributes| (self.spans[node_id], attributes.to_owned()))
})
.collect::<Vec<_>>()
}
/// Get the span and attributes of all calls.
pub fn get_spanned_calls(&self) -> Vec<(Span, CallType)> {
self.node_ids
.iter()
.filter_map(|node_id| {
self.get_call(*node_id)
.map(|attributes| (self.spans[node_id], attributes.to_owned()))
})
.collect::<Vec<_>>()
}
/// Get the span and attributes of all events.
pub fn get_spanned_events(&self) -> Vec<(Span, EventDef)> {
self.node_ids
.iter()
.filter_map(|node_id| {
self.get_event(*node_id)
.map(|attributes| (self.spans[node_id], attributes.to_owned()))
})
.collect::<Vec<_>>()
}
/// Get the span and attributes of all type descs.
pub fn get_spanned_type_descs(&self) -> Vec<(Span, Type)> {
self.node_ids
.iter()
.filter_map(|node_id| {
self.get_type_desc(*node_id)
.map(|attributes| (self.spans[node_id], attributes.to_owned()))
})
.collect::<Vec<_>>()
}
pub fn error(
&mut self,
message: impl Into<String>,
label_span: Span,
label: impl Into<String>,
) {
self.fancy_error(message, vec![Label::primary(label_span, label)], vec![]);
}
pub fn type_error(
&mut self,
message: impl Into<String>,
span: Span,
expected: impl Display,
actual: impl Display,
) {
self.error(
message,
span,
format!("this has type `{}`; expected type `{}`", actual, expected),
)
}
pub fn not_yet_implemented(&mut self, feature: impl Display, span: Span) {
self.error(
"feature not yet implemented",
span,
format!("{} is not yet implemented", feature),
)
}
pub fn fancy_error(
&mut self,
message: impl Into<String>,
labels: Vec<Label>,
notes: Vec<String>,
) {
self.diagnostics.push(Diagnostic {
severity: Severity::Error,
code: None,
message: message.into(),
labels: labels
.into_iter()
.map(|lbl| lbl.into_cs_label(self.file_id))
.collect(),
notes,
});
}
/// Makes a unique name from the given name, keeping it as readable as possible.
pub fn make_unique_name(&mut self, name: &str) -> String {
let id = self.fresh_id;
self.fresh_id += 1;
format!("${}_{}", name, id)
}
}
/// Temporary helper until `expect_none` is stable.
fn expect_none<T>(item: Option<T>, msg: &str) {
if item.is_some() {
panic!("{}", msg)
}
}