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

Allow prefixes in any call path. #472

Merged
merged 43 commits into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
9496e18
Test case and parsing.
Dec 15, 2021
476b498
Better test case.
Dec 17, 2021
834ae3b
Merge with master.
Dec 22, 2021
4bf0f3a
First pass as structs.
Dec 22, 2021
b24da59
CallPath keeps track of if its an absolute path or not.
Dec 22, 2021
9443f4c
Merge branch 'master' into emilyaherbert-75/allow-prefixes-in-any-cal…
adlerjohn Dec 30, 2021
6402fcc
Merge with master.
Jan 4, 2022
5f9be03
Merge with master.
Jan 5, 2022
5c547a9
Merge.
Jan 5, 2022
dd15e78
Update license.
Jan 5, 2022
5bcee46
Better test case.
Jan 5, 2022
acbf2e4
Merge remote-tracking branch 'origin/master' into emilyaherbert-75/al…
Jan 5, 2022
160f0cd
Implement for enums.
Jan 6, 2022
e362840
Merge with master.
Jan 6, 2022
a3a100d
Merge with master.
Jan 13, 2022
7dc4ba5
Fix bug created during merge with master.
Jan 13, 2022
0470e66
Better test case.
Jan 13, 2022
5205bd5
Fix clippy error.
Jan 13, 2022
772f7c8
Implement for functions.
Jan 14, 2022
832cc60
Merge with master.
Jan 14, 2022
42cd037
Fix merge mistake.
Jan 18, 2022
55d30a4
Merge remote-tracking branch 'origin/master' into emilyaherbert-75/al…
Jan 18, 2022
f72bc54
Fix bug.
Jan 18, 2022
af1d428
Fix cargo suggestion.
Jan 19, 2022
a62f440
Remove part of test case due to OOM error.
Jan 19, 2022
d9c0044
Turn silent mode back on.
Jan 19, 2022
0a23497
Merge with master.
Jan 21, 2022
f6a2982
Clippy suggestions.
Jan 21, 2022
e5a43fa
Pin lib version (#689)
adlerjohn Jan 23, 2022
3c29feb
Merge remote-tracking branch 'origin/master' into emilyaherbert-75/al…
Jan 24, 2022
154e2de
Add clippy allow.
Jan 24, 2022
46b0517
Clippy suggestions.
Jan 24, 2022
5882284
Clippy suggestions.
Jan 24, 2022
0332f7c
Clippy suggestions.
Jan 24, 2022
fd6a093
Merge remote-tracking branch 'origin/master' into emilyaherbert-75/al…
Jan 24, 2022
9f7409e
PR review feedback.
Feb 1, 2022
6ef76ff
Merge with master.
Feb 1, 2022
fc25386
Merge with master.
Feb 8, 2022
2df4f63
Add verbose back.
Feb 8, 2022
804eb3c
Merge with master.
Feb 22, 2022
37e4b92
Add unintended test.
Feb 22, 2022
51aa7c0
Update test case.
Feb 23, 2022
91fe472
Merge with master.
Feb 23, 2022
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
3 changes: 1 addition & 2 deletions sway-core/src/asm_generation/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,7 @@ pub(crate) fn convert_expression_to_asm(
}
// ABI casts are purely compile-time constructs and generate no corresponding bytecode
TypedExpressionVariant::AbiCast { .. } => ok(vec![], warnings, errors),
a => {
println!("unimplemented: {:?}", a);
_ => {
errors.push(CompileError::Unimplemented(
"ASM generation has not yet been implemented for this.",
exp.span.clone(),
Expand Down
6 changes: 4 additions & 2 deletions sway-core/src/control_flow_analysis/dead_code_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,9 @@ fn connect_trait_declaration(
) {
graph.namespace.add_trait(
CallPath {
suffix: decl.name.clone(),
prefixes: vec![],
suffix: decl.name.clone(),
is_absolute: false,
},
entry_node,
);
Expand All @@ -506,8 +507,9 @@ fn connect_abi_declaration(
) {
graph.namespace.add_trait(
CallPath {
suffix: decl.name.clone(),
prefixes: vec![],
suffix: decl.name.clone(),
is_absolute: false,
},
entry_node,
);
Expand Down
37 changes: 34 additions & 3 deletions sway-core/src/parse_tree/call_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ use pest::iterators::Pair;
pub struct CallPath {
pub prefixes: Vec<Ident>,
pub suffix: Ident,
// If `is_absolute` is true, then this call path is an absolute path from
// the project root namespace. If not, then it is relative to the current namespace.
pub(crate) is_absolute: bool,
}

impl std::convert::From<Ident> for CallPath {
fn from(other: Ident) -> Self {
CallPath {
prefixes: vec![],
suffix: other,
is_absolute: false,
}
}
}
Expand Down Expand Up @@ -66,10 +70,20 @@ impl CallPath {
pair: Pair<Rule>,
config: Option<&BuildConfig>,
) -> CompileResult<CallPath> {
assert!(pair.as_rule() == Rule::call_path || pair.as_rule() == Rule::call_path_);
emilyaherbert marked this conversation as resolved.
Show resolved Hide resolved
let mut warnings = vec![];
let mut errors = vec![];
let mut pairs_buf = vec![];
for pair in pair.into_inner() {
let stmt = pair.into_inner().next().unwrap();
let is_absolute = stmt.as_rule() == Rule::absolute_call_path
|| stmt.as_rule() == Rule::absolute_call_path_;
let stmt = stmt.into_inner();
let it = if is_absolute {
stmt.skip(1)
} else {
stmt.skip(0)
};
for pair in it {
if pair.as_rule() != Rule::path_separator {
pairs_buf.push(check!(
ident::parse_from_pair(pair, config),
Expand All @@ -83,7 +97,24 @@ impl CallPath {
let suffix = pairs_buf.pop().unwrap();
let prefixes = pairs_buf;

// TODO eventually we want to be able to call methods with colon-delineated syntax
ok(CallPath { prefixes, suffix }, warnings, errors)
ok(
CallPath {
prefixes,
suffix,
is_absolute,
},
warnings,
errors,
)
}

pub(crate) fn friendly_name(&self) -> String {
let mut buf = String::new();
for prefix in self.prefixes.iter() {
buf.push_str(prefix.as_str());
buf.push_str("::");
}
emilyaherbert marked this conversation as resolved.
Show resolved Hide resolved
buf.push_str(self.suffix.as_str());
buf
}
}
2 changes: 1 addition & 1 deletion sway-core/src/parse_tree/declaration/impl_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl ImplTrait {
let impl_keyword = iter.next().unwrap();
assert_eq!(impl_keyword.as_str(), "impl");
let trait_name = iter.next().unwrap();
assert_eq!(trait_name.as_rule(), Rule::trait_name);
assert_eq!(trait_name.as_rule(), Rule::call_path);
emilyaherbert marked this conversation as resolved.
Show resolved Hide resolved
let trait_name = check!(
CallPath::parse_from_pair(trait_name, config),
return err(warnings, errors),
Expand Down
1 change: 1 addition & 0 deletions sway-core/src/parse_tree/declaration/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ impl StructDeclaration {
span: name.as_span(),
path,
};

let name = check!(
ident::parse_from_pair(name, config),
return err(warnings, errors),
Expand Down
1 change: 0 additions & 1 deletion sway-core/src/parse_tree/expression/method_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ pub enum MethodName {
call_path: CallPath,
// if this is `None`, then use the first argument to determine the type
type_name: Option<TypeInfo>,
is_absolute: bool,
},
/// Represents a method lookup that does not contain any types in the path
FromModule { method_name: Ident },
Expand Down
84 changes: 53 additions & 31 deletions sway-core/src/parse_tree/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub enum Expression {
span: Span,
},
StructExpression {
struct_name: Ident,
struct_name: CallPath,
fields: Vec<StructExpressionField>,
span: Span,
},
Expand Down Expand Up @@ -218,9 +218,9 @@ impl Expression {
span: span.clone(),
}
.to_var_name(),
is_absolute: true,
},
type_name: None,
is_absolute: true,
},
arguments,
span,
Expand All @@ -236,9 +236,9 @@ impl Expression {
Ident::new_with_override("ops", span.clone()),
],
suffix: op.to_var_name(),
is_absolute: true,
},
type_name: None,
is_absolute: true,
},
arguments,
span,
Expand Down Expand Up @@ -394,17 +394,13 @@ impl Expression {
};
let mut func_app_parts = expr.into_inner();
let first_part = func_app_parts.next().unwrap();
assert!(first_part.as_rule() == Rule::ident);
let suffix = check!(
ident::parse_from_pair(first_part, config),
assert!(first_part.as_rule() == Rule::call_path);
let name = check!(
CallPath::parse_from_pair(first_part, config),
return err(warnings, errors),
warnings,
errors
);
let name = CallPath {
prefixes: vec![],
suffix,
};
let (arguments, type_args) = {
let maybe_type_args = func_app_parts.next().unwrap();
match maybe_type_args.as_rule() {
Expand Down Expand Up @@ -531,7 +527,7 @@ impl Expression {
let mut expr_iter = expr.into_inner();
let struct_name = expr_iter.next().unwrap();
let struct_name = check!(
ident::parse_from_pair(struct_name, config),
CallPath::parse_from_pair(struct_name, config),
return err(warnings, errors),
warnings,
errors
Expand Down Expand Up @@ -744,16 +740,16 @@ impl Expression {
}
}
Rule::fully_qualified_method => {
let mut path_parts_buf = vec![];
let mut call_path = None;
let mut type_name = None;
let mut method_name = None;
let mut arguments = None;
for pair in pair.into_inner() {
match pair.as_rule() {
Rule::path_separator => (),
Rule::path_ident => {
path_parts_buf.push(check!(
ident::parse_from_pair(pair, config),
Rule::call_path => {
call_path = Some(check!(
CallPath::parse_from_pair(pair, config),
continue,
warnings,
errors
Expand Down Expand Up @@ -781,22 +777,48 @@ impl Expression {
errors
);

// parse the method name into a call path
let method_name = MethodName::FromType {
call_path: CallPath {
prefixes: path_parts_buf,
suffix: check!(
ident::parse_from_pair(
method_name.expect("guaranteed by grammar"),
config
),
return err(warnings, errors),
warnings,
errors
),
},
type_name: Some(type_name),
is_absolute: false,
let method_name = match call_path {
Some(call_path) => {
let mut call_path_buf = call_path.prefixes;
call_path_buf.push(call_path.suffix);

// parse the method name into a call path
MethodName::FromType {
call_path: CallPath {
prefixes: call_path_buf,
suffix: check!(
ident::parse_from_pair(
method_name.expect("guaranteed by grammar"),
config
),
return err(warnings, errors),
warnings,
errors
),
is_absolute: call_path.is_absolute, //is_absolute: false,
},
type_name: Some(type_name),
}
}
None => {
// parse the method name into a call path
MethodName::FromType {
call_path: CallPath {
prefixes: vec![],
suffix: check!(
ident::parse_from_pair(
method_name.expect("guaranteed by grammar"),
config
),
return err(warnings, errors),
warnings,
errors
),
is_absolute: false, //is_absolute: false,
},
type_name: Some(type_name),
}
}
};

let mut arguments_buf = vec![];
Expand Down
1 change: 1 addition & 0 deletions sway-core/src/parse_tree/expression/unary_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ impl UnaryOp {
Ident::new_with_override("ops", op_span.clone()),
],
suffix: Ident::new_with_override(self.to_var_name(), op_span),
is_absolute: false,
},
arguments: vec![arg],
span,
Expand Down
Loading