From e6a92a6f467f56e25f26cfc41375dc292cec3c82 Mon Sep 17 00:00:00 2001 From: Matt Sheets Date: Mon, 6 Mar 2023 10:25:12 -0700 Subject: [PATCH] review comments -wip --- data/error_policies/call_casting.cas | 2 +- src/compile.rs | 87 +++++++++++++++++----------- src/functions.rs | 2 + 3 files changed, 55 insertions(+), 36 deletions(-) diff --git a/data/error_policies/call_casting.cas b/data/error_policies/call_casting.cas index 64b5a532..d2351c18 100644 --- a/data/error_policies/call_casting.cas +++ b/data/error_policies/call_casting.cas @@ -26,7 +26,7 @@ domain asd { asd.read_boo_tmp(this); // Function is not castable asd.read_boo_tmp_again(this); // Function is not castable asd.read_boo_tmp_more(this); // Function is not castable - asd.some_function(foo); // TODO this should break? + asd.some_function(foo); } virtual resource tmp { diff --git a/src/compile.rs b/src/compile.rs index e5a212a7..34765263 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -8,8 +8,8 @@ use std::convert::TryFrom; use std::ops::Range; use crate::ast::{ - Argument, CascadeString, Declaration, Expression, FuncCall, LetBinding, Machine, Module, - PolicyFile, Statement, + get_cil_name, Argument, CascadeString, Declaration, Expression, FuncCall, LetBinding, Machine, + Module, PolicyFile, Statement, }; use crate::constants; use crate::context::{BindableObject, BlockType, Context as BlockContext}; @@ -510,23 +510,21 @@ pub fn determine_castable(functions: &mut FunctionMap, types: &TypeMap) -> u64 { func.is_castable = false; continue 'outer; } - // The call may be a built-in so check the args - } else { - for arg in &call.args { - if let Argument::Var(arg) = &arg.0 { - // Need to special case this.* - if arg.to_string().contains("this.") { + } + for arg in &call.args { + if let Argument::Var(arg) = &arg.0 { + // Need to special case this.* + if arg.to_string().contains("this.") { + num_changed += 1; + func.is_castable = false; + continue 'outer; + } + if let Some(ti) = types.get(arg.as_ref()) { + if ti.is_associated_resource(types) { num_changed += 1; func.is_castable = false; continue 'outer; } - if let Some(ti) = types.get(arg.as_ref()) { - if ti.is_associated_resource(types) { - num_changed += 1; - func.is_castable = false; - continue 'outer; - } - } } } } @@ -546,8 +544,8 @@ pub fn initialize_terminated<'a>( let mut is_term = true; for call in func.original_body { - if let Statement::Call(call) = call { - match call.check_builtin() { + match call { + Statement::Call(call) => match call.check_builtin() { Some(_) => { continue; } @@ -555,6 +553,15 @@ pub fn initialize_terminated<'a>( is_term = false; break; } + }, + Statement::LetBinding(_) => { + continue; + } + Statement::IfBlock(_) => { + continue; + } + Statement::OptionalBlock(_) => { + continue; } } } @@ -576,29 +583,39 @@ pub fn search_for_recursion( while removed > 0 { removed = 0; for func in functions.clone().iter_mut() { - let mut is_term = true; + let mut is_term = false; for call in func.original_body { - if let Statement::Call(call) = call { - let mut call_cil_name = call.get_cil_name(); - // If we are calling something with this it must be in the same class - // so hand place the class name - if call_cil_name.contains("this-") { - if let Some(class) = func.class { - call_cil_name = - call_cil_name.replace("this-", &(class.name.to_string() + "-")) - } else { - return Err(CascadeErrors::from(ErrorItem::make_compile_or_internal_error( - "Could not determine class for 'this.' function call", - Some(func.declaration_file), - call.get_name_range(), - "Perhaps you meant to place the function in a resource or domain?"))); + match call { + Statement::Call(call) => { + let mut call_cil_name = call.get_cil_name(); + // If we are calling something with this it must be in the same class + // so hand place the class name + if call_cil_name.contains("this-") { + if let FunctionClass::Type(class) = func.class { + call_cil_name = get_cil_name(Some(&class.name), &call.name) + } else { + return Err(CascadeErrors::from(ErrorItem::make_compile_or_internal_error( + "Could not determine class for 'this.' function call", + Some(func.declaration_file), + call.get_name_range(), + "Perhaps you meant to place the function in a resource or domain?"))); + } } - } - if terminated_list.contains(&call_cil_name) { + if terminated_list.contains(&call_cil_name) { + is_term = true; + break; + } + } + Statement::LetBinding(_) => { + continue; + } + Statement::IfBlock(_) => { + continue; + } + Statement::OptionalBlock(_) => { continue; } - is_term = false; } } if is_term { diff --git a/src/functions.rs b/src/functions.rs index 9b7749b7..cc564b08 100644 --- a/src/functions.rs +++ b/src/functions.rs @@ -1341,6 +1341,8 @@ pub struct FunctionInfo<'a> { pub declaration_file: &'a SimpleFile, pub is_associated_call: bool, pub is_derived: bool, + // This will be initialized to true and prevalidate_functions will set this + // to false if needed. pub is_castable: bool, decl: Option<&'a FuncDecl>, }