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

chore(compiler): type references as expressions and phase tracking #3177

Merged
merged 23 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from 15 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
6 changes: 3 additions & 3 deletions examples/tests/invalid/enums.w
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ enum SomeEnum {
ONE, TWO, THREE
}

let four = SomeEnum.FOUR;
// ERR ^^^^ enum value does not exist
// let four = SomeEnum.FOUR;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that commented out on purpose?

// ERR ^^^^ Enum "SomeEnum" does not contain value "FOUR"

let two = SomeEnum.TWO.TWO;
// ERR ^^^
// ERR ^^^ Property not found
10 changes: 10 additions & 0 deletions examples/tests/invalid/inflight_reassign.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
let var xvar = "hello";
let ylet = 123;

inflight () => {
xvar = "hi";
//^^^^ Variable cannot be reassigned from inflight
eladb marked this conversation as resolved.
Show resolved Hide resolved

ylet = 456;
//^^^^ Variable is not reassignable
};
7 changes: 7 additions & 0 deletions examples/tests/valid/custom_obj_id.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Foo { }

let foo1 = new Foo();
let bar2 = new Foo() as "bar2";

assert(foo1.node.id == "Foo");
assert(bar2.node.id == "bar2");
52 changes: 41 additions & 11 deletions libs/wingc/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@ impl UserDefinedType {
pub fn full_path_str(&self) -> String {
self.full_path().iter().join(".")
}

pub fn to_expression(&self) -> Expr {
Expr::new(
ExprKind::Reference(Reference::TypeReference(self.clone())),
self.span.clone(),
)
}
}

impl Display for UserDefinedType {
Expand Down Expand Up @@ -319,11 +326,30 @@ pub struct Class {
pub methods: Vec<(Symbol, FunctionDefinition)>,
pub initializer: FunctionDefinition,
pub inflight_initializer: FunctionDefinition,
pub parent: Option<UserDefinedType>,
pub parent: Option<Expr>, // the expression must be a reference to a user defined type
pub implements: Vec<UserDefinedType>,
pub phase: Phase,
}

impl Class {
/// Returns the `UserDefinedType` of the parent class, if any.
pub fn parent_udt(&self) -> Option<UserDefinedType> {
let Some(expr) = &self.parent else {
return None;
};

let ExprKind::Reference(ref r) = expr.kind else {
return None;
};

let Reference::TypeReference(t) = r else {
return None;
};

Some(t.clone())
}
}

#[derive(Debug)]
pub struct Interface {
pub name: Symbol,
Expand Down Expand Up @@ -372,7 +398,7 @@ pub enum StmtKind {
Return(Option<Expr>),
Expression(Expr),
Assignment {
variable: Reference,
variable: Expr,
value: Expr,
},
Scope(Scope),
Expand Down Expand Up @@ -419,7 +445,7 @@ pub struct StructField {
#[derive(Debug)]
pub enum ExprKind {
New {
class: TypeAnnotation,
class: Box<Expr>, // expression must be a reference to a user defined type
obj_id: Option<Box<Expr>>,
obj_scope: Option<Box<Expr>>,
arg_list: ArgList,
Expand Down Expand Up @@ -592,8 +618,12 @@ pub enum Reference {
property: Symbol,
optional_accessor: bool,
},
TypeReference(UserDefinedType),
eladb marked this conversation as resolved.
Show resolved Hide resolved
/// A reference to a member inside a type: `MyType.x` or `MyEnum.A`
TypeMember { type_: UserDefinedType, property: Symbol },
TypeMember {
typeobject: Box<Expr>,
property: Symbol,
},
}

impl Display for Reference {
Expand All @@ -611,13 +641,13 @@ impl Display for Reference {
};
write!(f, "{}.{}", obj_str, property.name)
}
Reference::TypeMember { type_, property } => {
write!(
f,
"{}.{}",
TypeAnnotationKind::UserDefined(type_.clone()),
property.name
)
Reference::TypeReference(type_) => write!(f, "{}", type_),
Reference::TypeMember { typeobject, property } => {
let ExprKind::Reference(ref r) = typeobject.kind else {
return write!(f, "<?>.{}", property.name);
};

write!(f, "{}.{}", r, property.name)
}
}
}
Expand Down
51 changes: 29 additions & 22 deletions libs/wingc/src/closure_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,14 @@ impl Fold for ClosureTransformer {
span: WingSpan::default(),
};

let class_udt = UserDefinedType {
root: new_class_name.clone(),
fields: vec![],
span: WingSpan::default(),
};

let class_type_annotation = TypeAnnotation {
kind: TypeAnnotationKind::UserDefined(UserDefinedType {
root: new_class_name.clone(),
fields: vec![],
span: WingSpan::default(),
}),
kind: TypeAnnotationKind::UserDefined(class_udt.clone()),
span: WingSpan::default(),
};

Expand Down Expand Up @@ -177,21 +179,24 @@ impl Fold for ClosureTransformer {
let class_init_body = vec![Stmt {
idx: 0,
kind: StmtKind::Assignment {
variable: Reference::InstanceMember {
object: Box::new(Expr::new(
ExprKind::Reference(Reference::InstanceMember {
object: Box::new(Expr::new(
ExprKind::Reference(Reference::Identifier(Symbol::new("this", WingSpan::default()))),
WingSpan::default(),
)),
property: Symbol::new("display", WingSpan::default()),
optional_accessor: false,
}),
WingSpan::default(),
)),
property: Symbol::new("hidden", WingSpan::default()),
optional_accessor: false,
},
variable: Expr::new(
ExprKind::Reference(Reference::InstanceMember {
object: Box::new(Expr::new(
ExprKind::Reference(Reference::InstanceMember {
object: Box::new(Expr::new(
ExprKind::Reference(Reference::Identifier(Symbol::new("this", WingSpan::default()))),
WingSpan::default(),
)),
property: Symbol::new("display", WingSpan::default()),
optional_accessor: false,
}),
WingSpan::default(),
)),
property: Symbol::new("hidden", WingSpan::default()),
optional_accessor: false,
}),
WingSpan::default(),
),
value: Expr::new(ExprKind::Literal(Literal::Boolean(true)), WingSpan::default()),
},
span: WingSpan::default(),
Expand Down Expand Up @@ -272,7 +277,7 @@ impl Fold for ClosureTransformer {
// ```
let new_class_instance = Expr::new(
ExprKind::New {
class: class_type_annotation,
class: Box::new(class_udt.to_expression()),
arg_list: ArgList {
named_args: IndexMap::new(),
pos_args: vec![],
Expand Down Expand Up @@ -333,7 +338,9 @@ impl<'a> Fold for RenameThisTransformer<'a> {
Reference::Identifier(ident)
}
}
Reference::InstanceMember { .. } | Reference::TypeMember { .. } => fold::fold_reference(self, node),
Reference::InstanceMember { .. } | Reference::TypeMember { .. } | Reference::TypeReference(_) => {
fold::fold_reference(self, node)
}
}
}
}
4 changes: 2 additions & 2 deletions libs/wingc/src/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
jsify::codemaker::CodeMaker,
type_check::{
jsii_importer::is_construct_base, Class, FunctionSignature, Interface, Namespace, Struct, SymbolKind, Type,
TypeRef, VariableInfo,
TypeRef, VariableInfo, VariableKind,
},
};

Expand Down Expand Up @@ -94,7 +94,7 @@ impl Documented for VariableInfo {
fn render_docs(&self) -> String {
let mut modifiers = vec![];

if self.is_member && self.is_static {
if let VariableKind::StaticMember = self.kind {
modifiers.push("static");
}

Expand Down
11 changes: 6 additions & 5 deletions libs/wingc/src/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ where
StmtKind::Return(value) => StmtKind::Return(value.map(|value| f.fold_expr(value))),
StmtKind::Expression(expr) => StmtKind::Expression(f.fold_expr(expr)),
StmtKind::Assignment { variable, value } => StmtKind::Assignment {
variable: f.fold_reference(variable),
variable: f.fold_expr(variable),
value: f.fold_expr(value),
},
StmtKind::Scope(scope) => StmtKind::Scope(f.fold_scope(scope)),
Expand Down Expand Up @@ -193,7 +193,7 @@ where
.map(|(name, def)| (f.fold_symbol(name), f.fold_function_definition(def)))
.collect(),
initializer: f.fold_function_definition(node.initializer),
parent: node.parent.map(|parent| f.fold_user_defined_type(parent)),
parent: node.parent.map(|parent| f.fold_expr(parent)),
implements: node
.implements
.into_iter()
Expand Down Expand Up @@ -257,7 +257,7 @@ where
obj_scope,
arg_list,
} => ExprKind::New {
class: f.fold_type_annotation(class),
class: Box::new(f.fold_expr(*class)),
obj_id,
obj_scope: obj_scope.map(|scope| Box::new(f.fold_expr(*scope))),
arg_list: f.fold_args(arg_list),
Expand Down Expand Up @@ -364,8 +364,9 @@ where
property: f.fold_symbol(property),
optional_accessor,
},
Reference::TypeMember { type_, property } => Reference::TypeMember {
type_: f.fold_user_defined_type(type_),
Reference::TypeReference(udt) => Reference::TypeReference(f.fold_user_defined_type(udt)),
Reference::TypeMember { typeobject, property } => Reference::TypeMember {
typeobject: Box::new(f.fold_expr(*typeobject)),
property: f.fold_symbol(property),
},
}
Expand Down
Loading
Loading