Skip to content

Commit

Permalink
recursion check passes around strings instead of funcInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-sheets committed Mar 7, 2023
1 parent 5d1ac6d commit 93d48a9
Showing 1 changed file with 51 additions and 47 deletions.
98 changes: 51 additions & 47 deletions src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ use crate::functions::{
FunctionInfo, FunctionMap, ValidatedCall, ValidatedStatement,
};
use crate::internal_rep::{
generate_sid_rules, validate_derive_args, Annotated, AnnotationInfo, Associated, BoundTypeInfo,
ClassList, Context, Sid, TypeInfo, TypeInstance, TypeMap,
generate_sid_rules, validate_derive_args, Annotated,
AnnotationInfo, Associated, BoundTypeInfo, ClassList, Context, Sid, TypeInfo, TypeInstance,
TypeMap,
};
use crate::machine::{MachineMap, ModuleMap, ValidatedMachine, ValidatedModule};
use crate::warning::{Warnings, WithWarnings};
Expand Down Expand Up @@ -534,11 +535,9 @@ pub fn determine_castable(functions: &mut FunctionMap, types: &TypeMap) -> u64 {
num_changed
}

pub fn initialize_terminated<'a>(
functions: &'a FunctionMap<'a>,
) -> (Vec<String>, Vec<FunctionInfo<'a>>) {
pub fn initialize_terminated<'a>(functions: &'a FunctionMap<'a>) -> (Vec<String>, Vec<String>) {
let mut term_ret_vec: Vec<String> = Vec::new();
let mut nonterm_ret_vec: Vec<FunctionInfo> = Vec::new();
let mut nonterm_ret_vec: Vec<String> = Vec::new();

for func in functions.values() {
let mut is_term = true;
Expand All @@ -559,7 +558,7 @@ pub fn initialize_terminated<'a>(
if is_term {
term_ret_vec.push(func.get_cil_name().clone());
} else {
nonterm_ret_vec.push(func.clone());
nonterm_ret_vec.push(func.get_cil_name().clone());
}
}

Expand All @@ -568,46 +567,49 @@ pub fn initialize_terminated<'a>(

pub fn search_for_recursion(
terminated_list: &mut Vec<String>,
functions: &mut Vec<FunctionInfo>,
functions: &mut Vec<String>,
function_map: &FunctionMap,
) -> Result<(), CascadeErrors> {
let mut removed: u64 = 1;
while removed > 0 {
removed = 0;
for func in functions.clone().iter_mut() {
for func in functions.clone().iter() {
let mut is_term = false;
let func_calls = get_all_func_calls(func.original_body.to_vec());
for call in func_calls {
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 let Some(function_info) = function_map.get(func) {
let func_calls = get_all_func_calls(function_info.original_body.to_vec());
for call in func_calls {
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) = function_info.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(function_info.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) {
is_term = true;
break;
if terminated_list.contains(&call_cil_name) {
is_term = true;
break;
}
}
if is_term {
terminated_list.push(function_info.get_cil_name());
removed += 1;
let index = functions
.iter()
.position(|x| *x == function_info.get_cil_name())
.unwrap();
functions.remove(index);
}
}
if is_term {
terminated_list.push(func.get_cil_name());
removed += 1;
let index = functions
.iter()
.position(|x| *x.get_cil_name() == func.get_cil_name())
.unwrap();
functions.remove(index);
}
}
}
Expand All @@ -616,13 +618,15 @@ pub fn search_for_recursion(
let mut error: Option<CompileError> = None;

for func in functions {
error = Some(add_or_create_compile_error(
error,
"Recursive Function call found",
func.declaration_file,
func.get_declaration_range().unwrap_or_default(),
"Calls cannot recursively call each other",
));
if let Some(function_info) = function_map.get(func) {
error = Some(add_or_create_compile_error(
error,
"Recursive Function call found",
function_info.declaration_file,
function_info.get_declaration_range().unwrap_or_default(),
"Calls cannot recursively call each other",
));
}
}
// Unwrap is safe since we need to go through the loop above at least once
return Err(CascadeErrors::from(error.unwrap()));
Expand Down Expand Up @@ -660,7 +664,7 @@ pub fn prevalidate_functions(
return Err(CascadeErrors::from(error.unwrap()));
}

search_for_recursion(&mut terminated_functions, &mut nonterm_functions)?;
search_for_recursion(&mut terminated_functions, &mut nonterm_functions, functions)?;

Ok(())
}
Expand Down

0 comments on commit 93d48a9

Please sign in to comment.