| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657 |
- // SPDX-License-Identifier: Apache-2.0
- use crate::sema::ast::{
- ArrayLength, Builtin, CallArgs, CallTy, Expression, Function, Mutability, Namespace,
- RetrieveType, StructType, Symbol, Type,
- };
- use crate::sema::contracts::is_base;
- use crate::sema::diagnostics::Diagnostics;
- use crate::sema::expression::constructor::{deprecated_constructor_arguments, new};
- use crate::sema::expression::literals::{named_struct_literal, struct_literal};
- use crate::sema::expression::resolve_expression::expression;
- use crate::sema::expression::{ExprContext, ResolveTo};
- use crate::sema::format::string_format;
- use crate::sema::symtable::Symtable;
- use crate::sema::unused_variable::check_function_call;
- use crate::sema::{builtin, using};
- use crate::Target;
- use solang_parser::diagnostics::Diagnostic;
- use solang_parser::pt;
- use solang_parser::pt::{CodeLocation, Loc, Visibility};
- use std::collections::HashMap;
- /// Resolve a function call via function type
- /// Function types do not have names so call cannot be using named parameters
- pub(super) fn call_function_type(
- loc: &pt::Loc,
- expr: &pt::Expression,
- args: &[pt::Expression],
- call_args: &[&pt::NamedArgument],
- call_args_loc: Option<pt::Loc>,
- context: &ExprContext,
- ns: &mut Namespace,
- symtable: &mut Symtable,
- diagnostics: &mut Diagnostics,
- resolve_to: ResolveTo,
- ) -> Result<Expression, ()> {
- let mut function = expression(expr, context, ns, symtable, diagnostics, ResolveTo::Unknown)?;
- let mut ty = function.ty();
- match ty {
- Type::StorageRef(_, real_ty) | Type::Ref(real_ty) => {
- ty = *real_ty;
- function = function.cast(&expr.loc(), &ty, true, ns, diagnostics)?;
- }
- _ => (),
- };
- if let Type::InternalFunction {
- params, returns, ..
- } = ty
- {
- if let Some(loc) = call_args_loc {
- diagnostics.push(Diagnostic::error(
- loc,
- "call arguments not permitted for internal calls".to_string(),
- ));
- }
- if params.len() != args.len() {
- diagnostics.push(Diagnostic::error(
- *loc,
- format!(
- "function expects {} arguments, {} provided",
- params.len(),
- args.len()
- ),
- ));
- return Err(());
- }
- let mut cast_args = Vec::new();
- // check if arguments can be implicitly casted
- for (i, arg) in args.iter().enumerate() {
- let arg = expression(
- arg,
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Type(¶ms[i]),
- )?;
- cast_args.push(arg.cast(&arg.loc(), ¶ms[i], true, ns, diagnostics)?);
- }
- Ok(Expression::InternalFunctionCall {
- loc: *loc,
- returns: if returns.is_empty() || resolve_to == ResolveTo::Discard {
- vec![Type::Void]
- } else {
- returns
- },
- function: Box::new(function),
- args: cast_args,
- })
- } else if let Type::ExternalFunction {
- returns,
- params,
- mutability,
- } = ty
- {
- let call_args = parse_call_args(loc, call_args, true, context, ns, symtable, diagnostics)?;
- if let Some(value) = &call_args.value {
- if !value.const_zero(ns) && !matches!(mutability, Mutability::Payable(_)) {
- diagnostics.push(Diagnostic::error(
- *loc,
- format!(
- "sending value to function type '{}' which is not payable",
- function.ty().to_string(ns),
- ),
- ));
- return Err(());
- }
- }
- if params.len() != args.len() {
- diagnostics.push(Diagnostic::error(
- *loc,
- format!(
- "function expects {} arguments, {} provided",
- params.len(),
- args.len()
- ),
- ));
- return Err(());
- }
- let mut cast_args = Vec::new();
- // check if arguments can be implicitly casted
- for (i, arg) in args.iter().enumerate() {
- let arg = expression(
- arg,
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Type(¶ms[i]),
- )?;
- cast_args.push(arg.cast(&arg.loc(), ¶ms[i], true, ns, diagnostics)?);
- }
- Ok(Expression::ExternalFunctionCall {
- loc: *loc,
- returns: if returns.is_empty() || resolve_to == ResolveTo::Discard {
- vec![Type::Void]
- } else {
- returns
- },
- function: Box::new(function),
- args: cast_args,
- call_args,
- })
- } else {
- diagnostics.push(Diagnostic::error(
- *loc,
- "expression is not a function".to_string(),
- ));
- Err(())
- }
- }
- /// Create a list of functions matching the given `name` that can be called in this context.
- /// A function is available if it has a body block or if its virtual and its contract is not concrete.
- ///
- /// If global is true, then include functions outside of contracts.
- pub fn available_functions(
- name: &str,
- global: bool,
- file_no: usize,
- contract_no: Option<usize>,
- ns: &Namespace,
- ) -> 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]
- .all_functions
- .keys()
- .filter(|func_no| ns.functions[**func_no].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 {
- return Some(*func_no);
- }
- None
- }),
- );
- }
- 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();
- for base_contract_no in ns.contract_bases(contract_no).into_iter().rev() {
- if base_contract_no == contract_no {
- continue;
- }
- list.extend(
- ns.contracts[base_contract_no]
- .all_functions
- .keys()
- .filter_map(|func_no| {
- let func = &ns.functions[*func_no];
- if func.name == name && func.has_body {
- Some(*func_no)
- } else {
- None
- }
- }),
- );
- }
- list
- }
- /// Resolve a function call with positional arguments
- pub fn function_call_pos_args(
- loc: &pt::Loc,
- id: &pt::Identifier,
- func_ty: pt::FunctionTy,
- args: &[pt::Expression],
- function_nos: Vec<usize>,
- virtual_call: bool,
- context: &ExprContext,
- ns: &mut Namespace,
- resolve_to: ResolveTo,
- 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,
- "cannot call function in constant expression".to_string(),
- ));
- return Err(());
- }
- // 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();
- if params_len != args.len() {
- errors.push(Diagnostic::error(
- *loc,
- format!(
- "{} expects {} arguments, {} provided",
- func.ty,
- 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();
- matches &=
- evaluate_argument(arg, context, ns, symtable, &ty, &mut errors, &mut cast_args);
- }
- if !matches {
- if function_nos.len() > 1 && diagnostics.extend_non_casting(&errors) {
- return Err(());
- }
- continue;
- }
- match resolve_internal_call(
- loc,
- *function_no,
- context,
- resolve_to,
- virtual_call,
- cast_args,
- ns,
- &mut errors,
- ) {
- Some(resolved_call) => return Ok(resolved_call),
- None => continue,
- }
- }
- match name_matches {
- 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),
- ));
- }
- }
- 1 => diagnostics.extend(errors),
- _ => {
- diagnostics.push(Diagnostic::error(
- *loc,
- format!("cannot find overloaded {func_ty} which matches signature"),
- ));
- }
- }
- Err(())
- }
- /// Resolve a function call with named arguments
- pub(super) fn function_call_named_args(
- loc: &pt::Loc,
- id: &pt::Identifier,
- args: &[pt::NamedArgument],
- function_nos: Vec<usize>,
- virtual_call: bool,
- context: &ExprContext,
- resolve_to: ResolveTo,
- ns: &mut Namespace,
- symtable: &mut Symtable,
- diagnostics: &mut Diagnostics,
- ) -> Result<Expression, ()> {
- let mut arguments = HashMap::new();
- for arg in args {
- if arguments.contains_key(arg.name.name.as_str()) {
- diagnostics.push(Diagnostic::error(
- arg.name.loc,
- format!("duplicate argument with name '{}'", arg.name.name),
- ));
- let _ = expression(
- &arg.expr,
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Unknown,
- );
- }
- arguments.insert(arg.name.name.as_str(), &arg.expr);
- }
- // Try to resolve as a function call
- let mut errors = Diagnostics::default();
- // 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 unnamed_params = func.params.iter().filter(|p| p.id.is_none()).count();
- let params_len = func.params.len();
- let mut matches = true;
- if unnamed_params > 0 {
- errors.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"
- ),
- func.loc,
- format!("definition of {}", func.name),
- ));
- 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;
- }
- 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.name,
- ),
- ));
- continue;
- }
- };
- let ty = param.ty.clone();
- matches &=
- evaluate_argument(arg, context, ns, symtable, &ty, &mut errors, &mut cast_args);
- }
- if !matches {
- if diagnostics.extend_non_casting(&errors) {
- return Err(());
- }
- continue;
- }
- match resolve_internal_call(
- loc,
- *function_no,
- context,
- resolve_to,
- virtual_call,
- cast_args,
- ns,
- &mut errors,
- ) {
- Some(resolved_call) => return Ok(resolved_call),
- None => continue,
- }
- }
- match function_nos.len() {
- 0 => {
- diagnostics.push(Diagnostic::error(
- id.loc,
- format!("unknown function or type '{}'", id.name),
- ));
- }
- 1 => diagnostics.extend(errors),
- _ => {
- diagnostics.push(Diagnostic::error(
- *loc,
- "cannot find overloaded function which matches signature".to_string(),
- ));
- }
- }
- Err(())
- }
- /// Check if the function is a method of a variable
- /// Returns:
- /// 1. Err, when there is an error
- /// 2. Ok(Some()), when we have indeed received a method of a variable
- /// 3. Ok(None), when we have not received a function that is a method of a variable
- fn try_namespace(
- loc: &pt::Loc,
- var: &pt::Expression,
- func: &pt::Identifier,
- args: &[pt::Expression],
- call_args_loc: Option<pt::Loc>,
- context: &ExprContext,
- ns: &mut Namespace,
- symtable: &mut Symtable,
- diagnostics: &mut Diagnostics,
- resolve_to: ResolveTo,
- ) -> Result<Option<Expression>, ()> {
- if let pt::Expression::Variable(namespace) = var {
- if builtin::is_builtin_call(Some(&namespace.name), &func.name, ns) {
- if let Some(loc) = call_args_loc {
- diagnostics.push(Diagnostic::error(
- loc,
- "call arguments not allowed on builtins".to_string(),
- ));
- return Err(());
- }
- return Ok(Some(builtin::resolve_namespace_call(
- loc,
- &namespace.name,
- &func.name,
- args,
- context,
- ns,
- symtable,
- diagnostics,
- )?));
- }
- // is it a call to super
- if namespace.name == "super" {
- if let Some(cur_contract_no) = context.contract_no {
- if let Some(loc) = call_args_loc {
- diagnostics.push(Diagnostic::error(
- loc,
- "call arguments not allowed on super calls".to_string(),
- ));
- return Err(());
- }
- return Ok(Some(function_call_pos_args(
- loc,
- func,
- pt::FunctionTy::Function,
- args,
- available_super_functions(&func.name, cur_contract_no, ns),
- false,
- context,
- ns,
- resolve_to,
- symtable,
- diagnostics,
- )?));
- } else {
- diagnostics.push(Diagnostic::error(
- *loc,
- "super not available outside contracts".to_string(),
- ));
- return Err(());
- }
- }
- // library or base contract call
- if let Some(call_contract_no) = ns.resolve_contract(context.file_no, namespace) {
- if ns.contracts[call_contract_no].is_library() {
- if let Some(loc) = call_args_loc {
- diagnostics.push(Diagnostic::error(
- loc,
- "call arguments not allowed on library calls".to_string(),
- ));
- return Err(());
- }
- return Ok(Some(function_call_pos_args(
- loc,
- func,
- pt::FunctionTy::Function,
- args,
- available_functions(
- &func.name,
- false,
- context.file_no,
- Some(call_contract_no),
- ns,
- ),
- true,
- context,
- ns,
- resolve_to,
- symtable,
- diagnostics,
- )?));
- }
- // is a base contract of us
- if let Some(contract_no) = context.contract_no {
- if is_base(call_contract_no, contract_no, ns) {
- if let Some(loc) = call_args_loc {
- diagnostics.push(Diagnostic::error(
- loc,
- "call arguments not allowed on internal calls".to_string(),
- ));
- return Err(());
- }
- return Ok(Some(function_call_pos_args(
- loc,
- func,
- pt::FunctionTy::Function,
- args,
- available_functions(
- &func.name,
- false,
- context.file_no,
- Some(call_contract_no),
- ns,
- ),
- false,
- context,
- ns,
- resolve_to,
- symtable,
- diagnostics,
- )?));
- } else {
- diagnostics.push(Diagnostic::error(
- *loc,
- "function calls via contract name are only valid for base contracts".into(),
- ));
- }
- }
- }
- }
- Ok(None)
- }
- /// Check if the function is a method of a storage reference
- /// Returns:
- /// 1. Err, when there is an error
- /// 2. Ok(Some()), when we have indeed received a method of a storage reference
- /// 3. Ok(None), when we have not received a function that is a method of a storage reference
- fn try_storage_reference(
- loc: &pt::Loc,
- var_expr: &Expression,
- func: &pt::Identifier,
- args: &[pt::Expression],
- context: &ExprContext,
- diagnostics: &mut Diagnostics,
- call_args_loc: Option<pt::Loc>,
- ns: &mut Namespace,
- symtable: &mut Symtable,
- resolve_to: &ResolveTo,
- ) -> Result<Option<Expression>, ()> {
- if let Type::StorageRef(immutable, ty) = &var_expr.ty() {
- match ty.as_ref() {
- Type::Array(_, dim) => {
- if *immutable {
- if let Some(function_no) = context.function_no {
- if !ns.functions[function_no].is_constructor() {
- diagnostics.push(Diagnostic::error(
- *loc,
- "cannot call method on immutable array outside of constructor"
- .to_string(),
- ));
- return Err(());
- }
- }
- }
- if let Some(loc) = call_args_loc {
- diagnostics.push(Diagnostic::error(
- loc,
- "call arguments not allowed on arrays".to_string(),
- ));
- return Err(());
- }
- if func.name == "push" {
- if matches!(dim.last(), Some(ArrayLength::Fixed(_))) {
- diagnostics.push(Diagnostic::error(
- func.loc,
- "method 'push()' not allowed on fixed length array".to_string(),
- ));
- return Err(());
- }
- let elem_ty = ty.array_elem();
- let mut builtin_args = vec![var_expr.clone()];
- let ret_ty = match args.len() {
- 1 => {
- let expr = expression(
- &args[0],
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Type(&elem_ty),
- )?;
- builtin_args.push(expr.cast(
- &args[0].loc(),
- &elem_ty,
- true,
- ns,
- diagnostics,
- )?);
- Type::Void
- }
- 0 => {
- if elem_ty.is_reference_type(ns) {
- Type::StorageRef(false, Box::new(elem_ty))
- } else {
- elem_ty
- }
- }
- _ => {
- diagnostics.push(Diagnostic::error(
- func.loc,
- "method 'push()' takes at most 1 argument".to_string(),
- ));
- return Err(());
- }
- };
- return Ok(Some(Expression::Builtin {
- loc: func.loc,
- tys: vec![ret_ty],
- kind: Builtin::ArrayPush,
- args: builtin_args,
- }));
- }
- if func.name == "pop" {
- if matches!(dim.last(), Some(ArrayLength::Fixed(_))) {
- diagnostics.push(Diagnostic::error(
- func.loc,
- "method 'pop()' not allowed on fixed length array".to_string(),
- ));
- return Err(());
- }
- if !args.is_empty() {
- diagnostics.push(Diagnostic::error(
- func.loc,
- "method 'pop()' does not take any arguments".to_string(),
- ));
- return Err(());
- }
- let storage_elem = ty.storage_array_elem();
- let elem_ty = storage_elem.deref_any();
- let return_ty = if *resolve_to == ResolveTo::Discard {
- Type::Void
- } else {
- elem_ty.clone()
- };
- return Ok(Some(Expression::Builtin {
- loc: func.loc,
- tys: vec![return_ty],
- kind: Builtin::ArrayPop,
- args: vec![var_expr.clone()],
- }));
- }
- }
- Type::DynamicBytes => {
- if *immutable {
- if let Some(function_no) = context.function_no {
- if !ns.functions[function_no].is_constructor() {
- diagnostics.push(Diagnostic::error(
- *loc,
- "cannot call method on immutable bytes outside of constructor"
- .to_string(),
- ));
- return Err(());
- }
- }
- }
- if let Some(loc) = call_args_loc {
- diagnostics.push(Diagnostic::error(
- loc,
- "call arguments not allowed on bytes".to_string(),
- ));
- return Err(());
- }
- if func.name == "push" {
- let mut builtin_args = vec![var_expr.clone()];
- let elem_ty = Type::Bytes(1);
- let ret_ty = match args.len() {
- 1 => {
- let expr = expression(
- &args[0],
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Type(&elem_ty),
- )?;
- builtin_args.push(expr.cast(
- &args[0].loc(),
- &elem_ty,
- true,
- ns,
- diagnostics,
- )?);
- Type::Void
- }
- 0 => elem_ty,
- _ => {
- diagnostics.push(Diagnostic::error(
- func.loc,
- "method 'push()' takes at most 1 argument".to_string(),
- ));
- return Err(());
- }
- };
- return Ok(Some(Expression::Builtin {
- loc: func.loc,
- tys: vec![ret_ty],
- kind: Builtin::ArrayPush,
- args: builtin_args,
- }));
- }
- if func.name == "pop" {
- if !args.is_empty() {
- diagnostics.push(Diagnostic::error(
- func.loc,
- "method 'pop()' does not take any arguments".to_string(),
- ));
- return Err(());
- }
- return Ok(Some(Expression::Builtin {
- loc: func.loc,
- tys: vec![Type::Bytes(1)],
- kind: Builtin::ArrayPop,
- args: vec![var_expr.clone()],
- }));
- }
- }
- _ => {}
- }
- }
- Ok(None)
- }
- /// Check if we can resolve the call with ns.resolve_type
- /// Returns:
- /// 1. Err, when there is an error
- /// 2. Ok(Some()), when we have indeed received a method that has correctly been resolved with
- /// ns.resolve_type
- /// 3. Ok(None), when the function we have received could not be resolved with ns.resolve_type
- fn try_user_type(
- loc: &pt::Loc,
- var: &pt::Expression,
- func: &pt::Identifier,
- args: &[pt::Expression],
- call_args_loc: Option<pt::Loc>,
- context: &ExprContext,
- ns: &mut Namespace,
- symtable: &mut Symtable,
- diagnostics: &mut Diagnostics,
- ) -> Result<Option<Expression>, ()> {
- if let Ok(Type::UserType(no)) = ns.resolve_type(
- context.file_no,
- context.contract_no,
- false,
- var,
- &mut Diagnostics::default(),
- ) {
- if let Some(loc) = call_args_loc {
- diagnostics.push(Diagnostic::error(
- loc,
- "call arguments not allowed on builtins".to_string(),
- ));
- return Err(());
- }
- let elem_ty = ns.user_types[no].ty.clone();
- let user_ty = Type::UserType(no);
- if func.name == "unwrap" {
- return if args.len() != 1 {
- diagnostics.push(Diagnostic::error(
- func.loc,
- "method 'unwrap()' takes one argument".to_string(),
- ));
- Err(())
- } else {
- let expr = expression(
- &args[0],
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Type(&user_ty),
- )?;
- Ok(Some(Expression::Builtin {
- loc: *loc,
- tys: vec![elem_ty],
- kind: Builtin::UserTypeUnwrap,
- args: vec![expr.cast(&expr.loc(), &user_ty, true, ns, diagnostics)?],
- }))
- };
- } else if func.name == "wrap" {
- return if args.len() != 1 {
- diagnostics.push(Diagnostic::error(
- func.loc,
- "method 'wrap()' takes one argument".to_string(),
- ));
- Err(())
- } else {
- let expr = expression(
- &args[0],
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Type(&elem_ty),
- )?;
- Ok(Some(Expression::Builtin {
- loc: *loc,
- tys: vec![user_ty],
- kind: Builtin::UserTypeWrap,
- args: vec![expr.cast(&expr.loc(), &elem_ty, true, ns, diagnostics)?],
- }))
- };
- }
- }
- Ok(None)
- }
- /// Check if the function call is to a type's method
- /// Returns:
- /// 1. Err, when there is an error
- /// 2. Ok(Some()), when we have indeed received a method of a type
- /// 3. Ok(None), when we have received a function that is not a method of a type
- fn try_type_method(
- loc: &pt::Loc,
- func: &pt::Identifier,
- var: &pt::Expression,
- args: &[pt::Expression],
- call_args: &[&pt::NamedArgument],
- call_args_loc: Option<pt::Loc>,
- context: &ExprContext,
- var_expr: &Expression,
- ns: &mut Namespace,
- symtable: &mut Symtable,
- diagnostics: &mut Diagnostics,
- resolve_to: ResolveTo,
- ) -> Result<Option<Expression>, ()> {
- let var_ty = var_expr.ty();
- match var_ty.deref_any() {
- Type::Bytes(..) | Type::String if func.name == "format" => {
- return if let pt::Expression::StringLiteral(bs) = var {
- if let Some(loc) = call_args_loc {
- diagnostics.push(Diagnostic::error(
- loc,
- "call arguments not allowed on builtins".to_string(),
- ));
- return Err(());
- }
- Ok(Some(string_format(
- loc,
- bs,
- args,
- context,
- ns,
- symtable,
- diagnostics,
- )?))
- } else {
- diagnostics.push(Diagnostic::error(
- *loc,
- "format only allowed on string literals".to_string(),
- ));
- Err(())
- };
- }
- Type::Array(..) | Type::DynamicBytes if var_ty.is_dynamic(ns) => {
- if func.name == "push" {
- let elem_ty = var_ty.array_elem();
- let val = match args.len() {
- 0 => {
- return Ok(Some(Expression::Builtin {
- loc: *loc,
- tys: vec![elem_ty.clone()],
- kind: Builtin::ArrayPush,
- args: vec![var_expr.clone()],
- }));
- }
- 1 => {
- let val_expr = expression(
- &args[0],
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Type(&elem_ty),
- )?;
- val_expr.cast(&args[0].loc(), &elem_ty, true, ns, diagnostics)?
- }
- _ => {
- diagnostics.push(Diagnostic::error(
- func.loc,
- "method 'push()' takes at most 1 argument".to_string(),
- ));
- return Err(());
- }
- };
- return Ok(Some(Expression::Builtin {
- loc: *loc,
- tys: vec![elem_ty.clone()],
- kind: Builtin::ArrayPush,
- args: vec![var_expr.clone(), val],
- }));
- }
- if func.name == "pop" {
- if !args.is_empty() {
- diagnostics.push(Diagnostic::error(
- func.loc,
- "method 'pop()' does not take any arguments".to_string(),
- ));
- return Err(());
- }
- let elem_ty = match &var_ty {
- Type::Array(ty, _) => ty,
- Type::DynamicBytes => &Type::Uint(8),
- _ => unreachable!(),
- };
- return Ok(Some(Expression::Builtin {
- loc: *loc,
- tys: vec![elem_ty.clone()],
- kind: Builtin::ArrayPop,
- args: vec![var_expr.clone()],
- }));
- }
- }
- Type::Array(..) if func.name == "push" || func.name == "pop" => {
- diagnostics.push(Diagnostic::error(
- func.loc,
- format!(
- "method {}() is not available for fixed length arrays",
- func.name
- ),
- ));
- return Err(());
- }
- Type::Contract(ext_contract_no) => {
- let call_args =
- parse_call_args(loc, call_args, true, context, ns, symtable, diagnostics)?;
- let mut errors = Diagnostics::default();
- let mut name_matches: Vec<usize> = Vec::new();
- for function_no in ns.contracts[*ext_contract_no].all_functions.keys() {
- if func.name != ns.functions[*function_no].name
- || ns.functions[*function_no].ty != pt::FunctionTy::Function
- {
- continue;
- }
- name_matches.push(*function_no);
- }
- for function_no in &name_matches {
- let params_len = ns.functions[*function_no].params.len();
- if params_len != args.len() {
- errors.push(Diagnostic::error(
- *loc,
- format!(
- "function expects {} arguments, {} provided",
- 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();
- let arg = match expression(
- arg,
- context,
- ns,
- symtable,
- &mut errors,
- ResolveTo::Type(&ty),
- ) {
- Ok(e) => e,
- Err(_) => {
- matches = false;
- continue;
- }
- };
- match arg.cast(&arg.loc(), &ty, true, ns, &mut errors) {
- Ok(expr) => cast_args.push(expr),
- Err(()) => {
- matches = false;
- continue;
- }
- }
- }
- if matches {
- if !ns.functions[*function_no].is_public() {
- diagnostics.push(Diagnostic::error(
- *loc,
- format!("function '{}' is not 'public' or 'external'", func.name),
- ));
- return Err(());
- }
- if let Some(value) = &call_args.value {
- if !value.const_zero(ns) && !ns.functions[*function_no].is_payable() {
- diagnostics.push(Diagnostic::error(
- *loc,
- format!(
- "sending value to function '{}' which is not payable",
- func.name
- ),
- ));
- return Err(());
- }
- }
- let func = &ns.functions[*function_no];
- let returns = function_returns(func, resolve_to);
- let ty = function_type(func, true, resolve_to);
- return Ok(Some(Expression::ExternalFunctionCall {
- loc: *loc,
- returns,
- function: Box::new(Expression::ExternalFunction {
- loc: *loc,
- ty,
- function_no: *function_no,
- address: Box::new(var_expr.cast(
- &var.loc(),
- &Type::Contract(func.contract_no.unwrap()),
- true,
- ns,
- diagnostics,
- )?),
- }),
- args: cast_args,
- call_args,
- }));
- } else if name_matches.len() > 1 && diagnostics.extend_non_casting(&errors) {
- return Err(());
- }
- }
- // what about call args
- match using::try_resolve_using_call(
- loc,
- func,
- var_expr,
- context,
- args,
- symtable,
- diagnostics,
- ns,
- resolve_to,
- ) {
- Ok(Some(expr)) => {
- return Ok(Some(expr));
- }
- Ok(None) => (),
- Err(_) => {
- return Err(());
- }
- }
- 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(),
- ));
- }
- return Err(());
- }
- Type::Address(is_payable) => {
- if func.name == "transfer" || func.name == "send" {
- if !is_payable {
- diagnostics.push(Diagnostic::error(
- *loc,
- format!(
- "method '{}' available on type 'address payable' not 'address'",
- func.name,
- ),
- ));
- return Err(());
- }
- if args.len() != 1 {
- diagnostics.push(Diagnostic::error(
- *loc,
- format!(
- "'{}' expects 1 argument, {} provided",
- func.name,
- args.len()
- ),
- ));
- return Err(());
- }
- if let Some(loc) = call_args_loc {
- diagnostics.push(Diagnostic::error(
- loc,
- format!("call arguments not allowed on '{}'", func.name),
- ));
- return Err(());
- }
- let expr = expression(
- &args[0],
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Type(&Type::Value),
- )?;
- let address =
- var_expr.cast(&var_expr.loc(), var_ty.deref_any(), true, ns, diagnostics)?;
- let value = expr.cast(&args[0].loc(), &Type::Value, true, ns, diagnostics)?;
- return if func.name == "transfer" {
- Ok(Some(Expression::Builtin {
- loc: *loc,
- tys: vec![Type::Void],
- kind: Builtin::PayableTransfer,
- args: vec![address, value],
- }))
- } else {
- Ok(Some(Expression::Builtin {
- loc: *loc,
- tys: vec![Type::Bool],
- kind: Builtin::PayableSend,
- args: vec![address, value],
- }))
- };
- }
- let ty = match func.name.as_str() {
- "call" => Some(CallTy::Regular),
- "delegatecall" if ns.target != Target::Solana => Some(CallTy::Delegate),
- "staticcall" if ns.target == Target::EVM => Some(CallTy::Static),
- _ => None,
- };
- if let Some(ty) = ty {
- let call_args =
- parse_call_args(loc, call_args, true, context, ns, symtable, diagnostics)?;
- if ty != CallTy::Regular && call_args.value.is_some() {
- diagnostics.push(Diagnostic::error(
- *loc,
- format!("'{}' cannot have value specified", func.name,),
- ));
- return Err(());
- }
- if ty == CallTy::Delegate && ns.target.is_polkadot() && call_args.gas.is_some() {
- diagnostics.push(Diagnostic::warning(
- *loc,
- "'gas' specified on 'delegatecall' will be ignored".into(),
- ));
- }
- if args.len() != 1 {
- diagnostics.push(Diagnostic::error(
- *loc,
- format!(
- "'{}' expects 1 argument, {} provided",
- func.name,
- args.len()
- ),
- ));
- return Err(());
- }
- let args = expression(
- &args[0],
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Type(&Type::DynamicBytes),
- )?;
- let mut args_ty = args.ty();
- match args_ty.deref_any() {
- Type::DynamicBytes => (),
- Type::Bytes(_) => {
- args_ty = Type::DynamicBytes;
- }
- Type::Array(..) | Type::Struct(..) if !args_ty.is_dynamic(ns) => {}
- _ => {
- diagnostics.push(Diagnostic::error(
- args.loc(),
- format!("'{}' is not fixed length type", args_ty.to_string(ns),),
- ));
- return Err(());
- }
- }
- let args = args.cast(&args.loc(), args_ty.deref_any(), true, ns, diagnostics)?;
- return Ok(Some(Expression::ExternalFunctionCallRaw {
- loc: *loc,
- ty,
- args: Box::new(args),
- address: Box::new(var_expr.cast(
- &var_expr.loc(),
- &Type::Address(*is_payable),
- true,
- ns,
- diagnostics,
- )?),
- call_args,
- }));
- }
- }
- _ => (),
- }
- Ok(None)
- }
- /// Resolve a method call with positional arguments
- pub(super) fn method_call_pos_args(
- loc: &pt::Loc,
- var: &pt::Expression,
- func: &pt::Identifier,
- args: &[pt::Expression],
- call_args: &[&pt::NamedArgument],
- call_args_loc: Option<pt::Loc>,
- context: &ExprContext,
- ns: &mut Namespace,
- symtable: &mut Symtable,
- diagnostics: &mut Diagnostics,
- resolve_to: ResolveTo,
- ) -> Result<Expression, ()> {
- if let Some(resolved_call) = try_namespace(
- loc,
- var,
- func,
- args,
- call_args_loc,
- context,
- ns,
- symtable,
- diagnostics,
- resolve_to,
- )? {
- return Ok(resolved_call);
- }
- if let Some(resolved_call) = try_user_type(
- loc,
- var,
- func,
- args,
- call_args_loc,
- context,
- ns,
- symtable,
- diagnostics,
- )? {
- return Ok(resolved_call);
- }
- if context.constant {
- diagnostics.push(Diagnostic::error(
- *loc,
- "cannot call function in constant expression".to_string(),
- ));
- return Err(());
- }
- if let Some(mut path) = ns.expr_to_identifier_path(var) {
- path.identifiers.push(func.clone());
- if let Ok(list) = ns.resolve_free_function_with_namespace(
- context.file_no,
- &path,
- &mut Diagnostics::default(),
- ) {
- if let Some(loc) = call_args_loc {
- diagnostics.push(Diagnostic::error(
- loc,
- "call arguments not allowed on internal calls".to_string(),
- ));
- }
- return function_call_pos_args(
- loc,
- func,
- pt::FunctionTy::Function,
- args,
- list.iter().map(|(_, no)| *no).collect(),
- false,
- context,
- ns,
- resolve_to,
- symtable,
- diagnostics,
- );
- }
- }
- let var_expr = expression(var, context, ns, symtable, diagnostics, ResolveTo::Unknown)?;
- if let Some(resolved_call) =
- builtin::resolve_method_call(&var_expr, func, args, context, ns, symtable, diagnostics)?
- {
- return Ok(resolved_call);
- }
- if let Some(resolved_call) = try_storage_reference(
- loc,
- &var_expr,
- func,
- args,
- context,
- diagnostics,
- call_args_loc,
- ns,
- symtable,
- &resolve_to,
- )? {
- return Ok(resolved_call);
- }
- let mut diagnostics_type: u8 = 0;
- let mut type_method_diagnostics = Diagnostics::default();
- match try_type_method(
- loc,
- func,
- var,
- args,
- call_args,
- call_args_loc,
- context,
- &var_expr,
- ns,
- symtable,
- &mut type_method_diagnostics,
- resolve_to,
- ) {
- Ok(Some(resolved_call)) => {
- diagnostics.extend(type_method_diagnostics);
- return Ok(resolved_call);
- }
- Ok(None) => (),
- Err(()) => {
- // Adding one means diagnostics from type method
- diagnostics_type += 1;
- }
- }
- let mut resolve_using_diagnostics = Diagnostics::default();
- // resolve it using library extension
- match using::try_resolve_using_call(
- loc,
- func,
- &var_expr,
- context,
- args,
- symtable,
- &mut resolve_using_diagnostics,
- ns,
- resolve_to,
- ) {
- Ok(Some(resolved_call)) => {
- diagnostics.extend(resolve_using_diagnostics);
- return Ok(resolved_call);
- }
- Ok(None) => (),
- Err(()) => {
- // Adding two means diagnostics from resolve_using
- diagnostics_type += 2;
- }
- }
- match diagnostics_type {
- 1 => diagnostics.extend(type_method_diagnostics),
- 2 => diagnostics.extend(resolve_using_diagnostics),
- // If 'diagnostics_type' is 3, we have errors from both type_method and resolve_using.
- _ => diagnostics.push(Diagnostic::error(
- func.loc,
- format!("method '{}' does not exist", func.name),
- )),
- }
- Err(())
- }
- pub(super) fn method_call_named_args(
- loc: &pt::Loc,
- var: &pt::Expression,
- func_name: &pt::Identifier,
- args: &[pt::NamedArgument],
- call_args: &[&pt::NamedArgument],
- call_args_loc: Option<pt::Loc>,
- context: &ExprContext,
- ns: &mut Namespace,
- symtable: &mut Symtable,
- diagnostics: &mut Diagnostics,
- resolve_to: ResolveTo,
- ) -> Result<Expression, ()> {
- if let pt::Expression::Variable(namespace) = var {
- // is it a call to super
- if namespace.name == "super" {
- if let Some(cur_contract_no) = context.contract_no {
- if let Some(loc) = call_args_loc {
- diagnostics.push(Diagnostic::error(
- loc,
- "call arguments not allowed on super calls".to_string(),
- ));
- return Err(());
- }
- return function_call_named_args(
- loc,
- func_name,
- args,
- available_super_functions(&func_name.name, cur_contract_no, ns),
- false,
- context,
- resolve_to,
- ns,
- symtable,
- diagnostics,
- );
- } else {
- diagnostics.push(Diagnostic::error(
- *loc,
- "super not available outside contracts".to_string(),
- ));
- return Err(());
- }
- }
- // library or base contract call
- if let Some(call_contract_no) = ns.resolve_contract(context.file_no, namespace) {
- if ns.contracts[call_contract_no].is_library() {
- if let Some(loc) = call_args_loc {
- diagnostics.push(Diagnostic::error(
- loc,
- "call arguments not allowed on library calls".to_string(),
- ));
- return Err(());
- }
- return function_call_named_args(
- loc,
- func_name,
- args,
- available_functions(
- &func_name.name,
- false,
- context.file_no,
- Some(call_contract_no),
- ns,
- ),
- true,
- context,
- resolve_to,
- ns,
- symtable,
- diagnostics,
- );
- }
- // is a base contract of us
- if let Some(contract_no) = context.contract_no {
- if is_base(call_contract_no, contract_no, ns) {
- if let Some(loc) = call_args_loc {
- diagnostics.push(Diagnostic::error(
- loc,
- "call arguments not allowed on internal calls".to_string(),
- ));
- return Err(());
- }
- return function_call_named_args(
- loc,
- func_name,
- args,
- available_functions(
- &func_name.name,
- false,
- context.file_no,
- Some(call_contract_no),
- ns,
- ),
- false,
- context,
- resolve_to,
- ns,
- symtable,
- diagnostics,
- );
- } else {
- diagnostics.push(Diagnostic::error(
- *loc,
- "function calls via contract name are only valid for base contracts".into(),
- ));
- }
- }
- }
- }
- if let Some(mut path) = ns.expr_to_identifier_path(var) {
- path.identifiers.push(func_name.clone());
- if let Ok(list) = ns.resolve_free_function_with_namespace(
- context.file_no,
- &path,
- &mut Diagnostics::default(),
- ) {
- if let Some(loc) = call_args_loc {
- diagnostics.push(Diagnostic::error(
- loc,
- "call arguments not allowed on internal calls".to_string(),
- ));
- }
- return function_call_named_args(
- loc,
- func_name,
- args,
- list.iter().map(|(_, no)| *no).collect(),
- false,
- context,
- resolve_to,
- ns,
- symtable,
- diagnostics,
- );
- }
- }
- let var_expr = expression(var, context, ns, symtable, diagnostics, ResolveTo::Unknown)?;
- let var_ty = var_expr.ty();
- if let Type::Contract(external_contract_no) = &var_ty.deref_any() {
- let call_args = parse_call_args(loc, call_args, true, context, ns, symtable, diagnostics)?;
- let mut arguments = HashMap::new();
- // check if the arguments are not garbage
- for arg in args {
- if arguments.contains_key(arg.name.name.as_str()) {
- diagnostics.push(Diagnostic::error(
- arg.name.loc,
- format!("duplicate argument with name '{}'", arg.name.name),
- ));
- let _ = expression(
- &arg.expr,
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Unknown,
- );
- continue;
- }
- arguments.insert(arg.name.name.as_str(), &arg.expr);
- }
- let mut errors = Diagnostics::default();
- let mut name_matches: Vec<usize> = Vec::new();
- // function call
- for function_no in ns.contracts[*external_contract_no].all_functions.keys() {
- if ns.functions[*function_no].name != func_name.name
- || ns.functions[*function_no].ty != pt::FunctionTy::Function
- {
- continue;
- }
- name_matches.push(*function_no);
- }
- for function_no in &name_matches {
- let func = &ns.functions[*function_no];
- let unnamed_params = func.params.iter().filter(|p| p.id.is_none()).count();
- let params_len = func.params.len();
- let mut matches = true;
- if unnamed_params > 0 {
- errors.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"
- ),
- func.loc,
- format!("definition of {}", func.name),
- ));
- 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();
- for i in 0..params_len {
- let param = ns.functions[*function_no].params[i].clone();
- if param.id.is_none() {
- continue;
- }
- 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(),
- func_name.name,
- ),
- ));
- continue;
- }
- };
- let arg = match expression(
- arg,
- context,
- ns,
- symtable,
- &mut errors,
- ResolveTo::Type(¶m.ty),
- ) {
- Ok(e) => e,
- Err(()) => {
- matches = false;
- continue;
- }
- };
- match arg.cast(&arg.loc(), ¶m.ty, true, ns, &mut errors) {
- Ok(expr) => cast_args.push(expr),
- Err(()) => {
- matches = false;
- break;
- }
- }
- }
- if matches {
- if !ns.functions[*function_no].is_public() {
- diagnostics.push(Diagnostic::error(
- *loc,
- format!(
- "function '{}' is not 'public' or 'external'",
- func_name.name
- ),
- ));
- } else if let Some(value) = &call_args.value {
- if !value.const_zero(ns) && !ns.functions[*function_no].is_payable() {
- diagnostics.push(Diagnostic::error(
- *loc,
- format!(
- "sending value to function '{}' which is not payable",
- func_name.name
- ),
- ));
- }
- }
- let func = &ns.functions[*function_no];
- let returns = function_returns(func, resolve_to);
- let ty = function_type(func, true, resolve_to);
- return Ok(Expression::ExternalFunctionCall {
- loc: *loc,
- returns,
- function: Box::new(Expression::ExternalFunction {
- loc: *loc,
- ty,
- function_no: *function_no,
- address: Box::new(var_expr.cast(
- &var.loc(),
- &Type::Contract(func.contract_no.unwrap()),
- true,
- ns,
- diagnostics,
- )?),
- }),
- args: cast_args,
- call_args,
- });
- } else if name_matches.len() > 1 && diagnostics.extend_non_casting(&errors) {
- return Err(());
- }
- }
- match name_matches.len() {
- 0 => {
- diagnostics.push(Diagnostic::error(
- *loc,
- format!(
- "contract '{}' does not have function '{}'",
- var_ty.deref_any().to_string(ns),
- func_name.name
- ),
- ));
- }
- 1 => diagnostics.extend(errors),
- _ => {
- diagnostics.push(Diagnostic::error(
- *loc,
- "cannot find overloaded function which matches signature".to_string(),
- ));
- }
- }
- return Err(());
- }
- diagnostics.push(Diagnostic::error(
- func_name.loc,
- format!("method '{}' does not exist", func_name.name),
- ));
- Err(())
- }
- /// Function call arguments
- pub fn collect_call_args<'a>(
- expr: &'a pt::Expression,
- diagnostics: &mut Diagnostics,
- ) -> Result<
- (
- &'a pt::Expression,
- Vec<&'a pt::NamedArgument>,
- Option<pt::Loc>,
- ),
- (),
- > {
- let mut named_arguments = Vec::new();
- let mut expr = expr;
- let mut loc: Option<pt::Loc> = None;
- while let pt::Expression::FunctionCallBlock(_, e, block) = expr {
- match block.as_ref() {
- pt::Statement::Args(_, args) => {
- if let Some(pt::Loc::File(file_no, start, _)) = loc {
- loc = Some(pt::Loc::File(file_no, start, block.loc().end()));
- } else {
- loc = Some(block.loc());
- }
- named_arguments.extend(args);
- }
- pt::Statement::Block { statements, .. } if statements.is_empty() => {
- // {}
- diagnostics.push(Diagnostic::error(
- block.loc(),
- "missing call arguments".to_string(),
- ));
- return Err(());
- }
- _ => {
- diagnostics.push(Diagnostic::error(
- block.loc(),
- "code block found where list of call arguments expected, like '{gas: 5000}'"
- .to_string(),
- ));
- return Err(());
- }
- }
- expr = e;
- }
- Ok((expr, named_arguments, loc))
- }
- /// Parse call arguments for external calls
- pub(super) fn parse_call_args(
- loc: &pt::Loc,
- call_args: &[&pt::NamedArgument],
- external_call: bool,
- context: &ExprContext,
- ns: &mut Namespace,
- symtable: &mut Symtable,
- diagnostics: &mut Diagnostics,
- ) -> Result<CallArgs, ()> {
- let mut args: HashMap<&String, &pt::NamedArgument> = HashMap::new();
- for arg in call_args {
- if let Some(prev) = args.get(&arg.name.name) {
- diagnostics.push(Diagnostic::error_with_note(
- arg.loc,
- format!("'{}' specified multiple times", arg.name.name),
- prev.loc,
- format!("location of previous declaration of '{}'", arg.name.name),
- ));
- return Err(());
- }
- args.insert(&arg.name.name, arg);
- }
- let mut res = CallArgs::default();
- for arg in args.values() {
- match arg.name.name.as_str() {
- "value" => {
- if ns.target == Target::Solana {
- diagnostics.push(Diagnostic::error(
- arg.loc,
- "Solana Cross Program Invocation (CPI) cannot transfer native value. See https://solang.readthedocs.io/en/latest/language/functions.html#value_transfer".to_string(),
- ));
- expression(
- &arg.expr,
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Unknown,
- )?;
- } else {
- let ty = Type::Value;
- let expr = expression(
- &arg.expr,
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Type(&ty),
- )?;
- res.value = Some(Box::new(expr.cast(
- &arg.expr.loc(),
- &ty,
- true,
- ns,
- diagnostics,
- )?));
- }
- }
- "gas" => {
- if ns.target == Target::Solana {
- diagnostics.push(Diagnostic::error(
- arg.loc,
- format!(
- "'gas' not permitted for external calls or constructors on {}",
- ns.target
- ),
- ));
- return Err(());
- }
- let ty = Type::Uint(64);
- let expr = expression(
- &arg.expr,
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Type(&ty),
- )?;
- res.gas = Some(Box::new(expr.cast(
- &arg.expr.loc(),
- &ty,
- true,
- ns,
- diagnostics,
- )?));
- }
- "salt" => {
- if ns.target == Target::Solana {
- diagnostics.push(Diagnostic::error(
- arg.loc,
- format!(
- "'salt' not permitted for external calls or constructors on {}",
- ns.target
- ),
- ));
- return Err(());
- }
- if external_call {
- diagnostics.push(Diagnostic::error(
- arg.loc,
- "'salt' not valid for external calls".to_string(),
- ));
- return Err(());
- }
- let ty = Type::Bytes(32);
- let expr = expression(
- &arg.expr,
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Type(&ty),
- )?;
- res.salt = Some(Box::new(expr.cast(
- &arg.expr.loc(),
- &ty,
- true,
- ns,
- diagnostics,
- )?));
- }
- "accounts" => {
- if ns.target != Target::Solana {
- diagnostics.push(Diagnostic::error(
- arg.loc,
- format!(
- "'accounts' not permitted for external calls or constructors on {}",
- ns.target
- ),
- ));
- return Err(());
- }
- let expr = expression(
- &arg.expr,
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Unknown,
- )?;
- let mut correct_ty = false;
- let expr_ty = expr.ty();
- // if let chains would really help here
- if let Type::Array(elem_ty, dims) = expr_ty.deref_memory() {
- if elem_ty.is_builtin_struct() == Some(StructType::AccountMeta)
- && dims.len() == 1
- {
- correct_ty = true;
- }
- }
- if !correct_ty {
- diagnostics.push(Diagnostic::error(
- arg.loc,
- format!(
- "'accounts' takes array of AccountMeta, not '{}'",
- expr_ty.to_string(ns)
- ),
- ));
- return Err(());
- } else if expr_ty.is_dynamic(ns) {
- diagnostics.push(Diagnostic::error(
- arg.loc,
- "dynamic array is not supported for the 'accounts' argument".to_string(),
- ));
- }
- res.accounts = Some(Box::new(expr));
- }
- "seeds" => {
- if ns.target != Target::Solana {
- diagnostics.push(Diagnostic::error(
- arg.loc,
- format!(
- "'seeds' not permitted for external calls or constructors on {}",
- ns.target
- ),
- ));
- return Err(());
- }
- let ty = Type::Slice(Box::new(Type::Slice(Box::new(Type::Bytes(1)))));
- let expr = expression(
- &arg.expr,
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Type(&ty),
- )?;
- res.seeds = Some(Box::new(expr));
- }
- "flags" => {
- if !(ns.target.is_polkadot() && external_call) {
- diagnostics.push(Diagnostic::error(
- arg.loc,
- "'flags' are only permitted for external calls on polkadot".into(),
- ));
- return Err(());
- }
- let ty = Type::Uint(32);
- let expr = expression(
- &arg.expr,
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Type(&ty),
- )?;
- let flags = expr.cast(&arg.expr.loc(), &ty, true, ns, diagnostics)?;
- res.flags = Some(flags.into());
- }
- _ => {
- diagnostics.push(Diagnostic::error(
- arg.loc,
- format!("'{}' not a valid call parameter", arg.name.name),
- ));
- return Err(());
- }
- }
- }
- // address is required on solana constructors
- if ns.target == Target::Solana
- && !external_call
- && res.accounts.is_none()
- && !matches!(
- ns.functions[context.function_no.unwrap()].visibility,
- Visibility::External(_)
- )
- && !ns.functions[context.function_no.unwrap()].is_constructor()
- {
- diagnostics.push(Diagnostic::error(
- *loc,
- "accounts are required for calling a contract. You can either provide the \
- accounts with the {accounts: ...} call argument or change this function's \
- visibility to external"
- .to_string(),
- ));
- return Err(());
- }
- Ok(res)
- }
- pub fn named_call_expr(
- loc: &pt::Loc,
- ty: &pt::Expression,
- args: &[pt::NamedArgument],
- is_destructible: bool,
- context: &ExprContext,
- ns: &mut Namespace,
- symtable: &mut Symtable,
- diagnostics: &mut Diagnostics,
- resolve_to: ResolveTo,
- ) -> Result<Expression, ()> {
- let mut nullsink = Diagnostics::default();
- // is it a struct literal
- match ns.resolve_type(
- context.file_no,
- context.contract_no,
- true,
- ty,
- &mut nullsink,
- ) {
- Ok(Type::Struct(str_ty)) => {
- return named_struct_literal(loc, &str_ty, args, context, ns, symtable, diagnostics);
- }
- Ok(_) => {
- diagnostics.push(Diagnostic::error(
- *loc,
- "struct or function expected".to_string(),
- ));
- return Err(());
- }
- _ => {}
- }
- // not a struct literal, remove those errors and try resolving as function call
- if context.constant {
- diagnostics.push(Diagnostic::error(
- *loc,
- "cannot call function in constant expression".to_string(),
- ));
- return Err(());
- }
- let expr = named_function_call_expr(
- loc,
- ty,
- args,
- context,
- ns,
- symtable,
- diagnostics,
- resolve_to,
- )?;
- check_function_call(ns, &expr, symtable);
- if expr.tys().len() > 1 && !is_destructible {
- diagnostics.push(Diagnostic::error(
- *loc,
- "destucturing statement needed for function that returns multiple values".to_string(),
- ));
- return Err(());
- }
- Ok(expr)
- }
- /// Resolve any callable expression
- pub fn call_expr(
- loc: &pt::Loc,
- ty: &pt::Expression,
- args: &[pt::Expression],
- is_destructible: bool,
- context: &ExprContext,
- ns: &mut Namespace,
- symtable: &mut Symtable,
- diagnostics: &mut Diagnostics,
- resolve_to: ResolveTo,
- ) -> Result<Expression, ()> {
- let mut nullsink = Diagnostics::default();
- let ty = ty.remove_parenthesis();
- match ns.resolve_type(
- context.file_no,
- context.contract_no,
- true,
- ty,
- &mut nullsink,
- ) {
- Ok(Type::Struct(str_ty)) => {
- return struct_literal(loc, &str_ty, args, context, ns, symtable, diagnostics);
- }
- Ok(to) => {
- // Cast
- return if args.is_empty() {
- diagnostics.push(Diagnostic::error(
- *loc,
- "missing argument to cast".to_string(),
- ));
- Err(())
- } else if args.len() > 1 {
- diagnostics.push(Diagnostic::error(
- *loc,
- "too many arguments to cast".to_string(),
- ));
- Err(())
- } else {
- let expr = expression(
- &args[0],
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Unknown,
- )?;
- expr.cast(loc, &to, false, ns, diagnostics)
- };
- }
- Err(_) => (),
- }
- let expr = match ty.remove_parenthesis() {
- pt::Expression::New(_, ty) => new(loc, ty, args, context, ns, symtable, diagnostics)?,
- pt::Expression::FunctionCallBlock(loc, expr, _)
- if matches!(expr.remove_parenthesis(), pt::Expression::New(..)) =>
- {
- new(loc, ty, args, context, ns, symtable, diagnostics)?
- }
- _ => {
- deprecated_constructor_arguments(ty, diagnostics)?;
- function_call_expr(
- loc,
- ty,
- args,
- context,
- ns,
- symtable,
- diagnostics,
- resolve_to,
- )?
- }
- };
- check_function_call(ns, &expr, symtable);
- if expr.tys().len() > 1 && !is_destructible {
- diagnostics.push(Diagnostic::error(
- *loc,
- "destucturing statement needed for function that returns multiple values".to_string(),
- ));
- return Err(());
- }
- Ok(expr)
- }
- /// Resolve function call
- pub fn function_call_expr(
- loc: &pt::Loc,
- ty: &pt::Expression,
- args: &[pt::Expression],
- context: &ExprContext,
- ns: &mut Namespace,
- symtable: &mut Symtable,
- diagnostics: &mut Diagnostics,
- resolve_to: ResolveTo,
- ) -> Result<Expression, ()> {
- let (ty, call_args, call_args_loc) = collect_call_args(ty, diagnostics)?;
- match ty.remove_parenthesis() {
- pt::Expression::MemberAccess(_, member, func) => method_call_pos_args(
- loc,
- member,
- func,
- args,
- &call_args,
- call_args_loc,
- context,
- ns,
- symtable,
- diagnostics,
- resolve_to,
- ),
- pt::Expression::Variable(id) => {
- // is it a builtin
- if builtin::is_builtin_call(None, &id.name, ns) {
- return {
- let expr = builtin::resolve_call(
- &id.loc,
- None,
- &id.name,
- args,
- context,
- ns,
- symtable,
- diagnostics,
- )?;
- if expr.tys().len() > 1 {
- diagnostics.push(Diagnostic::error(
- *loc,
- format!("builtin function '{}' returns more than one value", id.name),
- ));
- Err(())
- } else {
- Ok(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(&id.name).is_some()
- || matches!(
- ns.resolve_var(context.file_no, context.contract_no, id, true),
- Some(Symbol::Variable(..))
- )
- {
- call_function_type(
- loc,
- ty,
- args,
- &call_args,
- call_args_loc,
- context,
- ns,
- symtable,
- diagnostics,
- resolve_to,
- )
- } else {
- if let Some(loc) = call_args_loc {
- diagnostics.push(Diagnostic::error(
- loc,
- "call arguments not permitted for internal calls".to_string(),
- ));
- return Err(());
- }
- function_call_pos_args(
- loc,
- id,
- pt::FunctionTy::Function,
- args,
- available_functions(&id.name, true, context.file_no, context.contract_no, ns),
- true,
- context,
- ns,
- resolve_to,
- symtable,
- diagnostics,
- )
- }
- }
- _ => call_function_type(
- loc,
- ty,
- args,
- &call_args,
- call_args_loc,
- context,
- ns,
- symtable,
- diagnostics,
- resolve_to,
- ),
- }
- }
- /// Resolve function call expression with named arguments
- pub fn named_function_call_expr(
- loc: &pt::Loc,
- ty: &pt::Expression,
- args: &[pt::NamedArgument],
- context: &ExprContext,
- ns: &mut Namespace,
- symtable: &mut Symtable,
- diagnostics: &mut Diagnostics,
- resolve_to: ResolveTo,
- ) -> Result<Expression, ()> {
- let (ty, call_args, call_args_loc) = collect_call_args(ty, diagnostics)?;
- match ty {
- pt::Expression::MemberAccess(_, member, func) => method_call_named_args(
- loc,
- member,
- func,
- args,
- &call_args,
- call_args_loc,
- context,
- ns,
- symtable,
- diagnostics,
- resolve_to,
- ),
- pt::Expression::Variable(id) => {
- if let Some(loc) = call_args_loc {
- diagnostics.push(Diagnostic::error(
- loc,
- "call arguments not permitted for internal calls".to_string(),
- ));
- return Err(());
- }
- function_call_named_args(
- loc,
- id,
- args,
- available_functions(&id.name, true, context.file_no, context.contract_no, ns),
- true,
- context,
- resolve_to,
- ns,
- symtable,
- diagnostics,
- )
- }
- pt::Expression::ArraySubscript(..) => {
- diagnostics.push(Diagnostic::error(
- ty.loc(),
- "unexpected array type".to_string(),
- ));
- Err(())
- }
- _ => {
- diagnostics.push(Diagnostic::error(
- ty.loc(),
- "expression not expected here".to_string(),
- ));
- Err(())
- }
- }
- }
- /// Get the return values for a function call
- pub(crate) fn function_returns(ftype: &Function, resolve_to: ResolveTo) -> Vec<Type> {
- if !ftype.returns.is_empty() && !matches!(resolve_to, ResolveTo::Discard) {
- ftype.returns.iter().map(|p| p.ty.clone()).collect()
- } else {
- vec![Type::Void]
- }
- }
- /// Get the function type for an internal.external function call
- pub(crate) fn function_type(func: &Function, external: bool, resolve_to: ResolveTo) -> Type {
- let params = func.params.iter().map(|p| p.ty.clone()).collect();
- let mutability = func.mutability.clone();
- let returns = function_returns(func, resolve_to);
- if external {
- Type::ExternalFunction {
- params,
- mutability,
- returns,
- }
- } else {
- Type::InternalFunction {
- params,
- mutability,
- returns,
- }
- }
- }
- /// This function evaluates the arguments of a function call with either positional arguments or
- /// named arguments.
- fn evaluate_argument(
- arg: &pt::Expression,
- context: &ExprContext,
- ns: &mut Namespace,
- symtable: &mut Symtable,
- arg_ty: &Type,
- errors: &mut Diagnostics,
- cast_args: &mut Vec<Expression>,
- ) -> bool {
- expression(arg, context, ns, symtable, errors, ResolveTo::Type(arg_ty))
- .and_then(|arg| arg.cast(&arg.loc(), arg_ty, true, ns, errors))
- .map(|expr| cast_args.push(expr))
- .is_ok()
- }
- /// This function finishes resolving internal function calls. It returns None if it is not
- /// possible to resolve the function.
- fn resolve_internal_call(
- loc: &Loc,
- function_no: usize,
- context: &ExprContext,
- resolve_to: ResolveTo,
- virtual_call: bool,
- cast_args: Vec<Expression>,
- ns: &Namespace,
- errors: &mut Diagnostics,
- ) -> Option<Expression> {
- let func = &ns.functions[function_no];
- if func.contract_no != context.contract_no && func.is_private() {
- errors.push(Diagnostic::error_with_note(
- *loc,
- format!("cannot call private {}", func.ty),
- func.loc,
- format!("declaration of {} '{}'", func.ty, func.name),
- ));
- return None;
- } else if let (Some(base_no), Some(derived_no)) = (func.contract_no, context.contract_no) {
- if is_base(base_no, derived_no, ns) && matches!(func.visibility, Visibility::External(_)) {
- if func.is_accessor {
- errors.push(Diagnostic::error_with_note(
- *loc,
- "accessor function cannot be called via an internal function call".to_string(),
- func.loc,
- format!("declaration of '{}'", func.name),
- ));
- } else {
- errors.push(Diagnostic::error_with_note(
- *loc,
- "functions declared external cannot be called via an internal function call"
- .to_string(),
- func.loc,
- format!("declaration of {} '{}'", func.ty, func.name),
- ));
- }
- return None;
- }
- }
- let returns = function_returns(func, resolve_to);
- let ty = function_type(func, false, resolve_to);
- Some(Expression::InternalFunctionCall {
- loc: *loc,
- returns,
- function: Box::new(Expression::InternalFunction {
- loc: *loc,
- ty,
- function_no,
- signature: if virtual_call && (func.is_virtual || func.is_override.is_some()) {
- Some(func.signature.clone())
- } else {
- None
- },
- }),
- args: cast_args,
- })
- }
|