Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement traits, impls and generic functions with trait bounds #710

Merged
merged 2 commits into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions crates/analyzer/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::namespace::items::{Class, ContractId, DiagnosticSink, EventId, FunctionId, Item};
use crate::namespace::types::{SelfDecl, Struct, Type};
use crate::namespace::items::{
Class, ContractId, DiagnosticSink, EventId, FunctionId, FunctionSigId, Item, TraitId,
};
use crate::namespace::types::{Generic, SelfDecl, Struct, Type};
use crate::AnalyzerDb;
use crate::{
builtins::{ContractTypeMethod, GlobalFunction, Intrinsic, ValueMethod},
Expand Down Expand Up @@ -361,7 +363,12 @@ impl Location {
pub fn assign_location(typ: &Type) -> Self {
match typ {
Type::Base(_) | Type::Contract(_) => Location::Value,
Type::Array(_) | Type::Tuple(_) | Type::String(_) | Type::Struct(_) => Location::Memory,
// For now assume that generics can only ever refer to structs
Type::Array(_)
| Type::Tuple(_)
| Type::String(_)
| Type::Struct(_)
| Type::Generic(_) => Location::Memory,
other => panic!("Type {other} can not be assigned, returned or passed"),
}
}
Expand Down Expand Up @@ -458,11 +465,21 @@ pub enum CallType {
class: Class,
function: FunctionId,
},
// some_struct_or_contract.foo()
ValueMethod {
is_self: bool,
class: Class,
method: FunctionId,
},
// some_trait.foo()
// The reason this can not use `ValueMethod` is mainly because the trait might not have a function implementation
// and even if it had it might not be the one that ends up getting executed. An `impl` block will decide that.
TraitValueMethod {
trait_id: TraitId,
method: FunctionSigId,
// Traits can not directly be used as types but can act as bounds for generics. This is the generic type
// that the method is called on.
generic_type: Generic,
},
External {
contract: ContractId,
function: FunctionId,
Expand All @@ -479,6 +496,7 @@ impl CallType {
| BuiltinValueMethod { .. }
| TypeConstructor(_)
| Intrinsic(_)
| TraitValueMethod { .. }
| BuiltinAssociatedFunction { .. } => None,
AssociatedFunction { function: id, .. }
| ValueMethod { method: id, .. }
Expand All @@ -497,6 +515,7 @@ impl CallType {
| CallType::ValueMethod { method: id, .. }
| CallType::External { function: id, .. }
| CallType::Pure(id) => id.name(db),
CallType::TraitValueMethod { method: id, .. } => id.name(db),
CallType::TypeConstructor(typ) => typ.name(),
}
}
Expand All @@ -508,7 +527,7 @@ impl CallType {
// check that this is the `Context` struct defined in `std`
// this should be deleted once associated functions are supported and we can
// define unsafe constructors in Fe
struct_.name == "Context" && struct_.id.module(db).ingot(db).name(db) == "std"
struct_.name == "Context" && struct_.id.module(db).is_in_std(db)
} else {
self.function().map(|id| id.is_unsafe(db)).unwrap_or(false)
}
Expand Down
32 changes: 28 additions & 4 deletions crates/analyzer/src/db.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::context::{Analysis, Constant, FunctionBody};
use crate::errors::{ConstEvalError, TypeError};
use crate::namespace::items::{
self, ContractFieldId, ContractId, DepGraphWrapper, EventId, FunctionId, IngotId, Item,
ModuleConstantId, ModuleId, StructFieldId, StructId, TypeAliasId,
self, ContractFieldId, ContractId, DepGraphWrapper, EventId, FunctionId, FunctionSigId, ImplId,
IngotId, Item, ModuleConstantId, ModuleId, StructFieldId, StructId, TraitId, TypeAliasId,
};
use crate::namespace::types;
use crate::namespace::types::{self, Type};
use fe_common::db::{SourceDb, SourceDbStorage, Upcast, UpcastMut};
use fe_common::{SourceFileId, Span};
use fe_parser::ast;
Expand All @@ -24,6 +24,10 @@ pub trait AnalyzerDb: SourceDb + Upcast<dyn SourceDb> + UpcastMut<dyn SourceDb>
#[salsa::interned]
fn intern_struct(&self, data: Rc<items::Struct>) -> StructId;
#[salsa::interned]
fn intern_trait(&self, data: Rc<items::Trait>) -> TraitId;
#[salsa::interned]
fn intern_impl(&self, data: Rc<items::Impl>) -> ImplId;
#[salsa::interned]
fn intern_struct_field(&self, data: Rc<items::StructField>) -> StructFieldId;
#[salsa::interned]
fn intern_type_alias(&self, data: Rc<items::TypeAlias>) -> TypeAliasId;
Expand All @@ -32,6 +36,8 @@ pub trait AnalyzerDb: SourceDb + Upcast<dyn SourceDb> + UpcastMut<dyn SourceDb>
#[salsa::interned]
fn intern_contract_field(&self, data: Rc<items::ContractField>) -> ContractFieldId;
#[salsa::interned]
fn intern_function_sig(&self, data: Rc<items::FunctionSig>) -> FunctionSigId;
#[salsa::interned]
fn intern_function(&self, data: Rc<items::Function>) -> FunctionId;
#[salsa::interned]
fn intern_event(&self, data: Rc<items::Event>) -> EventId;
Expand Down Expand Up @@ -60,8 +66,12 @@ pub trait AnalyzerDb: SourceDb + Upcast<dyn SourceDb> + UpcastMut<dyn SourceDb>
fn module_is_incomplete(&self, module: ModuleId) -> bool;
#[salsa::invoke(queries::module::module_all_items)]
fn module_all_items(&self, module: ModuleId) -> Rc<[Item]>;
#[salsa::invoke(queries::module::module_all_impls)]
fn module_all_impls(&self, module: ModuleId) -> Rc<[ImplId]>;
#[salsa::invoke(queries::module::module_item_map)]
fn module_item_map(&self, module: ModuleId) -> Analysis<Rc<IndexMap<SmolStr, Item>>>;
#[salsa::invoke(queries::module::module_impl_map)]
fn module_impl_map(&self, module: ModuleId) -> Analysis<Rc<IndexMap<(TraitId, Type), ImplId>>>;
#[salsa::invoke(queries::module::module_contracts)]
fn module_contracts(&self, module: ModuleId) -> Rc<[ContractId]>;
#[salsa::invoke(queries::module::module_structs)]
Expand Down Expand Up @@ -130,7 +140,7 @@ pub trait AnalyzerDb: SourceDb + Upcast<dyn SourceDb> + UpcastMut<dyn SourceDb>

// Function
#[salsa::invoke(queries::functions::function_signature)]
fn function_signature(&self, id: FunctionId) -> Analysis<Rc<types::FunctionSignature>>;
fn function_signature(&self, id: FunctionSigId) -> Analysis<Rc<types::FunctionSignature>>;
#[salsa::invoke(queries::functions::function_body)]
fn function_body(&self, id: FunctionId) -> Analysis<Rc<FunctionBody>>;
#[salsa::cycle(queries::functions::function_dependency_graph_cycle)]
Expand All @@ -154,6 +164,20 @@ pub trait AnalyzerDb: SourceDb + Upcast<dyn SourceDb> + UpcastMut<dyn SourceDb>
#[salsa::invoke(queries::structs::struct_dependency_graph)]
fn struct_dependency_graph(&self, id: StructId) -> Analysis<DepGraphWrapper>;

// Trait
#[salsa::invoke(queries::traits::trait_type)]
fn trait_type(&self, id: TraitId) -> Rc<types::Trait>;
#[salsa::invoke(queries::traits::trait_all_functions)]
fn trait_all_functions(&self, id: TraitId) -> Rc<[FunctionSigId]>;
#[salsa::invoke(queries::traits::trait_function_map)]
fn trait_function_map(&self, id: TraitId) -> Analysis<Rc<IndexMap<SmolStr, FunctionSigId>>>;

// Impl
#[salsa::invoke(queries::impls::impl_all_functions)]
fn impl_all_functions(&self, id: ImplId) -> Rc<[FunctionId]>;
#[salsa::invoke(queries::impls::impl_function_map)]
fn impl_function_map(&self, id: ImplId) -> Analysis<Rc<IndexMap<SmolStr, FunctionId>>>;

// Event
#[salsa::invoke(queries::events::event_type)]
fn event_type(&self, event: EventId) -> Analysis<Rc<types::Event>>;
Expand Down
2 changes: 2 additions & 0 deletions crates/analyzer/src/db/queries.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
pub mod contracts;
pub mod events;
pub mod functions;
pub mod impls;
pub mod ingots;
pub mod module;
pub mod structs;
pub mod traits;
pub mod types;
22 changes: 13 additions & 9 deletions crates/analyzer/src/db/queries/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,9 @@ pub fn contract_all_functions(db: &dyn AnalyzerDb, contract: ContractId) -> Rc<[
body.iter()
.filter_map(|stmt| match stmt {
ast::ContractStmt::Event(_) => None,
ast::ContractStmt::Function(node) => {
Some(db.intern_function(Rc::new(items::Function {
ast: node.clone(),
module,
parent: Some(items::Class::Contract(contract)),
})))
}
ast::ContractStmt::Function(node) => Some(db.intern_function(Rc::new(
items::Function::new(db, node, Some(items::Class::Contract(contract)), module),
))),
})
.collect()
}
Expand All @@ -53,7 +49,7 @@ pub fn contract_function_map(
def_name,
&NamedThing::Item(Item::Event(event)),
Some(event.name_span(db)),
def.kind.name.span,
def.kind.sig.kind.name.span,
);
continue;
}
Expand All @@ -64,7 +60,7 @@ pub fn contract_function_map(
def_name,
&named_item,
named_item.name_span(db),
def.kind.name.span,
def.kind.sig.kind.name.span,
);
continue;
}
Expand Down Expand Up @@ -334,6 +330,14 @@ pub fn contract_field_type(

let node = &field.data(db).ast;

if let Ok(Type::Trait(ref val)) = typ {
scope.error(
"traits can not be used as contract fields",
node.span,
&format!("trait `{}` can not appear here", val.name),
);
}

if node.kind.is_pub {
scope.not_yet_implemented("contract `pub` fields", node.span);
}
Expand Down
Loading