| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636 |
- // SPDX-License-Identifier: Apache-2.0
- use super::ast::*;
- use super::contracts::is_base;
- use super::diagnostics::Diagnostics;
- use super::eval::check_term_for_constant_overflow;
- use super::expression::{
- function_call::{available_functions, call_expr, named_call_expr},
- ExprContext, ResolveTo,
- };
- use super::symtable::{LoopScopes, Symtable};
- use crate::sema::expression::constructor::{
- constructor_named_args, match_constructor_to_args, new,
- };
- use crate::sema::expression::function_call::{
- function_call_expr, function_call_pos_args, named_function_call_expr,
- };
- use crate::sema::expression::resolve_expression::expression;
- use crate::sema::function_annotation::function_body_annotations;
- use crate::sema::function_annotation::{unexpected_parameter_annotation, UnresolvedAnnotation};
- use crate::sema::symtable::{VariableInitializer, VariableUsage};
- use crate::sema::unused_variable::{assigned_variable, check_function_call, used_variable};
- use crate::sema::yul::resolve_inline_assembly;
- use crate::sema::Recurse;
- use crate::Target;
- use solang_parser::pt;
- use solang_parser::pt::CatchClause;
- use solang_parser::pt::CodeLocation;
- use solang_parser::pt::OptionalCodeLocation;
- use std::collections::{BTreeMap, HashMap, HashSet};
- use std::sync::Arc;
- pub fn resolve_function_body(
- def: &pt::FunctionDefinition,
- annotations: &[&pt::Annotation],
- file_no: usize,
- contract_no: Option<usize>,
- function_no: usize,
- ns: &mut Namespace,
- ) -> Result<(), ()> {
- let mut symtable = Symtable::new();
- let mut loops = LoopScopes::new();
- let mut res = Vec::new();
- let context = ExprContext {
- file_no,
- contract_no,
- function_no: Some(function_no),
- unchecked: false,
- constant: false,
- lvalue: false,
- yul_function: false,
- };
- let mut unresolved_annotation: Vec<UnresolvedAnnotation> = Vec::new();
- // first add function parameters
- for (i, p) in def.params.iter().enumerate() {
- let p = p.1.as_ref().unwrap();
- if let Some(ref name) = p.name {
- if let Some(pos) = symtable.add(
- name,
- ns.functions[function_no].params[i].ty.clone(),
- ns,
- VariableInitializer::Solidity(None),
- VariableUsage::Parameter,
- p.storage.clone(),
- ) {
- ns.check_shadowing(file_no, contract_no, name);
- if p.annotation.is_some() {
- unresolved_annotation.push(UnresolvedAnnotation {
- parameter_no: i,
- var_no: pos,
- });
- }
- symtable.arguments.push(Some(pos));
- }
- } else {
- symtable.arguments.push(None);
- }
- }
- function_body_annotations(
- function_no,
- annotations,
- &unresolved_annotation,
- &mut symtable,
- &context,
- ns,
- );
- // now that the function arguments have been resolved, we can resolve the bases for
- // constructors.
- if def.ty == pt::FunctionTy::Constructor {
- let contract_no = contract_no.unwrap();
- let mut resolve_bases: BTreeMap<usize, pt::Loc> = BTreeMap::new();
- let mut all_ok = true;
- let mut diagnostics = Diagnostics::default();
- for attr in &def.attributes {
- if let pt::FunctionAttribute::BaseOrModifier(loc, base) = attr {
- match ns.resolve_contract_with_namespace(file_no, &base.name, &mut diagnostics) {
- Ok(base_no) => {
- if base_no == contract_no || !is_base(base_no, contract_no, ns) {
- ns.diagnostics.push(Diagnostic::error(
- *loc,
- format!(
- "contract '{}' is not a base contract of '{}'",
- base.name, ns.contracts[contract_no].name,
- ),
- ));
- all_ok = false;
- } else if let Some(prev) = resolve_bases.get(&base_no) {
- ns.diagnostics.push(Diagnostic::error_with_note(
- *loc,
- format!("duplicate base contract '{}'", base.name),
- *prev,
- format!("previous base contract '{}'", base.name),
- ));
- all_ok = false;
- } else if let Some(args) = &base.args {
- let mut diagnostics = Diagnostics::default();
- // find constructor which matches this
- if let Ok((Some(constructor_no), args)) = match_constructor_to_args(
- &base.loc,
- args,
- base_no,
- &context,
- ns,
- &mut symtable,
- &mut diagnostics,
- ) {
- for arg in &args {
- used_variable(ns, arg, &mut symtable);
- }
- ns.functions[function_no]
- .bases
- .insert(base_no, (base.loc, constructor_no, args));
- resolve_bases.insert(base_no, base.loc);
- }
- ns.diagnostics.extend(diagnostics);
- } else {
- ns.diagnostics.push(Diagnostic::error(
- *loc,
- format!(
- "missing arguments to constructor of contract '{}'",
- base.name
- ),
- ));
- all_ok = false;
- }
- }
- Err(_) => {
- all_ok = false;
- }
- }
- }
- }
- if all_ok && ns.contracts[contract_no].instantiable {
- for base in &ns.contracts[contract_no].bases {
- // do we have constructor arguments
- if base.constructor.is_some() || resolve_bases.contains_key(&base.contract_no) {
- continue;
- }
- // does the contract require arguments
- if ns.contracts[base.contract_no].constructor_needs_arguments(ns) {
- ns.diagnostics.push(Diagnostic::error(
- def.loc,
- format!(
- "missing arguments to contract '{}' constructor",
- ns.contracts[base.contract_no].name
- ),
- ));
- }
- }
- }
- ns.diagnostics.extend(diagnostics);
- }
- // resolve modifiers on functions
- if def.ty == pt::FunctionTy::Function {
- let mut modifiers = Vec::new();
- let mut diagnostics = Diagnostics::default();
- for attr in &def.attributes {
- if let pt::FunctionAttribute::BaseOrModifier(_, modifier) = attr {
- if modifier.name.identifiers.len() != 1 {
- ns.diagnostics.push(Diagnostic::error(
- def.loc,
- format!("unknown modifier '{}' on function", modifier.name),
- ));
- } else {
- let modifier_name = &modifier.name.identifiers[0];
- if let Ok(e) = function_call_pos_args(
- &modifier.loc,
- modifier_name,
- pt::FunctionTy::Modifier,
- modifier.args.as_ref().unwrap_or(&Vec::new()),
- available_functions(
- &modifier_name.name,
- false,
- context.file_no,
- context.contract_no,
- ns,
- ),
- true,
- &context,
- ns,
- ResolveTo::Unknown,
- &mut symtable,
- &mut diagnostics,
- ) {
- modifiers.push(e);
- }
- }
- }
- }
- ns.diagnostics.extend(diagnostics);
- ns.functions[function_no].modifiers = modifiers;
- }
- // If there is no return statement, any unnamed return types will
- // implicitly be 0. If there is a return type which is a storage
- // reference (e.g. int[] storage), a value must be given via
- // a return statement, else the value will not refer to valid storage,
- // as 0 is almost certainly an invalid storage key.
- let mut return_required = false;
- // If any of the return values are named, then the return statement can be omitted at
- // the end of the function, and return values may be omitted too. Create variables to
- // store the return values
- for (i, p) in def.returns.iter().enumerate() {
- let ret = &ns.functions[function_no].returns[i];
- if let Some(ref name) = p.1.as_ref().unwrap().name {
- if let Some(pos) = symtable.add(
- name,
- ret.ty.clone(),
- ns,
- VariableInitializer::Solidity(None),
- VariableUsage::ReturnVariable,
- None,
- ) {
- ns.check_shadowing(file_no, contract_no, name);
- symtable.returns.push(pos);
- }
- } else {
- if ret.ty.is_contract_storage() {
- return_required = true;
- }
- // anonymous return
- let id = pt::Identifier {
- loc: p.0,
- name: "".to_owned(),
- };
- let pos = symtable
- .add(
- &id,
- ret.ty.clone(),
- ns,
- VariableInitializer::Solidity(None),
- VariableUsage::AnonymousReturnVariable,
- None,
- )
- .unwrap();
- symtable.returns.push(pos);
- }
- }
- let body = match def.body {
- None => return Ok(()),
- Some(ref body) => body,
- };
- let mut diagnostics = Diagnostics::default();
- let reachable = statement(
- body,
- &mut res,
- &context,
- &mut symtable,
- &mut loops,
- ns,
- &mut diagnostics,
- );
- ns.diagnostics.extend(diagnostics);
- if reachable? && return_required {
- for param in ns.functions[function_no].returns.iter() {
- if param.id.is_none() && param.ty.is_contract_storage() {
- ns.diagnostics.push(Diagnostic::error(
- param.loc,
- "storage reference must be given value with a return statement".to_string(),
- ));
- }
- }
- }
- if def.ty == pt::FunctionTy::Modifier {
- let mut has_underscore = false;
- // unsure modifier has underscore
- fn check_statement(stmt: &Statement, has_underscore: &mut bool) -> bool {
- if stmt.is_underscore() {
- *has_underscore = true;
- false
- } else {
- true
- }
- }
- for stmt in &mut res {
- stmt.recurse(&mut has_underscore, check_statement);
- }
- if !has_underscore {
- ns.diagnostics.push(Diagnostic::error(
- body.loc().end_range(),
- "missing '_' in modifier".to_string(),
- ));
- }
- }
- ns.functions[function_no].body = res;
- std::mem::swap(&mut ns.functions[function_no].symtable, &mut symtable);
- Ok(())
- }
- /// Resolve a statement
- #[allow(clippy::ptr_arg)]
- fn statement(
- stmt: &pt::Statement,
- res: &mut Vec<Statement>,
- context: &ExprContext,
- symtable: &mut Symtable,
- loops: &mut LoopScopes,
- ns: &mut Namespace,
- diagnostics: &mut Diagnostics,
- ) -> Result<bool, ()> {
- let function_no = context.function_no.unwrap();
- match stmt {
- pt::Statement::VariableDefinition(loc, decl, initializer) => {
- let (var_ty, ty_loc) =
- resolve_var_decl_ty(&decl.ty, &decl.storage, context, ns, diagnostics)?;
- let initializer = if let Some(init) = initializer {
- let expr = expression(
- init,
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Type(&var_ty),
- )?;
- expr.recurse(ns, check_term_for_constant_overflow);
- used_variable(ns, &expr, symtable);
- Some(Arc::new(expr.cast(
- &expr.loc(),
- &var_ty,
- true,
- ns,
- diagnostics,
- )?))
- } else {
- None
- };
- if let Some(pos) = symtable.add(
- decl.name.as_ref().unwrap(),
- var_ty.clone(),
- ns,
- VariableInitializer::Solidity(initializer.clone()),
- VariableUsage::LocalVariable,
- decl.storage.clone(),
- ) {
- ns.check_shadowing(
- context.file_no,
- context.contract_no,
- decl.name.as_ref().unwrap(),
- );
- res.push(Statement::VariableDecl(
- *loc,
- pos,
- Parameter {
- loc: decl.loc,
- ty: var_ty,
- ty_loc: Some(ty_loc),
- id: Some(decl.name.clone().unwrap()),
- indexed: false,
- readonly: false,
- infinite_size: false,
- recursive: false,
- annotation: None,
- },
- initializer,
- ));
- }
- Ok(true)
- }
- pt::Statement::Block {
- statements,
- unchecked,
- ..
- } => {
- symtable.new_scope();
- let mut reachable = true;
- let mut context = context.clone();
- context.unchecked |= *unchecked;
- for stmt in statements {
- if !reachable {
- ns.diagnostics.push(Diagnostic::error(
- stmt.loc(),
- "unreachable statement".to_string(),
- ));
- return Err(());
- }
- reachable = statement(stmt, res, &context, symtable, loops, ns, diagnostics)?;
- }
- symtable.leave_scope();
- Ok(reachable)
- }
- pt::Statement::Break(loc) => {
- if loops.do_break() {
- res.push(Statement::Break(*loc));
- Ok(false)
- } else {
- diagnostics.push(Diagnostic::error(
- stmt.loc(),
- "break statement not in loop".to_string(),
- ));
- Err(())
- }
- }
- pt::Statement::Continue(loc) => {
- if loops.do_continue() {
- res.push(Statement::Continue(*loc));
- Ok(false)
- } else {
- diagnostics.push(Diagnostic::error(
- stmt.loc(),
- "continue statement not in loop".to_string(),
- ));
- Err(())
- }
- }
- pt::Statement::While(loc, cond_expr, body) => {
- let expr = expression(
- cond_expr,
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Type(&Type::Bool),
- )?;
- used_variable(ns, &expr, symtable);
- let cond = expr.cast(&expr.loc(), &Type::Bool, true, ns, diagnostics)?;
- symtable.new_scope();
- let mut body_stmts = Vec::new();
- loops.new_scope();
- statement(
- body,
- &mut body_stmts,
- context,
- symtable,
- loops,
- ns,
- diagnostics,
- )?;
- symtable.leave_scope();
- loops.leave_scope();
- res.push(Statement::While(*loc, true, cond, body_stmts));
- Ok(true)
- }
- pt::Statement::DoWhile(loc, body, cond_expr) => {
- let expr = expression(
- cond_expr,
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Type(&Type::Bool),
- )?;
- used_variable(ns, &expr, symtable);
- let cond = expr.cast(&expr.loc(), &Type::Bool, true, ns, diagnostics)?;
- symtable.new_scope();
- let mut body_stmts = Vec::new();
- loops.new_scope();
- statement(
- body,
- &mut body_stmts,
- context,
- symtable,
- loops,
- ns,
- diagnostics,
- )?;
- symtable.leave_scope();
- loops.leave_scope();
- res.push(Statement::DoWhile(*loc, true, body_stmts, cond));
- Ok(true)
- }
- pt::Statement::If(loc, cond_expr, then, else_) => {
- let expr = expression(
- cond_expr,
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Type(&Type::Bool),
- )?;
- used_variable(ns, &expr, symtable);
- let cond = expr.cast(&expr.loc(), &Type::Bool, true, ns, diagnostics)?;
- symtable.new_scope();
- let mut then_stmts = Vec::new();
- let mut reachable = statement(
- then,
- &mut then_stmts,
- context,
- symtable,
- loops,
- ns,
- diagnostics,
- )?;
- symtable.leave_scope();
- let mut else_stmts = Vec::new();
- if let Some(stmts) = else_ {
- symtable.new_scope();
- reachable |= statement(
- stmts,
- &mut else_stmts,
- context,
- symtable,
- loops,
- ns,
- diagnostics,
- )?;
- symtable.leave_scope();
- } else {
- reachable = true;
- }
- res.push(Statement::If(*loc, reachable, cond, then_stmts, else_stmts));
- Ok(reachable)
- }
- pt::Statement::Args(loc, _) => {
- ns.diagnostics.push(Diagnostic::error(
- *loc,
- "expected code block, not list of named arguments".to_string(),
- ));
- Err(())
- }
- pt::Statement::For(loc, init_stmt, None, next_expr, body_stmt) => {
- symtable.new_scope();
- let mut init = Vec::new();
- if let Some(init_stmt) = init_stmt {
- statement(
- init_stmt,
- &mut init,
- context,
- symtable,
- loops,
- ns,
- diagnostics,
- )?;
- }
- loops.new_scope();
- let mut body = Vec::new();
- if let Some(body_stmt) = body_stmt {
- statement(
- body_stmt,
- &mut body,
- context,
- symtable,
- loops,
- ns,
- diagnostics,
- )?;
- }
- let control = loops.leave_scope();
- let reachable = control.no_breaks > 0;
- let mut next = None;
- if let Some(next_expr) = next_expr {
- next = Some(expression(
- next_expr,
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Type(&Type::Bool),
- )?);
- }
- symtable.leave_scope();
- res.push(Statement::For {
- loc: *loc,
- reachable,
- init,
- next,
- cond: None,
- body,
- });
- Ok(reachable)
- }
- pt::Statement::For(loc, init_stmt, Some(cond_expr), next_expr, body_stmt) => {
- symtable.new_scope();
- let mut init = Vec::new();
- let mut body = Vec::new();
- let mut next = None;
- if let Some(init_stmt) = init_stmt {
- statement(
- init_stmt,
- &mut init,
- context,
- symtable,
- loops,
- ns,
- diagnostics,
- )?;
- }
- let cond = expression(
- cond_expr,
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Type(&Type::Bool),
- )?;
- let cond = cond.cast(&cond_expr.loc(), &Type::Bool, true, ns, diagnostics)?;
- // continue goes to next, and if that does exist, cond
- loops.new_scope();
- let mut body_reachable = match body_stmt {
- Some(body_stmt) => statement(
- body_stmt,
- &mut body,
- context,
- symtable,
- loops,
- ns,
- diagnostics,
- )?,
- None => true,
- };
- let control = loops.leave_scope();
- if control.no_continues > 0 {
- body_reachable = true;
- }
- if let Some(next_expr) = next_expr {
- if body_reachable {
- next = Some(expression(
- next_expr,
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Type(&Type::Bool),
- )?);
- }
- }
- symtable.leave_scope();
- res.push(Statement::For {
- loc: *loc,
- reachable: true,
- init,
- next,
- cond: Some(cond),
- body,
- });
- Ok(true)
- }
- pt::Statement::Return(loc, None) => {
- let no_returns = ns.functions[context.function_no.unwrap()].returns.len();
- if symtable.returns.len() != no_returns {
- ns.diagnostics.push(Diagnostic::error(
- *loc,
- format!("missing return value, {no_returns} return values expected"),
- ));
- return Err(());
- }
- res.push(Statement::Return(*loc, None));
- Ok(false)
- }
- pt::Statement::Return(loc, Some(returns)) => {
- let expr = return_with_values(returns, loc, context, symtable, ns, diagnostics)?;
- expr.recurse(ns, check_term_for_constant_overflow);
- for offset in symtable.returns.iter() {
- let elem = symtable.vars.get_mut(offset).unwrap();
- elem.assigned = true;
- }
- res.push(Statement::Return(*loc, Some(expr)));
- Ok(false)
- }
- pt::Statement::Expression(loc, expr) => {
- let expr = match expr {
- // delete statement
- pt::Expression::Delete(_, expr) => {
- let expr =
- expression(expr, context, ns, symtable, diagnostics, ResolveTo::Unknown)?;
- used_variable(ns, &expr, symtable);
- return if let Type::StorageRef(_, ty) = expr.ty() {
- if expr.ty().is_mapping() {
- ns.diagnostics.push(Diagnostic::error(
- *loc,
- "'delete' cannot be applied to mapping type".to_string(),
- ));
- return Err(());
- }
- res.push(Statement::Delete(*loc, ty.as_ref().clone(), expr));
- Ok(true)
- } else {
- ns.diagnostics.push(Diagnostic::warning(
- *loc,
- "argument to 'delete' should be storage reference".to_string(),
- ));
- Err(())
- };
- }
- // is it an underscore modifier statement
- pt::Expression::Variable(id)
- if id.name == "_"
- && ns.functions[function_no].ty == pt::FunctionTy::Modifier =>
- {
- res.push(Statement::Underscore(*loc));
- return Ok(true);
- }
- pt::Expression::FunctionCall(loc, ty, args) => {
- let ret = call_expr(
- loc,
- ty,
- args,
- true,
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Discard,
- )?;
- ret.recurse(ns, check_term_for_constant_overflow);
- ret
- }
- pt::Expression::NamedFunctionCall(loc, ty, args) => {
- let ret = named_call_expr(
- loc,
- ty,
- args,
- true,
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Discard,
- )?;
- ret.recurse(ns, check_term_for_constant_overflow);
- ret
- }
- _ => {
- // is it a destructure statement
- if let pt::Expression::Assign(_, var, expr) = expr {
- if let pt::Expression::List(_, var) = var.as_ref() {
- res.push(destructure(
- loc,
- var,
- expr,
- context,
- symtable,
- ns,
- diagnostics,
- )?);
- // if a noreturn function was called, then the destructure would not resolve
- return Ok(true);
- }
- }
- // the rest. We don't care about the result
- expression(expr, context, ns, symtable, diagnostics, ResolveTo::Unknown)?
- }
- };
- let reachable = expr.tys() != vec![Type::Unreachable];
- res.push(Statement::Expression(*loc, reachable, expr));
- Ok(reachable)
- }
- pt::Statement::Try(loc, expr, returns_and_ok, clause_stmts) => {
- let (stmt, reachable) = try_catch(
- loc,
- expr,
- returns_and_ok,
- clause_stmts,
- context,
- symtable,
- loops,
- ns,
- diagnostics,
- )?;
- res.push(stmt);
- Ok(reachable)
- }
- pt::Statement::Emit(loc, ty) => {
- if let Ok(emit) = emit_event(loc, ty, context, symtable, ns, diagnostics) {
- res.push(emit);
- }
- Ok(true)
- }
- pt::Statement::Assembly {
- loc,
- dialect,
- flags,
- block,
- } => {
- if dialect.is_some() && dialect.as_ref().unwrap().string != "evmasm" {
- ns.diagnostics.push(Diagnostic::error(
- dialect.as_ref().unwrap().loc,
- "only evmasm dialect is supported".to_string(),
- ));
- return Err(());
- }
- if let Some(flags) = flags {
- for flag in flags {
- ns.diagnostics.push(Diagnostic::error(
- flag.loc,
- format!("flag '{}' not supported", flag.string),
- ));
- }
- }
- let resolved_asm =
- resolve_inline_assembly(loc, &block.statements, context, symtable, ns);
- res.push(Statement::Assembly(resolved_asm.0, resolved_asm.1));
- Ok(resolved_asm.1)
- }
- pt::Statement::Revert(loc, path, args) => {
- if let Ok(stmt) = revert_pos_arg(loc, path, args, context, symtable, diagnostics, ns) {
- res.push(stmt);
- }
- Ok(false)
- }
- pt::Statement::RevertNamedArgs(loc, path, args) => {
- if let Ok(stmt) = revert_named_arg(loc, path, args, context, symtable, diagnostics, ns)
- {
- res.push(stmt);
- }
- Ok(false)
- }
- pt::Statement::Error(_) => unimplemented!(),
- }
- }
- /// Resolve a revert statement with position arguments, and optional error, e.g.
- /// ```ignore
- /// revert();
- /// revert("the reason why");
- /// revert NotEnoughBalance(address);
- /// ```
- fn revert_pos_arg(
- loc: &pt::Loc,
- path: &Option<pt::IdentifierPath>,
- args: &[pt::Expression],
- context: &ExprContext,
- symtable: &mut Symtable,
- diagnostics: &mut Diagnostics,
- ns: &mut Namespace,
- ) -> Result<Statement, ()> {
- if let Some(path) = path {
- let error_no = ns.resolve_error(context.file_no, context.contract_no, path, diagnostics)?;
- let mut arguments = Vec::new();
- for (pos, arg) in args.iter().enumerate() {
- let error = &ns.errors[error_no];
- let ty = if let Some(field) = error.fields.get(pos) {
- field.ty.clone()
- } else {
- let _ = expression(arg, context, ns, symtable, diagnostics, ResolveTo::Unknown);
- continue;
- };
- let arg = match expression(
- arg,
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Type(&ty),
- ) {
- Ok(e) => e,
- Err(()) => {
- continue;
- }
- };
- match arg.cast(&arg.loc(), &ty, true, ns, diagnostics) {
- Ok(expr) => {
- arguments.push(expr);
- }
- Err(()) => {
- continue;
- }
- }
- }
- let error = &ns.errors[error_no];
- if args.len() != error.fields.len() {
- ns.diagnostics.push(Diagnostic::error_with_note(
- path.loc,
- format!(
- "error '{}' has {} fields, {} provided",
- error.name,
- error.fields.len(),
- args.len()
- ),
- error.loc,
- format!("definition of '{}'", error.name),
- ));
- }
- if ns.target != Target::EVM {
- ns.diagnostics.push(Diagnostic::error(
- *loc,
- format!("revert with custom errors not supported on {}", ns.target),
- ));
- }
- ns.errors[error_no].used = true;
- Ok(Statement::Revert {
- loc: *loc,
- error_no: Some(error_no),
- args: arguments,
- })
- } else {
- let mut arguments = Vec::new();
- match args.len() {
- 0 => (),
- 1 => {
- if let Ok(arg) = expression(
- &args[0],
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Type(&Type::String),
- ) {
- if let Ok(expr) = arg.cast(&arg.loc(), &Type::String, true, ns, diagnostics) {
- arguments.push(expr);
- }
- }
- }
- count => {
- let loc = pt::Loc::File(loc.file_no(), loc.start(), loc.start() + 6);
- ns.diagnostics.push(Diagnostic::error(
- loc,
- format!(
- "revert takes either no argument or a single reason string argument, {} provided",
- count
- ),
- ));
- for arg in args {
- let _ = expression(arg, context, ns, symtable, diagnostics, ResolveTo::Unknown);
- }
- }
- }
- Ok(Statement::Revert {
- loc: *loc,
- error_no: None,
- args: arguments,
- })
- }
- }
- /// Resolve a revert statement with named arguments, and optional error, e.g.
- /// ```ignore
- /// revert({foo: 1}); // not allowed
- /// revert({reason:"the reason why"}); // not allowed
- /// revert NotEnoughBalance({user: address});
- /// ```
- fn revert_named_arg(
- loc: &pt::Loc,
- path: &Option<pt::IdentifierPath>,
- args: &[pt::NamedArgument],
- context: &ExprContext,
- symtable: &mut Symtable,
- diagnostics: &mut Diagnostics,
- ns: &mut Namespace,
- ) -> Result<Statement, ()> {
- if let Some(path) = path {
- let error_no = ns.resolve_error(context.file_no, context.contract_no, path, diagnostics)?;
- let error = &ns.errors[error_no];
- let unnamed_fields = error
- .fields
- .iter()
- .filter(|param| param.id.is_none())
- .count();
- if unnamed_fields > 0 {
- ns.diagnostics.push(Diagnostic::error_with_note(
- path.loc,
- format!(
- "error '{}' has {} unnamed fields",
- error.name, unnamed_fields
- ),
- error.loc,
- format!("definition of '{}'", error.name),
- ));
- }
- let fields: HashMap<String, (usize, Type)> = error
- .fields
- .iter()
- .enumerate()
- .filter_map(|(i, p)| {
- p.id.as_ref()
- .map(|id| (id.name.to_owned(), (i, p.ty.clone())))
- })
- .collect();
- let mut arguments = HashMap::new();
- for arg in args {
- let error = &ns.errors[error_no];
- let name = arg.name.name.as_str();
- if let Some((pos, ty)) = fields.get(name) {
- if arguments.contains_key(pos) {
- 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;
- }
- let arg = match expression(
- &arg.expr,
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Type(ty),
- ) {
- Ok(e) => e,
- Err(()) => {
- continue;
- }
- };
- match arg.cast(&arg.loc(), ty, true, ns, diagnostics) {
- Ok(expr) => {
- arguments.insert(*pos, expr);
- }
- Err(()) => {
- continue;
- }
- }
- } else {
- ns.diagnostics.push(Diagnostic::error_with_note(
- arg.name.loc,
- format!("error '{}' has no field called '{}'", error.name, name),
- error.loc,
- format!("definition of '{}'", error.name),
- ));
- let _ = expression(
- &arg.expr,
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Unknown,
- );
- }
- }
- if ns.target != Target::EVM {
- ns.diagnostics.push(Diagnostic::error(
- *loc,
- format!("revert with custom errors not supported on {}", ns.target),
- ));
- }
- let mut args = Vec::new();
- let error = &ns.errors[error_no];
- for pos in 0..error.fields.len() {
- if let Some(arg) = arguments.remove(&pos) {
- args.push(arg);
- } else if let Some(id) = &error.fields[pos].id {
- ns.diagnostics.push(Diagnostic::error_with_note(
- path.loc,
- format!("missing field '{}'", id.name),
- error.loc,
- format!("definition of '{}'", error.name),
- ));
- }
- }
- ns.errors[error_no].used = true;
- Ok(Statement::Revert {
- loc: *loc,
- error_no: Some(error_no),
- args,
- })
- } else {
- ns.diagnostics.push(Diagnostic::error(
- *loc,
- "revert with named arguments requires error type".to_string(),
- ));
- Err(())
- }
- }
- /// Resolve emit event
- fn emit_event(
- loc: &pt::Loc,
- ty: &pt::Expression,
- context: &ExprContext,
- symtable: &mut Symtable,
- ns: &mut Namespace,
- diagnostics: &mut Diagnostics,
- ) -> Result<Statement, ()> {
- let function_no = context.function_no.unwrap();
- let to_stmt =
- |ns: &mut Namespace, event_no: usize, event_loc: pt::Loc, cast_args: Vec<Expression>| {
- if !ns.functions[function_no].emits_events.contains(&event_no) {
- ns.functions[function_no].emits_events.push(event_no);
- }
- Statement::Emit {
- loc: *loc,
- event_no,
- event_loc,
- args: cast_args,
- }
- };
- match ty {
- pt::Expression::FunctionCall(_, ty, args) => {
- let event_loc = ty.loc();
- let mut errors = Diagnostics::default();
- let event_nos =
- match ns.resolve_event(context.file_no, context.contract_no, ty, diagnostics) {
- Ok(nos) => nos,
- Err(_) => {
- for arg in args {
- if let Ok(exp) = expression(
- arg,
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Unknown,
- ) {
- used_variable(ns, &exp, symtable);
- };
- }
- return Err(());
- }
- };
- for event_no in &event_nos {
- let event = &mut ns.events[*event_no];
- event.used = true;
- let mut matches = true;
- if args.len() != event.fields.len() {
- errors.push(Diagnostic::cast_error(
- *loc,
- format!(
- "event type '{}' has {} fields, {} provided",
- event.name,
- event.fields.len(),
- args.len()
- ),
- ));
- matches = false;
- }
- let mut cast_args = Vec::new();
- // check if arguments can be implicitly casted
- for (i, arg) in args.iter().enumerate() {
- let ty = ns.events[*event_no]
- .fields
- .get(i)
- .map(|field| field.ty.clone());
- let resolve_to = ty
- .as_ref()
- .map(ResolveTo::Type)
- .unwrap_or(ResolveTo::Unknown);
- let arg = match expression(arg, context, ns, symtable, &mut errors, resolve_to)
- {
- Ok(e) => e,
- Err(()) => {
- matches = false;
- break;
- }
- };
- used_variable(ns, &arg, symtable);
- if let Some(ty) = &ty {
- match arg.cast(&arg.loc(), ty, true, ns, &mut errors) {
- Ok(expr) => cast_args.push(expr),
- Err(_) => {
- matches = false;
- }
- }
- }
- }
- if matches {
- return Ok(to_stmt(ns, *event_no, event_loc, cast_args));
- } else if event_nos.len() > 1 && diagnostics.extend_non_casting(&errors) {
- return Err(());
- }
- }
- if event_nos.len() == 1 {
- diagnostics.extend(errors);
- } else {
- diagnostics.push(Diagnostic::error(
- *loc,
- "cannot find event which matches signature".to_string(),
- ));
- }
- }
- pt::Expression::NamedFunctionCall(_, ty, args) => {
- let event_loc = ty.loc();
- let mut temp_diagnostics = Diagnostics::default();
- 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,
- );
- continue;
- }
- arguments.insert(arg.name.name.as_str(), &arg.expr);
- }
- let event_nos = match ns.resolve_event(
- context.file_no,
- context.contract_no,
- ty,
- &mut temp_diagnostics,
- ) {
- Ok(nos) => nos,
- Err(_) => {
- // check arguments for errors
- for (_, arg) in arguments {
- let _ =
- expression(arg, context, ns, symtable, diagnostics, ResolveTo::Unknown);
- }
- return Err(());
- }
- };
- for event_no in &event_nos {
- let event = &mut ns.events[*event_no];
- event.used = true;
- let params_len = event.fields.len();
- let mut matches = true;
- let unnamed_fields = event.fields.iter().filter(|p| p.id.is_none()).count();
- if unnamed_fields > 0 {
- temp_diagnostics.push(Diagnostic::cast_error_with_note(
- *loc,
- format!(
- "event cannot be emmited with named fields as {unnamed_fields} of its fields do not have names"
- ),
- event.loc,
- format!("definition of {}", event.name),
- ));
- matches = false;
- } else if params_len != arguments.len() {
- temp_diagnostics.push(Diagnostic::error(
- *loc,
- format!(
- "event expects {} arguments, {} provided",
- params_len,
- arguments.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.events[*event_no].fields[i].clone();
- if param.id.is_none() {
- continue;
- }
- let arg = match arguments.get(param.name_as_str()) {
- Some(a) => a,
- None => {
- matches = false;
- temp_diagnostics.push(Diagnostic::cast_error(
- *loc,
- format!(
- "missing argument '{}' to event '{}'",
- param.name_as_str(),
- ns.events[*event_no].name,
- ),
- ));
- continue;
- }
- };
- let arg = match expression(
- arg,
- context,
- ns,
- symtable,
- &mut temp_diagnostics,
- ResolveTo::Type(¶m.ty),
- ) {
- Ok(e) => e,
- Err(()) => {
- matches = false;
- continue;
- }
- };
- used_variable(ns, &arg, symtable);
- match arg.cast(&arg.loc(), ¶m.ty, true, ns, &mut temp_diagnostics) {
- Ok(expr) => cast_args.push(expr),
- Err(_) => {
- matches = false;
- }
- }
- }
- if matches {
- return Ok(to_stmt(ns, *event_no, event_loc, cast_args));
- } else if event_nos.len() > 1 && diagnostics.extend_non_casting(&temp_diagnostics) {
- return Err(());
- }
- }
- if event_nos.len() == 1 {
- diagnostics.extend(temp_diagnostics);
- } else {
- diagnostics.push(Diagnostic::error(
- *loc,
- "cannot find event which matches signature".to_string(),
- ));
- }
- }
- pt::Expression::FunctionCallBlock(_, ty, block) => {
- let _ = ns.resolve_event(context.file_no, context.contract_no, ty, diagnostics);
- diagnostics.push(Diagnostic::error(
- block.loc(),
- "expected event arguments, found code block".to_string(),
- ));
- }
- _ => unreachable!(),
- }
- Err(())
- }
- /// Resolve destructuring assignment
- fn destructure(
- loc: &pt::Loc,
- vars: &[(pt::Loc, Option<pt::Parameter>)],
- expr: &pt::Expression,
- context: &ExprContext,
- symtable: &mut Symtable,
- ns: &mut Namespace,
- diagnostics: &mut Diagnostics,
- ) -> Result<Statement, ()> {
- // first resolve the fields so we know the types
- let mut fields = Vec::new();
- let mut left_tys = Vec::new();
- let mut lcontext = context.clone();
- lcontext.lvalue = true;
- for (_, param) in vars {
- match param {
- None => {
- left_tys.push(None);
- fields.push(DestructureField::None);
- }
- Some(pt::Parameter {
- loc,
- ty,
- storage,
- name: None,
- annotation,
- }) => {
- // The grammar does not allow annotation in destructures, so this assertion shall
- // always be true.
- assert!(annotation.is_none());
- if let Some(storage) = storage {
- diagnostics.push(Diagnostic::error(
- storage.loc(),
- format!("storage modifier '{storage}' not permitted on assignment"),
- ));
- return Err(());
- }
- // ty will just be a normal expression, not a type
- let e = expression(ty, &lcontext, ns, symtable, diagnostics, ResolveTo::Unknown)?;
- match &e {
- Expression::ConstantVariable {
- contract_no: Some(contract_no),
- var_no,
- ..
- } => {
- diagnostics.push(Diagnostic::error(
- *loc,
- format!(
- "cannot assign to constant '{}'",
- ns.contracts[*contract_no].variables[*var_no].name
- ),
- ));
- return Err(());
- }
- Expression::ConstantVariable {
- contract_no: None,
- var_no,
- ..
- } => {
- diagnostics.push(Diagnostic::error(
- *loc,
- format!("cannot assign to constant '{}'", ns.constants[*var_no].name),
- ));
- return Err(());
- }
- Expression::StorageVariable {
- contract_no,
- var_no,
- ..
- } => {
- let store_var = &ns.contracts[*contract_no].variables[*var_no];
- if store_var.immutable
- && !ns.functions[context.function_no.unwrap()].is_constructor()
- {
- diagnostics.push(Diagnostic::error(
- *loc,
- format!(
- "cannot assign to immutable '{}' outside of constructor",
- store_var.name
- ),
- ));
- return Err(());
- }
- }
- Expression::Variable { .. } => (),
- _ => match e.ty() {
- Type::Ref(_) | Type::StorageRef(false, _) => (),
- _ => {
- diagnostics.push(Diagnostic::error(
- *loc,
- "expression is not assignable".to_string(),
- ));
- return Err(());
- }
- },
- }
- assigned_variable(ns, &e, symtable);
- left_tys.push(Some(e.ty()));
- fields.push(DestructureField::Expression(e));
- }
- Some(pt::Parameter {
- loc,
- ty,
- storage,
- name: Some(name),
- annotation,
- }) => {
- // The grammar does not allow annotation in destructures, so this assertion shall
- // always be true.
- assert!(annotation.is_none());
- let (ty, ty_loc) = resolve_var_decl_ty(ty, storage, context, ns, diagnostics)?;
- if let Some(pos) = symtable.add(
- name,
- ty.clone(),
- ns,
- VariableInitializer::Solidity(None),
- VariableUsage::DestructureVariable,
- storage.clone(),
- ) {
- ns.check_shadowing(context.file_no, context.contract_no, name);
- left_tys.push(Some(ty.clone()));
- fields.push(DestructureField::VariableDecl(
- pos,
- Parameter {
- loc: *loc,
- id: Some(name.clone()),
- ty,
- ty_loc: Some(ty_loc),
- indexed: false,
- readonly: false,
- infinite_size: false,
- recursive: false,
- annotation: None,
- },
- ));
- }
- }
- }
- }
- let expr = destructure_values(
- loc,
- expr,
- &left_tys,
- &fields,
- context,
- symtable,
- ns,
- diagnostics,
- )?;
- Ok(Statement::Destructure(*loc, fields, expr))
- }
- fn destructure_values(
- loc: &pt::Loc,
- expr: &pt::Expression,
- left_tys: &[Option<Type>],
- fields: &[DestructureField],
- context: &ExprContext,
- symtable: &mut Symtable,
- ns: &mut Namespace,
- diagnostics: &mut Diagnostics,
- ) -> Result<Expression, ()> {
- let expr = match expr.remove_parenthesis() {
- pt::Expression::FunctionCall(loc, ty, args) => {
- let res = function_call_expr(
- loc,
- ty,
- args,
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Unknown,
- )?;
- check_function_call(ns, &res, symtable);
- res
- }
- pt::Expression::NamedFunctionCall(loc, ty, args) => {
- let res = named_function_call_expr(
- loc,
- ty,
- args,
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Unknown,
- )?;
- check_function_call(ns, &res, symtable);
- res
- }
- pt::Expression::ConditionalOperator(loc, cond, left, right) => {
- let cond = expression(
- cond,
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Type(&Type::Bool),
- )?;
- used_variable(ns, &cond, symtable);
- let left = destructure_values(
- &left.loc(),
- left,
- left_tys,
- fields,
- context,
- symtable,
- ns,
- diagnostics,
- )?;
- used_variable(ns, &left, symtable);
- let right = destructure_values(
- &right.loc(),
- right,
- left_tys,
- fields,
- context,
- symtable,
- ns,
- diagnostics,
- )?;
- used_variable(ns, &right, symtable);
- return Ok(Expression::ConditionalOperator {
- loc: *loc,
- ty: Type::Unreachable,
- cond: Box::new(cond),
- true_option: Box::new(left),
- false_option: Box::new(right),
- });
- }
- _ => {
- let mut list = Vec::new();
- let exprs = parameter_list_to_expr_list(expr, diagnostics)?;
- if exprs.len() != left_tys.len() {
- diagnostics.push(Diagnostic::error(
- *loc,
- format!(
- "destructuring assignment has {} elements on the left and {} on the right",
- left_tys.len(),
- exprs.len(),
- ),
- ));
- return Err(());
- }
- for (i, e) in exprs.iter().enumerate() {
- let e = expression(
- e,
- context,
- ns,
- symtable,
- diagnostics,
- if let Some(ty) = left_tys[i].as_ref() {
- ResolveTo::Type(ty)
- } else {
- ResolveTo::Unknown
- },
- )?;
- match e.ty() {
- Type::Void | Type::Unreachable => {
- diagnostics.push(Diagnostic::error(
- e.loc(),
- "function does not return a value".to_string(),
- ));
- return Err(());
- }
- _ => {
- used_variable(ns, &e, symtable);
- }
- }
- list.push(e);
- }
- Expression::List { loc: *loc, list }
- }
- };
- let mut right_tys = expr.tys();
- // Return type void or unreachable are synthetic
- if right_tys.len() == 1 && (right_tys[0] == Type::Unreachable || right_tys[0] == Type::Void) {
- right_tys.truncate(0);
- }
- if left_tys.len() != right_tys.len() {
- diagnostics.push(Diagnostic::error(
- *loc,
- format!(
- "destructuring assignment has {} elements on the left and {} on the right",
- left_tys.len(),
- right_tys.len()
- ),
- ));
- return Err(());
- }
- // Check that the values can be cast
- for (i, field) in fields.iter().enumerate() {
- if let Some(left_ty) = &left_tys[i] {
- let loc = field.loc_opt().unwrap();
- let _ = Expression::Variable {
- loc,
- ty: right_tys[i].clone(),
- var_no: i,
- }
- .cast(&loc, left_ty.deref_any(), true, ns, diagnostics)?;
- }
- }
- Ok(expr)
- }
- /// Resolve the type of a variable declaration
- fn resolve_var_decl_ty(
- ty: &pt::Expression,
- storage: &Option<pt::StorageLocation>,
- context: &ExprContext,
- ns: &mut Namespace,
- diagnostics: &mut Diagnostics,
- ) -> Result<(Type, pt::Loc), ()> {
- let mut loc_ty = ty.loc();
- let mut var_ty =
- ns.resolve_type(context.file_no, context.contract_no, false, ty, diagnostics)?;
- if let Some(storage) = storage {
- if !var_ty.can_have_data_location() {
- diagnostics.push(Diagnostic::error(
- storage.loc(),
- format!("data location '{storage}' only allowed for array, struct or mapping type"),
- ));
- return Err(());
- }
- if let pt::StorageLocation::Storage(loc) = storage {
- loc_ty.use_end_from(loc);
- var_ty = Type::StorageRef(false, Box::new(var_ty));
- }
- // Note we are completely ignoring memory or calldata data locations. Everything
- // will be stored in memory.
- }
- if var_ty.contains_mapping(ns) && !var_ty.is_contract_storage() {
- diagnostics.push(Diagnostic::error(
- ty.loc(),
- "mapping only allowed in storage".to_string(),
- ));
- return Err(());
- }
- if !var_ty.is_contract_storage() && !var_ty.fits_in_memory(ns) {
- diagnostics.push(Diagnostic::error(
- ty.loc(),
- "type is too large to fit into memory".to_string(),
- ));
- return Err(());
- }
- Ok((var_ty, loc_ty))
- }
- /// Resolve return statement
- fn return_with_values(
- returns: &pt::Expression,
- loc: &pt::Loc,
- context: &ExprContext,
- symtable: &mut Symtable,
- ns: &mut Namespace,
- diagnostics: &mut Diagnostics,
- ) -> Result<Expression, ()> {
- let function_no = context.function_no.unwrap();
- let no_returns = ns.functions[function_no].returns.len();
- let expr_returns = match returns.remove_parenthesis() {
- pt::Expression::FunctionCall(loc, ty, args) => {
- let expr = call_expr(
- loc,
- ty,
- args,
- true,
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Unknown,
- )?;
- used_variable(ns, &expr, symtable);
- expr
- }
- pt::Expression::NamedFunctionCall(loc, ty, args) => {
- let expr = named_call_expr(
- loc,
- ty,
- args,
- true,
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Unknown,
- )?;
- used_variable(ns, &expr, symtable);
- expr
- }
- pt::Expression::ConditionalOperator(loc, cond, left, right) => {
- let cond = expression(
- cond,
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Type(&Type::Bool),
- )?;
- used_variable(ns, &cond, symtable);
- let left = return_with_values(left, &left.loc(), context, symtable, ns, diagnostics)?;
- used_variable(ns, &left, symtable);
- let right =
- return_with_values(right, &right.loc(), context, symtable, ns, diagnostics)?;
- used_variable(ns, &right, symtable);
- return Ok(Expression::ConditionalOperator {
- loc: *loc,
- ty: Type::Unreachable,
- cond: Box::new(cond),
- true_option: Box::new(left),
- false_option: Box::new(right),
- });
- }
- _ => {
- let returns = parameter_list_to_expr_list(returns, diagnostics)?;
- if no_returns > 0 && returns.is_empty() {
- diagnostics.push(Diagnostic::error(
- *loc,
- format!("missing return value, {no_returns} return values expected"),
- ));
- return Err(());
- }
- if no_returns == 0 && !returns.is_empty() {
- diagnostics.push(Diagnostic::error(
- *loc,
- "function has no return values".to_string(),
- ));
- return Err(());
- }
- if no_returns != returns.len() {
- diagnostics.push(Diagnostic::error(
- *loc,
- format!(
- "incorrect number of return values, expected {} but got {}",
- no_returns,
- returns.len(),
- ),
- ));
- return Err(());
- }
- let mut exprs = Vec::new();
- let return_tys = ns.functions[function_no]
- .returns
- .iter()
- .map(|r| r.ty.clone())
- .collect::<Vec<_>>();
- for (expr_return, return_ty) in returns.iter().zip(return_tys) {
- let expr = expression(
- expr_return,
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Type(&return_ty),
- )?;
- let expr = expr.cast(loc, &return_ty, true, ns, diagnostics)?;
- used_variable(ns, &expr, symtable);
- exprs.push(expr);
- }
- return Ok(if exprs.len() == 1 {
- exprs[0].clone()
- } else {
- Expression::List {
- loc: *loc,
- list: exprs,
- }
- });
- }
- };
- let mut expr_return_tys = expr_returns.tys();
- // Return type void or unreachable are synthetic
- if expr_return_tys.len() == 1
- && (expr_return_tys[0] == Type::Unreachable || expr_return_tys[0] == Type::Void)
- {
- expr_return_tys.truncate(0);
- }
- if no_returns > 0 && expr_return_tys.is_empty() {
- diagnostics.push(Diagnostic::error(
- *loc,
- format!("missing return value, {no_returns} return values expected"),
- ));
- return Err(());
- }
- if no_returns == 0 && !expr_return_tys.is_empty() {
- diagnostics.push(Diagnostic::error(
- *loc,
- "function has no return values".to_string(),
- ));
- return Err(());
- }
- if no_returns != expr_return_tys.len() {
- diagnostics.push(Diagnostic::error(
- *loc,
- format!(
- "incorrect number of return values, expected {} but got {}",
- no_returns,
- expr_return_tys.len(),
- ),
- ));
- return Err(());
- }
- let func_returns_tys = ns.functions[function_no]
- .returns
- .iter()
- .map(|r| r.ty.clone())
- .collect::<Vec<_>>();
- // Check that the values can be cast
- let _ = expr_return_tys
- .into_iter()
- .zip(func_returns_tys)
- .enumerate()
- .map(|(i, (expr_return_ty, func_return_ty))| {
- Expression::Variable {
- loc: expr_returns.loc(),
- ty: expr_return_ty,
- var_no: i,
- }
- .cast(&expr_returns.loc(), &func_return_ty, true, ns, diagnostics)
- })
- .collect::<Result<Vec<_>, _>>()?;
- Ok(expr_returns)
- }
- /// The parser generates parameter lists for lists. Sometimes this needs to be a
- /// simple expression list.
- pub fn parameter_list_to_expr_list<'a>(
- e: &'a pt::Expression,
- diagnostics: &mut Diagnostics,
- ) -> Result<Vec<&'a pt::Expression>, ()> {
- match e {
- pt::Expression::List(_, v) => {
- let mut list = Vec::new();
- let mut broken = false;
- for e in v {
- match &e.1 {
- None => {
- diagnostics.push(Diagnostic::error(e.0, "stray comma".to_string()));
- broken = true;
- }
- Some(pt::Parameter {
- name: Some(name),
- annotation,
- ..
- }) => {
- // The grammar does not allow an annotation inside a list
- assert!(annotation.is_none());
- diagnostics.push(Diagnostic::error(
- name.loc,
- "single value expected".to_string(),
- ));
- broken = true;
- }
- Some(pt::Parameter {
- storage: Some(storage),
- annotation,
- ..
- }) => {
- // The grammar does not allow an annotation inside a list
- assert!(annotation.is_none());
- diagnostics.push(Diagnostic::error(
- storage.loc(),
- "storage specified not permitted here".to_string(),
- ));
- broken = true;
- }
- Some(pt::Parameter { annotation, ty, .. }) => {
- // The grammar does not allow an annotation inside a list
- assert!(annotation.is_none());
- list.push(ty);
- }
- }
- }
- if !broken {
- Ok(list)
- } else {
- Err(())
- }
- }
- pt::Expression::Parenthesis(_, e) => Ok(vec![e]),
- e => Ok(vec![e]),
- }
- }
- /// Parse try catch
- #[allow(clippy::type_complexity)]
- fn try_catch(
- loc: &pt::Loc,
- expr: &pt::Expression,
- returns_and_ok: &Option<(Vec<(pt::Loc, Option<pt::Parameter>)>, Box<pt::Statement>)>,
- clause_stmts: &[pt::CatchClause],
- context: &ExprContext,
- symtable: &mut Symtable,
- loops: &mut LoopScopes,
- ns: &mut Namespace,
- diagnostics: &mut Diagnostics,
- ) -> Result<(Statement, bool), ()> {
- if ns.target == Target::Solana {
- diagnostics.push(Diagnostic::error(
- *loc,
- "The try-catch statement is not supported on Solana. Please, go to \
- https://solang.readthedocs.io/en/latest/language/statements.html#try-catch-statement \
- for more information"
- .to_string(),
- ));
- return Err(());
- }
- let mut expr = expr.remove_parenthesis();
- let mut ok = None;
- while let pt::Expression::FunctionCallBlock(_, e, block) = expr {
- if ok.is_some() {
- diagnostics.push(Diagnostic::error(
- block.loc(),
- "unexpected code block".to_string(),
- ));
- return Err(());
- }
- ok = Some(block.as_ref());
- expr = e.as_ref();
- }
- let fcall = match expr.remove_parenthesis() {
- pt::Expression::FunctionCall(loc, ty, args) => {
- let res = 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)?
- }
- _ => function_call_expr(
- loc,
- ty,
- args,
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Unknown,
- )?,
- };
- check_function_call(ns, &res, symtable);
- res
- }
- pt::Expression::NamedFunctionCall(loc, ty, args) => {
- let res = named_function_call_expr(
- loc,
- ty,
- args,
- context,
- ns,
- symtable,
- diagnostics,
- ResolveTo::Unknown,
- )?;
- check_function_call(ns, &res, symtable);
- res
- }
- pt::Expression::New(loc, call) => {
- let mut call = call.remove_parenthesis();
- while let pt::Expression::FunctionCallBlock(_, expr, block) = call {
- if ok.is_some() {
- ns.diagnostics.push(Diagnostic::error(
- block.loc(),
- "unexpected code block".to_string(),
- ));
- return Err(());
- }
- ok = Some(block.as_ref());
- call = expr.remove_parenthesis();
- }
- match call {
- pt::Expression::FunctionCall(_, ty, args) => {
- let res = new(loc, ty, args, context, ns, symtable, diagnostics)?;
- check_function_call(ns, &res, symtable);
- res
- }
- pt::Expression::NamedFunctionCall(_, ty, args) => {
- let res =
- constructor_named_args(loc, ty, args, context, ns, symtable, diagnostics)?;
- check_function_call(ns, &res, symtable);
- res
- }
- _ => unreachable!(),
- }
- }
- _ => {
- diagnostics.push(Diagnostic::error(
- expr.loc(),
- "try only supports external calls or constructor calls".to_string(),
- ));
- return Err(());
- }
- };
- let mut returns = &Vec::new();
- if let Some((rets, block)) = returns_and_ok {
- if ok.is_some() {
- diagnostics.push(Diagnostic::error(
- block.loc(),
- "unexpected code block".to_string(),
- ));
- return Err(());
- }
- ok = Some(block);
- returns = rets;
- }
- let ok = match ok {
- Some(ok) => ok,
- None => {
- // position after the expression
- let pos = expr.loc().begin_range();
- diagnostics.push(Diagnostic::error(
- pos,
- "code block missing for no catch".to_string(),
- ));
- return Err(());
- }
- };
- symtable.new_scope();
- let mut args = match &fcall {
- Expression::ExternalFunctionCall {
- returns: func_returns,
- ..
- } => {
- let mut func_returns = func_returns.clone();
- if func_returns == vec![Type::Void] {
- func_returns = vec![];
- }
- if !returns.is_empty() && returns.len() != func_returns.len() {
- diagnostics.push(Diagnostic::error(
- expr.loc(),
- format!(
- "try returns list has {} entries while function returns {} values",
- returns.len(),
- func_returns.len()
- ),
- ));
- return Err(());
- }
- func_returns
- }
- Expression::Constructor { contract_no, .. } => match returns.len() {
- 0 => Vec::new(),
- 1 => vec![Type::Contract(*contract_no)],
- _ => {
- diagnostics.push(Diagnostic::error(
- expr.loc(),
- format!(
- "constructor returns single contract, not {} values",
- returns.len()
- ),
- ));
- return Err(());
- }
- },
- _ => {
- diagnostics.push(Diagnostic::error(
- expr.loc(),
- "try only supports external calls or constructor calls".to_string(),
- ));
- return Err(());
- }
- };
- symtable.new_scope();
- let mut params = Vec::new();
- let mut broken = false;
- for param in returns {
- let arg_ty = args.remove(0);
- match ¶m.1 {
- Some(pt::Parameter {
- annotation: Some(annotation),
- ..
- }) => {
- diagnostics.push(unexpected_parameter_annotation(annotation.loc));
- broken = true;
- }
- Some(pt::Parameter {
- ty, storage, name, ..
- }) => {
- let (ret_ty, ty_loc) = resolve_var_decl_ty(ty, storage, context, ns, diagnostics)?;
- if arg_ty != ret_ty {
- diagnostics.push(Diagnostic::error(
- ty.loc(),
- format!(
- "type '{}' does not match return value of function '{}'",
- ret_ty.to_string(ns),
- arg_ty.to_string(ns)
- ),
- ));
- broken = true;
- }
- if let Some(name) = name {
- if let Some(pos) = symtable.add(
- name,
- ret_ty.clone(),
- ns,
- VariableInitializer::Solidity(None),
- VariableUsage::TryCatchReturns,
- storage.clone(),
- ) {
- ns.check_shadowing(context.file_no, context.contract_no, name);
- params.push((
- Some(pos),
- Parameter {
- loc: param.0,
- ty: ret_ty,
- ty_loc: Some(ty_loc),
- id: Some(name.clone()),
- indexed: false,
- readonly: false,
- infinite_size: false,
- recursive: false,
- annotation: None,
- },
- ));
- }
- } else {
- params.push((
- None,
- Parameter {
- loc: param.0,
- ty: ret_ty,
- ty_loc: Some(ty_loc),
- indexed: false,
- id: None,
- readonly: false,
- infinite_size: false,
- recursive: false,
- annotation: None,
- },
- ));
- }
- }
- None => {
- diagnostics.push(Diagnostic::error(
- param.0,
- "missing return type".to_string(),
- ));
- broken = true;
- }
- }
- }
- if broken {
- return Err(());
- }
- let mut ok_resolved = Vec::new();
- let mut finally_reachable = statement(
- ok,
- &mut ok_resolved,
- context,
- symtable,
- loops,
- ns,
- diagnostics,
- )?;
- symtable.leave_scope();
- let mut clauses_unique = HashSet::new();
- let mut errors_resolved = Vec::new();
- let mut catch_all = None;
- clause_stmts.iter().try_for_each(|clause_stmt| {
- let (loc, name) = match clause_stmt {
- CatchClause::Simple(loc, _, _) => (loc, ""),
- CatchClause::Named(loc, id, _, _) => (loc, id.name.as_str()),
- };
- if !clauses_unique.insert(name) {
- ns.diagnostics.push(Diagnostic::error(
- *loc,
- if name.is_empty() {
- "duplicate catch clause".to_string()
- } else {
- format!("duplicate '{name}' catch clause")
- },
- ));
- return Err(());
- }
- match clause_stmt {
- CatchClause::Simple(_, param, stmt) => {
- symtable.new_scope();
- let mut catch_param = None;
- let mut catch_param_pos = None;
- let mut catch_stmt_resolved = vec![];
- if let Some(param) = param {
- let (catch_ty, ty_loc) =
- resolve_var_decl_ty(¶m.ty, ¶m.storage, context, ns, diagnostics)?;
- if catch_ty != Type::DynamicBytes {
- diagnostics.push(Diagnostic::error(
- param.ty.loc(),
- format!(
- "catch can only take 'bytes memory', not '{}'",
- catch_ty.to_string(ns)
- ),
- ));
- return Err(());
- } else if let Some(annotation) = ¶m.annotation {
- diagnostics.push(unexpected_parameter_annotation(annotation.loc));
- }
- let mut result = Parameter {
- loc: param.loc,
- ty: Type::DynamicBytes,
- ty_loc: Some(ty_loc),
- id: None,
- indexed: false,
- readonly: false,
- infinite_size: false,
- recursive: false,
- annotation: None,
- };
- if let Some(name) = ¶m.name {
- if let Some(pos) = symtable.add(
- name,
- catch_ty,
- ns,
- VariableInitializer::Solidity(None),
- VariableUsage::TryCatchErrorBytes,
- param.storage.clone(),
- ) {
- ns.check_shadowing(context.file_no, context.contract_no, name);
- catch_param_pos = Some(pos);
- result.id = Some(name.clone());
- }
- }
- catch_param = Some(result);
- }
- let reachable = statement(
- stmt,
- &mut catch_stmt_resolved,
- context,
- symtable,
- loops,
- ns,
- diagnostics,
- )?;
- finally_reachable |= reachable;
- symtable.leave_scope();
- catch_all = Some(super::ast::CatchClause {
- param: catch_param,
- param_pos: catch_param_pos,
- stmt: catch_stmt_resolved,
- });
- Ok(())
- }
- CatchClause::Named(_, id, param, stmt) => {
- match ns.target {
- Target::Polkadot { .. } if id.name != "Error" => {
- ns.diagnostics.push(Diagnostic::error(
- id.loc,
- format!("only catch 'Error' is supported, not '{}'", id.name),
- ));
- return Err(());
- }
- Target::EVM if id.name != "Error" && id.name != "Panic" => {
- ns.diagnostics.push(Diagnostic::error(
- id.loc,
- format!(
- "only catch 'Error' or 'Panic' is supported, not '{}'",
- id.name
- ),
- ));
- return Err(());
- }
- _ => {
- if let Some(annotation) = ¶m.annotation {
- ns.diagnostics
- .push(unexpected_parameter_annotation(annotation.loc))
- }
- }
- }
- let (error_ty, ty_loc) =
- resolve_var_decl_ty(¶m.ty, ¶m.storage, context, ns, diagnostics)?;
- if id.name == "Error" && error_ty != Type::String {
- ns.diagnostics.push(Diagnostic::error(
- param.ty.loc(),
- format!(
- "catch Error(...) can only take 'string memory', not '{}'",
- error_ty.to_string(ns)
- ),
- ));
- }
- if id.name == "Panic" && error_ty != Type::Uint(256) {
- ns.diagnostics.push(Diagnostic::error(
- param.ty.loc(),
- format!(
- "catch Panic(...) can only take 'uint256', not '{}'",
- error_ty.to_string(ns)
- ),
- ));
- }
- symtable.new_scope();
- let mut error_pos = None;
- let mut error_stmt_resolved = Vec::new();
- let mut error_param = Parameter {
- loc: id.loc,
- ty: Type::String,
- ty_loc: Some(ty_loc),
- id: None,
- indexed: false,
- readonly: false,
- infinite_size: false,
- recursive: false,
- annotation: None,
- };
- if let Some(name) = ¶m.name {
- if let Some(pos) = symtable.add(
- name,
- Type::String,
- ns,
- VariableInitializer::Solidity(None),
- VariableUsage::TryCatchErrorString,
- param.storage.clone(),
- ) {
- ns.check_shadowing(context.file_no, context.contract_no, name);
- error_pos = Some(pos);
- error_param.id = Some(name.clone());
- }
- }
- let reachable = statement(
- stmt,
- &mut error_stmt_resolved,
- context,
- symtable,
- loops,
- ns,
- diagnostics,
- )?;
- finally_reachable |= reachable;
- symtable.leave_scope();
- errors_resolved.push((error_pos, error_param, error_stmt_resolved));
- Ok(())
- }
- }
- })?;
- let stmt = Statement::TryCatch(
- *loc,
- finally_reachable,
- TryCatch {
- expr: fcall,
- returns: params,
- errors: errors_resolved
- .iter()
- .map(|(pos, param, stmt)| super::ast::CatchClause {
- param: param.clone().into(),
- param_pos: *pos,
- stmt: stmt.clone(),
- })
- .collect(),
- ok_stmt: ok_resolved,
- catch_all,
- },
- );
- Ok((stmt, finally_reachable))
- }
|