|
|
@@ -19,10 +19,10 @@ use crate::sema::unused_variable::check_function_call;
|
|
|
use crate::sema::{builtin, using};
|
|
|
use crate::Target;
|
|
|
use num_bigint::{BigInt, Sign};
|
|
|
-use solang_parser::diagnostics::Diagnostic;
|
|
|
+use solang_parser::diagnostics::{Diagnostic, Note};
|
|
|
use solang_parser::pt;
|
|
|
use solang_parser::pt::{CodeLocation, Loc, Visibility};
|
|
|
-use std::collections::HashMap;
|
|
|
+use std::collections::{HashMap, HashSet};
|
|
|
|
|
|
/// Resolve a function call via function type
|
|
|
/// Function types do not have names so call cannot be using named parameters
|
|
|
@@ -38,6 +38,14 @@ pub(super) fn call_function_type(
|
|
|
diagnostics: &mut Diagnostics,
|
|
|
resolve_to: ResolveTo,
|
|
|
) -> Result<Expression, ()> {
|
|
|
+ if context.constant {
|
|
|
+ diagnostics.push(Diagnostic::error(
|
|
|
+ *loc,
|
|
|
+ "cannot call function in constant expression".to_string(),
|
|
|
+ ));
|
|
|
+ return Err(());
|
|
|
+ }
|
|
|
+
|
|
|
let mut function = expression(expr, context, ns, symtable, diagnostics, ResolveTo::Unknown)?;
|
|
|
|
|
|
let mut ty = function.ty();
|
|
|
@@ -190,14 +198,6 @@ pub fn available_functions(
|
|
|
) -> Vec<usize> {
|
|
|
let mut list = Vec::new();
|
|
|
|
|
|
- if global {
|
|
|
- if let Some(Symbol::Function(v)) =
|
|
|
- ns.function_symbols.get(&(file_no, None, name.to_owned()))
|
|
|
- {
|
|
|
- list.extend(v.iter().map(|(_, func_no)| *func_no));
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
if let Some(contract_no) = contract_no {
|
|
|
list.extend(
|
|
|
ns.contracts[contract_no]
|
|
|
@@ -205,9 +205,19 @@ pub fn available_functions(
|
|
|
.keys()
|
|
|
.filter(|func_no| ns.functions[**func_no].id.name == name)
|
|
|
.filter_map(|func_no| {
|
|
|
- let is_abstract = ns.functions[*func_no].is_virtual
|
|
|
- && !ns.contracts[contract_no].is_concrete();
|
|
|
- if ns.functions[*func_no].has_body || is_abstract {
|
|
|
+ let func = &ns.functions[*func_no];
|
|
|
+
|
|
|
+ // For a virtual function, only the most-overriden is available
|
|
|
+ if func.is_virtual
|
|
|
+ && ns.contracts[contract_no].virtual_functions[&func.signature].last()
|
|
|
+ != Some(func_no)
|
|
|
+ {
|
|
|
+ return None;
|
|
|
+ }
|
|
|
+
|
|
|
+ let is_abstract = func.is_virtual && !ns.contracts[contract_no].is_concrete();
|
|
|
+
|
|
|
+ if func.has_body || is_abstract {
|
|
|
return Some(*func_no);
|
|
|
}
|
|
|
None
|
|
|
@@ -215,12 +225,22 @@ pub fn available_functions(
|
|
|
);
|
|
|
}
|
|
|
|
|
|
+ // global functions may shadowed by contract-defined functions
|
|
|
+ if list.is_empty() && global {
|
|
|
+ if let Some(Symbol::Function(v)) =
|
|
|
+ ns.function_symbols.get(&(file_no, None, name.to_owned()))
|
|
|
+ {
|
|
|
+ list.extend(v.iter().map(|(_, func_no)| *func_no));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
list
|
|
|
}
|
|
|
|
|
|
/// Create a list of functions that can be called via super
|
|
|
pub fn available_super_functions(name: &str, contract_no: usize, ns: &Namespace) -> Vec<usize> {
|
|
|
let mut list = Vec::new();
|
|
|
+ let mut signatures = HashSet::new();
|
|
|
|
|
|
for base_contract_no in ns.contract_bases(contract_no).into_iter().rev() {
|
|
|
if base_contract_no == contract_no {
|
|
|
@@ -235,7 +255,11 @@ pub fn available_super_functions(name: &str, contract_no: usize, ns: &Namespace)
|
|
|
let func = &ns.functions[*func_no];
|
|
|
|
|
|
if func.id.name == name && func.has_body {
|
|
|
- Some(*func_no)
|
|
|
+ if func.is_virtual && !signatures.insert(&func.signature) {
|
|
|
+ None
|
|
|
+ } else {
|
|
|
+ Some(*func_no)
|
|
|
+ }
|
|
|
} else {
|
|
|
None
|
|
|
}
|
|
|
@@ -252,7 +276,7 @@ pub fn function_call_pos_args(
|
|
|
id: &pt::IdentifierPath,
|
|
|
func_ty: pt::FunctionTy,
|
|
|
args: &[pt::Expression],
|
|
|
- function_nos: Vec<usize>,
|
|
|
+ mut function_nos: Vec<usize>,
|
|
|
virtual_call: bool,
|
|
|
context: &mut ExprContext,
|
|
|
ns: &mut Namespace,
|
|
|
@@ -260,9 +284,6 @@ pub fn function_call_pos_args(
|
|
|
symtable: &mut Symtable,
|
|
|
diagnostics: &mut Diagnostics,
|
|
|
) -> Result<Expression, ()> {
|
|
|
- let mut name_matches = 0;
|
|
|
- let mut errors = Diagnostics::default();
|
|
|
-
|
|
|
if context.constant {
|
|
|
diagnostics.push(Diagnostic::error(
|
|
|
*loc,
|
|
|
@@ -271,51 +292,70 @@ pub fn function_call_pos_args(
|
|
|
return Err(());
|
|
|
}
|
|
|
|
|
|
+ // try to resolve the arguments, give up if there are any errors
|
|
|
+ if args.iter().fold(false, |acc, arg| {
|
|
|
+ acc | expression(arg, context, ns, symtable, diagnostics, ResolveTo::Unknown).is_err()
|
|
|
+ }) {
|
|
|
+ return Err(());
|
|
|
+ }
|
|
|
+
|
|
|
+ function_nos.retain(|function_no| ns.functions[*function_no].ty == func_ty);
|
|
|
+
|
|
|
+ let mut call_diagnostics = Diagnostics::default();
|
|
|
+ let mut resolved_calls = Vec::new();
|
|
|
+
|
|
|
// Try to resolve as a function call
|
|
|
for function_no in &function_nos {
|
|
|
let func = &ns.functions[*function_no];
|
|
|
|
|
|
- if func.ty != func_ty {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- name_matches += 1;
|
|
|
-
|
|
|
- let params_len = func.params.len();
|
|
|
+ let mut candidate_diagnostics = Diagnostics::default();
|
|
|
+ let mut cast_args = Vec::new();
|
|
|
|
|
|
- if params_len != args.len() {
|
|
|
- errors.push(Diagnostic::error(
|
|
|
+ if func.params.len() != args.len() {
|
|
|
+ candidate_diagnostics.push(Diagnostic::error(
|
|
|
*loc,
|
|
|
format!(
|
|
|
"{} expects {} arguments, {} provided",
|
|
|
func.ty,
|
|
|
- params_len,
|
|
|
+ func.params.len(),
|
|
|
args.len()
|
|
|
),
|
|
|
));
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- let mut matches = true;
|
|
|
- let mut cast_args = Vec::new();
|
|
|
-
|
|
|
- // check if arguments can be implicitly casted
|
|
|
- for (i, arg) in args.iter().enumerate() {
|
|
|
- let ty = ns.functions[*function_no].params[i].ty.clone();
|
|
|
+ } else {
|
|
|
+ // check if arguments can be implicitly casted
|
|
|
+ for (i, arg) in args.iter().enumerate() {
|
|
|
+ let ty = ns.functions[*function_no].params[i].ty.clone();
|
|
|
|
|
|
- matches &=
|
|
|
- evaluate_argument(arg, context, ns, symtable, &ty, &mut errors, &mut cast_args);
|
|
|
+ evaluate_argument(
|
|
|
+ arg,
|
|
|
+ context,
|
|
|
+ ns,
|
|
|
+ symtable,
|
|
|
+ &ty,
|
|
|
+ &mut candidate_diagnostics,
|
|
|
+ &mut cast_args,
|
|
|
+ );
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- if !matches {
|
|
|
- if function_nos.len() > 1 && diagnostics.extend_non_casting(&errors) {
|
|
|
- return Err(());
|
|
|
- }
|
|
|
+ if candidate_diagnostics.any_errors() {
|
|
|
+ if function_nos.len() != 1 {
|
|
|
+ // will be de-duped
|
|
|
+ candidate_diagnostics.push(Diagnostic::error(
|
|
|
+ *loc,
|
|
|
+ format!("cannot find overloaded {func_ty} which matches signature"),
|
|
|
+ ));
|
|
|
|
|
|
- continue;
|
|
|
- }
|
|
|
+ let func = &ns.functions[*function_no];
|
|
|
|
|
|
- match resolve_internal_call(
|
|
|
+ candidate_diagnostics.iter_mut().for_each(|diagnostic| {
|
|
|
+ diagnostic.notes.push(Note {
|
|
|
+ loc: func.loc,
|
|
|
+ message: "candidate function".into(),
|
|
|
+ })
|
|
|
+ });
|
|
|
+ }
|
|
|
+ } else if let Some(resolved_call) = resolve_internal_call(
|
|
|
loc,
|
|
|
id,
|
|
|
*function_no,
|
|
|
@@ -324,38 +364,56 @@ pub fn function_call_pos_args(
|
|
|
virtual_call,
|
|
|
cast_args,
|
|
|
ns,
|
|
|
- &mut errors,
|
|
|
+ &mut candidate_diagnostics,
|
|
|
) {
|
|
|
- Some(resolved_call) => return Ok(resolved_call),
|
|
|
- None => continue,
|
|
|
+ resolved_calls.push((*function_no, resolved_call));
|
|
|
+ continue;
|
|
|
}
|
|
|
+
|
|
|
+ call_diagnostics.extend(candidate_diagnostics);
|
|
|
}
|
|
|
|
|
|
let id = id.identifiers.last().unwrap();
|
|
|
- match name_matches {
|
|
|
+ match resolved_calls.len() {
|
|
|
0 => {
|
|
|
- if func_ty == pt::FunctionTy::Modifier {
|
|
|
- diagnostics.push(Diagnostic::error(
|
|
|
- id.loc,
|
|
|
- format!("unknown modifier '{}'", id.name),
|
|
|
- ));
|
|
|
- } else {
|
|
|
- diagnostics.push(Diagnostic::error(
|
|
|
- id.loc,
|
|
|
- format!("unknown {} or type '{}'", func_ty, id.name),
|
|
|
- ));
|
|
|
+ diagnostics.extend(call_diagnostics);
|
|
|
+
|
|
|
+ if function_nos.is_empty() {
|
|
|
+ if func_ty == pt::FunctionTy::Modifier {
|
|
|
+ diagnostics.push(Diagnostic::error(
|
|
|
+ id.loc,
|
|
|
+ format!("unknown modifier '{}'", id.name),
|
|
|
+ ));
|
|
|
+ } else {
|
|
|
+ diagnostics.push(Diagnostic::error(
|
|
|
+ id.loc,
|
|
|
+ format!("unknown {} or type '{}'", func_ty, id.name),
|
|
|
+ ));
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
+ Err(())
|
|
|
}
|
|
|
- 1 => diagnostics.extend(errors),
|
|
|
+ 1 => Ok(resolved_calls[0].1.clone()),
|
|
|
_ => {
|
|
|
- diagnostics.push(Diagnostic::error(
|
|
|
+ diagnostics.push(Diagnostic::error_with_notes(
|
|
|
*loc,
|
|
|
- format!("cannot find overloaded {func_ty} which matches signature"),
|
|
|
+ "function call can be resolved to multiple functions".into(),
|
|
|
+ resolved_calls
|
|
|
+ .iter()
|
|
|
+ .map(|(func_no, _)| {
|
|
|
+ let func = &ns.functions[*func_no];
|
|
|
+
|
|
|
+ Note {
|
|
|
+ loc: func.loc,
|
|
|
+ message: "candidate function".into(),
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .collect(),
|
|
|
));
|
|
|
+ Err(())
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- Err(())
|
|
|
}
|
|
|
|
|
|
/// Resolve a function call with named arguments
|
|
|
@@ -363,7 +421,7 @@ pub(super) fn function_call_named_args(
|
|
|
loc: &pt::Loc,
|
|
|
id: &pt::IdentifierPath,
|
|
|
args: &[pt::NamedArgument],
|
|
|
- function_nos: Vec<usize>,
|
|
|
+ mut function_nos: Vec<usize>,
|
|
|
virtual_call: bool,
|
|
|
context: &mut ExprContext,
|
|
|
resolve_to: ResolveTo,
|
|
|
@@ -373,7 +431,7 @@ pub(super) fn function_call_named_args(
|
|
|
) -> Result<Expression, ()> {
|
|
|
let mut arguments = HashMap::new();
|
|
|
|
|
|
- for arg in args {
|
|
|
+ if args.iter().fold(false, |mut acc, arg| {
|
|
|
if arguments.contains_key(arg.name.name.as_str()) {
|
|
|
diagnostics.push(Diagnostic::error(
|
|
|
arg.name.loc,
|
|
|
@@ -388,27 +446,43 @@ pub(super) fn function_call_named_args(
|
|
|
diagnostics,
|
|
|
ResolveTo::Unknown,
|
|
|
);
|
|
|
+ } else {
|
|
|
+ acc |= expression(
|
|
|
+ &arg.expr,
|
|
|
+ context,
|
|
|
+ ns,
|
|
|
+ symtable,
|
|
|
+ diagnostics,
|
|
|
+ ResolveTo::Unknown,
|
|
|
+ )
|
|
|
+ .is_err()
|
|
|
}
|
|
|
|
|
|
arguments.insert(arg.name.name.as_str(), &arg.expr);
|
|
|
+
|
|
|
+ acc
|
|
|
+ }) {
|
|
|
+ return Err(());
|
|
|
}
|
|
|
+
|
|
|
+ function_nos.retain(|function_no| ns.functions[*function_no].ty == pt::FunctionTy::Function);
|
|
|
+
|
|
|
// Try to resolve as a function call
|
|
|
- let mut errors = Diagnostics::default();
|
|
|
+ let mut call_diagnostics = Diagnostics::default();
|
|
|
+ let mut resolved_calls = Vec::new();
|
|
|
|
|
|
// Try to resolve as a function call
|
|
|
for function_no in &function_nos {
|
|
|
let func = &ns.functions[*function_no];
|
|
|
|
|
|
- if func.ty != pt::FunctionTy::Function {
|
|
|
- continue;
|
|
|
- }
|
|
|
+ let mut candidate_diagnostics = Diagnostics::default();
|
|
|
|
|
|
let unnamed_params = func.params.iter().filter(|p| p.id.is_none()).count();
|
|
|
let params_len = func.params.len();
|
|
|
- let mut matches = true;
|
|
|
+ let mut cast_args = Vec::new();
|
|
|
|
|
|
if unnamed_params > 0 {
|
|
|
- errors.push(Diagnostic::cast_error_with_note(
|
|
|
+ candidate_diagnostics.push(Diagnostic::cast_error_with_note(
|
|
|
*loc,
|
|
|
format!(
|
|
|
"function cannot be called with named arguments as {unnamed_params} of its parameters do not have names"
|
|
|
@@ -416,57 +490,71 @@ pub(super) fn function_call_named_args(
|
|
|
func.loc_prototype,
|
|
|
format!("definition of {}", func.id),
|
|
|
));
|
|
|
- matches = false;
|
|
|
- } else if params_len != args.len() {
|
|
|
- errors.push(Diagnostic::cast_error(
|
|
|
- *loc,
|
|
|
- format!(
|
|
|
- "function expects {} arguments, {} provided",
|
|
|
- params_len,
|
|
|
- args.len()
|
|
|
- ),
|
|
|
- ));
|
|
|
- matches = false;
|
|
|
- }
|
|
|
-
|
|
|
- let mut cast_args = Vec::new();
|
|
|
-
|
|
|
- // check if arguments can be implicitly casted
|
|
|
- for i in 0..params_len {
|
|
|
- let param = &ns.functions[*function_no].params[i];
|
|
|
- if param.id.is_none() {
|
|
|
- continue;
|
|
|
+ } else {
|
|
|
+ if params_len != args.len() {
|
|
|
+ candidate_diagnostics.push(Diagnostic::cast_error(
|
|
|
+ *loc,
|
|
|
+ format!(
|
|
|
+ "function expects {} arguments, {} provided",
|
|
|
+ params_len,
|
|
|
+ args.len()
|
|
|
+ ),
|
|
|
+ ));
|
|
|
}
|
|
|
- let arg = match arguments.get(param.name_as_str()) {
|
|
|
- Some(a) => a,
|
|
|
- None => {
|
|
|
- matches = false;
|
|
|
- diagnostics.push(Diagnostic::cast_error(
|
|
|
- *loc,
|
|
|
- format!(
|
|
|
- "missing argument '{}' to function '{}'",
|
|
|
- param.name_as_str(),
|
|
|
- id.identifiers.last().unwrap().name,
|
|
|
- ),
|
|
|
- ));
|
|
|
+
|
|
|
+ // check if arguments can be implicitly casted
|
|
|
+ for i in 0..params_len {
|
|
|
+ let param = &ns.functions[*function_no].params[i];
|
|
|
+ if param.id.is_none() {
|
|
|
continue;
|
|
|
}
|
|
|
- };
|
|
|
-
|
|
|
- let ty = param.ty.clone();
|
|
|
+ let arg = match arguments.get(param.name_as_str()) {
|
|
|
+ Some(a) => a,
|
|
|
+ None => {
|
|
|
+ candidate_diagnostics.push(Diagnostic::cast_error(
|
|
|
+ *loc,
|
|
|
+ format!(
|
|
|
+ "missing argument '{}' to function '{}'",
|
|
|
+ param.name_as_str(),
|
|
|
+ id,
|
|
|
+ ),
|
|
|
+ ));
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ };
|
|
|
|
|
|
- matches &=
|
|
|
- evaluate_argument(arg, context, ns, symtable, &ty, &mut errors, &mut cast_args);
|
|
|
- }
|
|
|
+ let ty = param.ty.clone();
|
|
|
|
|
|
- if !matches {
|
|
|
- if diagnostics.extend_non_casting(&errors) {
|
|
|
- return Err(());
|
|
|
+ evaluate_argument(
|
|
|
+ arg,
|
|
|
+ context,
|
|
|
+ ns,
|
|
|
+ symtable,
|
|
|
+ &ty,
|
|
|
+ &mut candidate_diagnostics,
|
|
|
+ &mut cast_args,
|
|
|
+ );
|
|
|
}
|
|
|
- continue;
|
|
|
}
|
|
|
|
|
|
- match resolve_internal_call(
|
|
|
+ if candidate_diagnostics.any_errors() {
|
|
|
+ if function_nos.len() != 1 {
|
|
|
+ // will be de-duped
|
|
|
+ candidate_diagnostics.push(Diagnostic::error(
|
|
|
+ *loc,
|
|
|
+ "cannot find overloaded function which matches signature".into(),
|
|
|
+ ));
|
|
|
+
|
|
|
+ let func = &ns.functions[*function_no];
|
|
|
+
|
|
|
+ candidate_diagnostics.iter_mut().for_each(|diagnostic| {
|
|
|
+ diagnostic.notes.push(Note {
|
|
|
+ loc: func.loc,
|
|
|
+ message: "candidate function".into(),
|
|
|
+ })
|
|
|
+ });
|
|
|
+ }
|
|
|
+ } else if let Some(resolved_call) = resolve_internal_call(
|
|
|
loc,
|
|
|
id,
|
|
|
*function_no,
|
|
|
@@ -475,31 +563,49 @@ pub(super) fn function_call_named_args(
|
|
|
virtual_call,
|
|
|
cast_args,
|
|
|
ns,
|
|
|
- &mut errors,
|
|
|
+ &mut candidate_diagnostics,
|
|
|
) {
|
|
|
- Some(resolved_call) => return Ok(resolved_call),
|
|
|
- None => continue,
|
|
|
+ resolved_calls.push((*function_no, resolved_call));
|
|
|
+ continue;
|
|
|
}
|
|
|
+
|
|
|
+ call_diagnostics.extend(candidate_diagnostics);
|
|
|
}
|
|
|
|
|
|
- match function_nos.len() {
|
|
|
+ match resolved_calls.len() {
|
|
|
0 => {
|
|
|
- let id = id.identifiers.last().unwrap();
|
|
|
- diagnostics.push(Diagnostic::error(
|
|
|
- id.loc,
|
|
|
- format!("unknown function or type '{}'", id.name),
|
|
|
- ));
|
|
|
+ diagnostics.extend(call_diagnostics);
|
|
|
+
|
|
|
+ if function_nos.is_empty() {
|
|
|
+ let id = id.identifiers.last().unwrap();
|
|
|
+ diagnostics.push(Diagnostic::error(
|
|
|
+ id.loc,
|
|
|
+ format!("unknown function or type '{}'", id.name),
|
|
|
+ ));
|
|
|
+ }
|
|
|
+
|
|
|
+ Err(())
|
|
|
}
|
|
|
- 1 => diagnostics.extend(errors),
|
|
|
+ 1 => Ok(resolved_calls[0].1.clone()),
|
|
|
_ => {
|
|
|
- diagnostics.push(Diagnostic::error(
|
|
|
+ diagnostics.push(Diagnostic::error_with_notes(
|
|
|
*loc,
|
|
|
- "cannot find overloaded function which matches signature".to_string(),
|
|
|
+ "function call can be resolved to multiple functions".into(),
|
|
|
+ resolved_calls
|
|
|
+ .iter()
|
|
|
+ .map(|(func_no, _)| {
|
|
|
+ let func = &ns.functions[*func_no];
|
|
|
+
|
|
|
+ Note {
|
|
|
+ loc: func.loc,
|
|
|
+ message: "candidate function".into(),
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .collect(),
|
|
|
));
|
|
|
+ Err(())
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- Err(())
|
|
|
}
|
|
|
|
|
|
/// Check if the function is a method of a variable
|
|
|
@@ -2340,14 +2446,6 @@ pub fn function_call_expr(
|
|
|
};
|
|
|
}
|
|
|
|
|
|
- if context.constant {
|
|
|
- diagnostics.push(Diagnostic::error(
|
|
|
- *loc,
|
|
|
- "cannot call function in constant expression".to_string(),
|
|
|
- ));
|
|
|
- return Err(());
|
|
|
- }
|
|
|
-
|
|
|
// is there a local variable or contract variable with this name
|
|
|
if symtable.find(context, &id.name).is_some()
|
|
|
|| matches!(
|
|
|
@@ -2514,7 +2612,7 @@ pub(crate) fn function_type(func: &Function, external: bool, resolve_to: Resolve
|
|
|
|
|
|
/// This function evaluates the arguments of a function call with either positional arguments or
|
|
|
/// named arguments.
|
|
|
-fn evaluate_argument(
|
|
|
+pub(crate) fn evaluate_argument(
|
|
|
arg: &pt::Expression,
|
|
|
context: &mut ExprContext,
|
|
|
ns: &mut Namespace,
|
|
|
@@ -2613,7 +2711,7 @@ fn contract_call_named_args(
|
|
|
let mut arguments = HashMap::new();
|
|
|
|
|
|
// check if the arguments are not garbage
|
|
|
- for arg in args {
|
|
|
+ if args.iter().fold(false, |mut acc, arg| {
|
|
|
if arguments.contains_key(arg.name.name.as_str()) {
|
|
|
diagnostics.push(Diagnostic::error(
|
|
|
arg.name.loc,
|
|
|
@@ -2628,11 +2726,23 @@ fn contract_call_named_args(
|
|
|
diagnostics,
|
|
|
ResolveTo::Unknown,
|
|
|
);
|
|
|
-
|
|
|
- continue;
|
|
|
+ } else {
|
|
|
+ acc |= expression(
|
|
|
+ &arg.expr,
|
|
|
+ context,
|
|
|
+ ns,
|
|
|
+ symtable,
|
|
|
+ diagnostics,
|
|
|
+ ResolveTo::Unknown,
|
|
|
+ )
|
|
|
+ .is_err()
|
|
|
}
|
|
|
|
|
|
arguments.insert(arg.name.name.as_str(), &arg.expr);
|
|
|
+
|
|
|
+ acc
|
|
|
+ }) {
|
|
|
+ return Err(());
|
|
|
}
|
|
|
|
|
|
let (call_args, name_matches) = match preprocess_contract_call(
|
|
|
@@ -2654,18 +2764,20 @@ fn contract_call_named_args(
|
|
|
PreProcessedCall::Error => return Err(()),
|
|
|
};
|
|
|
|
|
|
- let mut errors = Diagnostics::default();
|
|
|
+ let mut call_diagnostics = Diagnostics::default();
|
|
|
+ let mut resolved_calls = Vec::new();
|
|
|
|
|
|
for function_no in &name_matches {
|
|
|
let func = &ns.functions[*function_no];
|
|
|
|
|
|
+ let mut candidate_diagnostics = Diagnostics::default();
|
|
|
+
|
|
|
let unnamed_params = func.params.iter().filter(|p| p.id.is_none()).count();
|
|
|
let params_len = func.params.len();
|
|
|
-
|
|
|
- let mut matches = true;
|
|
|
+ let mut cast_args = Vec::new();
|
|
|
|
|
|
if unnamed_params > 0 {
|
|
|
- errors.push(Diagnostic::cast_error_with_note(
|
|
|
+ candidate_diagnostics.push(Diagnostic::cast_error_with_note(
|
|
|
*loc,
|
|
|
format!(
|
|
|
"function cannot be called with named arguments as {unnamed_params} of its parameters do not have names"
|
|
|
@@ -2673,9 +2785,8 @@ fn contract_call_named_args(
|
|
|
func.loc_prototype,
|
|
|
format!("definition of {}", func.id),
|
|
|
));
|
|
|
- matches = false;
|
|
|
} else if params_len != args.len() {
|
|
|
- errors.push(Diagnostic::cast_error(
|
|
|
+ candidate_diagnostics.push(Diagnostic::cast_error(
|
|
|
*loc,
|
|
|
format!(
|
|
|
"function expects {} arguments, {} provided",
|
|
|
@@ -2683,9 +2794,7 @@ fn contract_call_named_args(
|
|
|
args.len()
|
|
|
),
|
|
|
));
|
|
|
- matches = false;
|
|
|
}
|
|
|
- let mut cast_args = Vec::new();
|
|
|
|
|
|
for i in 0..params_len {
|
|
|
let param = ns.functions[*function_no].params[i].clone();
|
|
|
@@ -2696,8 +2805,7 @@ fn contract_call_named_args(
|
|
|
let arg = match arguments.get(param.name_as_str()) {
|
|
|
Some(a) => a,
|
|
|
None => {
|
|
|
- matches = false;
|
|
|
- diagnostics.push(Diagnostic::cast_error(
|
|
|
+ candidate_diagnostics.push(Diagnostic::cast_error(
|
|
|
*loc,
|
|
|
format!(
|
|
|
"missing argument '{}' to function '{}'",
|
|
|
@@ -2709,67 +2817,91 @@ fn contract_call_named_args(
|
|
|
}
|
|
|
};
|
|
|
|
|
|
- let arg = match expression(
|
|
|
+ let ty = param.ty.clone();
|
|
|
+
|
|
|
+ evaluate_argument(
|
|
|
arg,
|
|
|
context,
|
|
|
ns,
|
|
|
symtable,
|
|
|
- &mut errors,
|
|
|
- ResolveTo::Type(¶m.ty),
|
|
|
- ) {
|
|
|
- Ok(e) => e,
|
|
|
- Err(()) => {
|
|
|
- matches = false;
|
|
|
- continue;
|
|
|
- }
|
|
|
- };
|
|
|
+ &ty,
|
|
|
+ &mut candidate_diagnostics,
|
|
|
+ &mut cast_args,
|
|
|
+ );
|
|
|
+ }
|
|
|
|
|
|
- match arg.cast(&arg.loc(), ¶m.ty, true, ns, &mut errors) {
|
|
|
- Ok(expr) => cast_args.push(expr),
|
|
|
- Err(()) => {
|
|
|
- matches = false;
|
|
|
- break;
|
|
|
- }
|
|
|
+ if candidate_diagnostics.any_errors() {
|
|
|
+ if name_matches.len() != 1 {
|
|
|
+ // will be de-duped
|
|
|
+ candidate_diagnostics.push(Diagnostic::error(
|
|
|
+ *loc,
|
|
|
+ "cannot find overloaded function which matches signature".into(),
|
|
|
+ ));
|
|
|
+
|
|
|
+ let func = &ns.functions[*function_no];
|
|
|
+
|
|
|
+ candidate_diagnostics.iter_mut().for_each(|diagnostic| {
|
|
|
+ diagnostic.notes.push(Note {
|
|
|
+ loc: func.loc,
|
|
|
+ message: "candidate function".into(),
|
|
|
+ })
|
|
|
+ });
|
|
|
}
|
|
|
+ } else if let Ok(resolved_call) = contract_call_match(
|
|
|
+ loc,
|
|
|
+ func_name,
|
|
|
+ *function_no,
|
|
|
+ external_contract_no,
|
|
|
+ call_args.clone(),
|
|
|
+ cast_args,
|
|
|
+ var_expr.as_ref(),
|
|
|
+ ns,
|
|
|
+ &mut candidate_diagnostics,
|
|
|
+ resolve_to,
|
|
|
+ ) {
|
|
|
+ resolved_calls.push((*function_no, resolved_call));
|
|
|
+ continue;
|
|
|
}
|
|
|
|
|
|
- if matches {
|
|
|
- return contract_call_match(
|
|
|
- loc,
|
|
|
- func_name,
|
|
|
- *function_no,
|
|
|
- external_contract_no,
|
|
|
- call_args,
|
|
|
- cast_args,
|
|
|
- var_expr.as_ref(),
|
|
|
- ns,
|
|
|
- diagnostics,
|
|
|
- resolve_to,
|
|
|
- );
|
|
|
- } else if name_matches.len() > 1 && diagnostics.extend_non_casting(&errors) {
|
|
|
- return Err(());
|
|
|
- }
|
|
|
+ call_diagnostics.extend(candidate_diagnostics);
|
|
|
}
|
|
|
|
|
|
- match name_matches.len() {
|
|
|
+ match resolved_calls.len() {
|
|
|
0 => {
|
|
|
- diagnostics.push(Diagnostic::error(
|
|
|
- *loc,
|
|
|
- format!(
|
|
|
- "contract '{}' does not have function '{}'",
|
|
|
- ns.contracts[external_contract_no].id, func_name.name
|
|
|
- ),
|
|
|
- ));
|
|
|
+ diagnostics.extend(call_diagnostics);
|
|
|
+
|
|
|
+ if name_matches.is_empty() {
|
|
|
+ diagnostics.push(Diagnostic::error(
|
|
|
+ *loc,
|
|
|
+ format!(
|
|
|
+ "contract '{}' does not have function '{}'",
|
|
|
+ ns.contracts[external_contract_no].id, func_name.name
|
|
|
+ ),
|
|
|
+ ));
|
|
|
+ }
|
|
|
+
|
|
|
+ Err(())
|
|
|
}
|
|
|
- 1 => diagnostics.extend(errors),
|
|
|
+ 1 => Ok(resolved_calls[0].1.clone()),
|
|
|
_ => {
|
|
|
- diagnostics.push(Diagnostic::error(
|
|
|
+ diagnostics.push(Diagnostic::error_with_notes(
|
|
|
*loc,
|
|
|
- "cannot find overloaded function which matches signature".to_string(),
|
|
|
+ "function call can be resolved to multiple functions".into(),
|
|
|
+ resolved_calls
|
|
|
+ .iter()
|
|
|
+ .map(|(func_no, _)| {
|
|
|
+ let func = &ns.functions[*func_no];
|
|
|
+
|
|
|
+ Note {
|
|
|
+ loc: func.loc,
|
|
|
+ message: "candidate function".into(),
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .collect(),
|
|
|
));
|
|
|
+ Err(())
|
|
|
}
|
|
|
}
|
|
|
- Err(())
|
|
|
}
|
|
|
|
|
|
/// Resolve call to contract with positional arguments
|
|
|
@@ -2805,13 +2937,24 @@ fn contract_call_pos_args(
|
|
|
PreProcessedCall::Error => return Err(()),
|
|
|
};
|
|
|
|
|
|
- let mut errors = Diagnostics::default();
|
|
|
+ let mut call_diagnostics = Diagnostics::default();
|
|
|
+ let mut resolved_calls = Vec::new();
|
|
|
+
|
|
|
+ // try to resolve the arguments, give up if there are any errors
|
|
|
+ if args.iter().fold(false, |acc, arg| {
|
|
|
+ acc | expression(arg, context, ns, symtable, diagnostics, ResolveTo::Unknown).is_err()
|
|
|
+ }) {
|
|
|
+ return Err(());
|
|
|
+ }
|
|
|
|
|
|
for function_no in &name_matches {
|
|
|
+ let mut candidate_diagnostics = Diagnostics::default();
|
|
|
+ let mut cast_args = Vec::new();
|
|
|
+
|
|
|
let params_len = ns.functions[*function_no].params.len();
|
|
|
|
|
|
if params_len != args.len() {
|
|
|
- errors.push(Diagnostic::error(
|
|
|
+ candidate_diagnostics.push(Diagnostic::error(
|
|
|
*loc,
|
|
|
format!(
|
|
|
"function expects {} arguments, {} provided",
|
|
|
@@ -2819,92 +2962,116 @@ fn contract_call_pos_args(
|
|
|
args.len()
|
|
|
),
|
|
|
));
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- let mut matches = true;
|
|
|
- let mut cast_args = Vec::new();
|
|
|
-
|
|
|
- // check if arguments can be implicitly casted
|
|
|
- for (i, arg) in args.iter().enumerate() {
|
|
|
- let ty = ns.functions[*function_no].params[i].ty.clone();
|
|
|
-
|
|
|
- let arg = match expression(
|
|
|
- arg,
|
|
|
- context,
|
|
|
- ns,
|
|
|
- symtable,
|
|
|
- &mut errors,
|
|
|
- ResolveTo::Type(&ty),
|
|
|
- ) {
|
|
|
- Ok(e) => e,
|
|
|
- Err(_) => {
|
|
|
- matches = false;
|
|
|
- continue;
|
|
|
- }
|
|
|
- };
|
|
|
+ } else {
|
|
|
+ // check if arguments can be implicitly casted
|
|
|
+ for (i, arg) in args.iter().enumerate() {
|
|
|
+ let ty = ns.functions[*function_no].params[i].ty.clone();
|
|
|
|
|
|
- match arg.cast(&arg.loc(), &ty, true, ns, &mut errors) {
|
|
|
- Ok(expr) => cast_args.push(expr),
|
|
|
- Err(()) => {
|
|
|
- matches = false;
|
|
|
- continue;
|
|
|
- }
|
|
|
+ evaluate_argument(
|
|
|
+ arg,
|
|
|
+ context,
|
|
|
+ ns,
|
|
|
+ symtable,
|
|
|
+ &ty,
|
|
|
+ &mut candidate_diagnostics,
|
|
|
+ &mut cast_args,
|
|
|
+ );
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if matches {
|
|
|
- let resolved_call = contract_call_match(
|
|
|
- loc,
|
|
|
- func,
|
|
|
- *function_no,
|
|
|
- external_contract_no,
|
|
|
- call_args,
|
|
|
- cast_args,
|
|
|
- var_expr,
|
|
|
- ns,
|
|
|
- diagnostics,
|
|
|
- resolve_to,
|
|
|
- )?;
|
|
|
- return Ok(Some(resolved_call));
|
|
|
- } else if name_matches.len() > 1 && diagnostics.extend_non_casting(&errors) {
|
|
|
- return Err(());
|
|
|
- }
|
|
|
- }
|
|
|
+ if candidate_diagnostics.any_errors() {
|
|
|
+ if name_matches.len() != 1 {
|
|
|
+ candidate_diagnostics.iter_mut().for_each(|diagnostic| {
|
|
|
+ diagnostic.notes.push(Note {
|
|
|
+ loc: ns.functions[*function_no].loc_prototype,
|
|
|
+ message: "candidate function".into(),
|
|
|
+ })
|
|
|
+ });
|
|
|
|
|
|
- if let Some(var) = var_expr {
|
|
|
- // what about call args
|
|
|
- match using::try_resolve_using_call(
|
|
|
+ // will be de-duped
|
|
|
+ candidate_diagnostics.push(Diagnostic::error(
|
|
|
+ *loc,
|
|
|
+ "cannot find overloaded function which matches signature".into(),
|
|
|
+ ));
|
|
|
+ }
|
|
|
+ } else if let Ok(resolved_call) = contract_call_match(
|
|
|
loc,
|
|
|
func,
|
|
|
- var,
|
|
|
- context,
|
|
|
- args,
|
|
|
- symtable,
|
|
|
- diagnostics,
|
|
|
+ *function_no,
|
|
|
+ external_contract_no,
|
|
|
+ call_args.clone(),
|
|
|
+ cast_args,
|
|
|
+ var_expr,
|
|
|
ns,
|
|
|
+ &mut candidate_diagnostics,
|
|
|
resolve_to,
|
|
|
) {
|
|
|
- Ok(Some(expr)) => {
|
|
|
- return Ok(Some(expr));
|
|
|
+ resolved_calls.push((*function_no, resolved_call));
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ call_diagnostics.extend(candidate_diagnostics);
|
|
|
+ }
|
|
|
+
|
|
|
+ match resolved_calls.len() {
|
|
|
+ 0 => {
|
|
|
+ if let Some(var) = var_expr {
|
|
|
+ // what about call args
|
|
|
+ match using::try_resolve_using_call(
|
|
|
+ loc,
|
|
|
+ func,
|
|
|
+ var,
|
|
|
+ context,
|
|
|
+ args,
|
|
|
+ symtable,
|
|
|
+ diagnostics,
|
|
|
+ ns,
|
|
|
+ resolve_to,
|
|
|
+ ) {
|
|
|
+ Ok(Some(expr)) => {
|
|
|
+ return Ok(Some(expr));
|
|
|
+ }
|
|
|
+ Ok(None) => (),
|
|
|
+ Err(_) => {
|
|
|
+ return Err(());
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
- Ok(None) => (),
|
|
|
- Err(_) => {
|
|
|
- return Err(());
|
|
|
+
|
|
|
+ diagnostics.extend(call_diagnostics);
|
|
|
+
|
|
|
+ if name_matches.is_empty() {
|
|
|
+ diagnostics.push(Diagnostic::error(
|
|
|
+ func.loc,
|
|
|
+ format!("unknown function '{}'", func.name),
|
|
|
+ ));
|
|
|
}
|
|
|
+
|
|
|
+ Err(())
|
|
|
}
|
|
|
- }
|
|
|
+ 1 => Ok(Some(resolved_calls[0].1.clone())),
|
|
|
+ _ => {
|
|
|
+ diagnostics.extend(call_diagnostics);
|
|
|
|
|
|
- if name_matches.len() == 1 {
|
|
|
- diagnostics.extend(errors);
|
|
|
- } else if name_matches.len() != 1 {
|
|
|
- diagnostics.push(Diagnostic::error(
|
|
|
- *loc,
|
|
|
- "cannot find overloaded function which matches signature".to_string(),
|
|
|
- ));
|
|
|
- }
|
|
|
+ diagnostics.push(Diagnostic::error_with_notes(
|
|
|
+ *loc,
|
|
|
+ "function call can be resolved to multiple functions".into(),
|
|
|
+ resolved_calls
|
|
|
+ .iter()
|
|
|
+ .map(|(func_no, _)| {
|
|
|
+ let func = &ns.functions[*func_no];
|
|
|
+
|
|
|
+ Note {
|
|
|
+ loc: func.loc,
|
|
|
+ message: "candidate function".into(),
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .collect(),
|
|
|
+ ));
|
|
|
|
|
|
- Err(())
|
|
|
+ Err(())
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/// Checks if an identifier path is an external call on Solana.
|