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

Support constant string concatenation #3318

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
193 changes: 189 additions & 4 deletions compiler-core/generated/schema_capnp.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions compiler-core/schema.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ struct Constant {
typ @12 :Type;
constructor @13 :ValueConstructor;
}

stringConcatenation :group {
left @14 :Constant;
right @15 :Constant;
}
}
}

Expand Down
11 changes: 9 additions & 2 deletions compiler-core/src/ast/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ pub enum Constant<T, RecordTag> {
typ: T,
},

StringConcatenation {
location: SrcSpan,
left: Box<Self>,
right: Box<Self>,
},

/// A placeholder constant used to allow module analysis to continue
/// even when there are type errors. Should never end up in generated code.
Invalid {
Expand All @@ -68,7 +74,7 @@ impl TypedConstant {
match self {
Constant::Int { .. } => type_::int(),
Constant::Float { .. } => type_::float(),
Constant::String { .. } => type_::string(),
Constant::String { .. } | Constant::StringConcatenation { .. } => type_::string(),
Constant::BitArray { .. } => type_::bits(),
Constant::Tuple { elements, .. } => {
type_::tuple(elements.iter().map(|e| e.type_()).collect())
Expand Down Expand Up @@ -98,7 +104,8 @@ impl<A, B> Constant<A, B> {
| Constant::Record { location, .. }
| Constant::BitArray { location, .. }
| Constant::Var { location, .. }
| Constant::Invalid { location, .. } => *location,
| Constant::Invalid { location, .. }
| Constant::StringConcatenation { location, .. } => *location,
}
}

Expand Down
33 changes: 33 additions & 0 deletions compiler-core/src/ast_folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,12 @@ pub trait UntypedConstantFolder {
typ: (),
} => self.fold_constant_var(location, module, name),

Constant::StringConcatenation {
location,
left,
right,
} => self.fold_constant_string_concatenation(location, left, right),

Constant::Invalid { location, typ: () } => self.fold_constant_invalid(location),
}
}
Expand Down Expand Up @@ -955,6 +961,19 @@ pub trait UntypedConstantFolder {
}
}

fn fold_constant_string_concatenation(
&mut self,
location: SrcSpan,
left: Box<UntypedConstant>,
right: Box<UntypedConstant>,
) -> UntypedConstant {
Constant::StringConcatenation {
location,
left,
right,
}
}

fn fold_constant_invalid(&mut self, location: SrcSpan) -> UntypedConstant {
Constant::Invalid { location, typ: () }
}
Expand Down Expand Up @@ -1022,6 +1041,20 @@ pub trait UntypedConstantFolder {
.collect();
Constant::BitArray { location, segments }
}

Constant::StringConcatenation {
location,
left,
right,
} => {
let left = Box::new(self.fold_constant(*left));
let right = Box::new(self.fold_constant(*right));
Constant::StringConcatenation {
location,
left,
right,
}
}
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions compiler-core/src/call_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,11 @@ impl<'a> CallGraphBuilder<'a> {
self.constant(&segment.value);
}
}

Constant::StringConcatenation { left, right, .. } => {
self.constant(left);
self.constant(right);
}
}
}
}
Expand Down
Loading